diff --git a/.github/workflows/codeing_guildelines.yml b/.github/workflows/codeing_guildelines.yml index 9c0f71c5d..de1364f28 100644 --- a/.github/workflows/codeing_guildelines.yml +++ b/.github/workflows/codeing_guildelines.yml @@ -33,7 +33,7 @@ jobs: uses: actions/checkout@v2 with: fetch-depth: 0 - + # github.event.pull_request.base.label = ${{github.repository}}/${{github.base_ref}} - name: Run Coding Guidelines Checks run: /usr/bin/env python3 ./ci/coding_guidelines_check.py --commits ${{ github.event.pull_request.base.sha }}..HEAD diff --git a/core/app-framework/app-native-shared/attr_container.c b/core/app-framework/app-native-shared/attr_container.c index b27dacdf7..e8c120196 100644 --- a/core/app-framework/app-native-shared/attr_container.c +++ b/core/app-framework/app-native-shared/attr_container.c @@ -16,79 +16,89 @@ typedef union jvalue { double d; } jvalue; - - -static inline int16_t get_int16(const char *buf) +static inline int16_t +get_int16(const char *buf) { int16_t ret; bh_memcpy_s(&ret, sizeof(int16_t), buf, sizeof(int16_t)); return ret; } -static inline uint16_t get_uint16(const char *buf) +static inline uint16_t +get_uint16(const char *buf) { return get_int16(buf); } -static inline int32_t get_int32(const char *buf) +static inline int32_t +get_int32(const char *buf) { int32_t ret; bh_memcpy_s(&ret, sizeof(int32_t), buf, sizeof(int32_t)); return ret; } -static inline uint32_t get_uint32(const char *buf) +static inline uint32_t +get_uint32(const char *buf) { return get_int32(buf); } -static inline int64_t get_int64(const char *buf) +static inline int64_t +get_int64(const char *buf) { int64_t ret; bh_memcpy_s(&ret, sizeof(int64_t), buf, sizeof(int64_t)); return ret; } -static inline uint64_t get_uint64(const char *buf) +static inline uint64_t +get_uint64(const char *buf) { return get_int64(buf); } -static inline void set_int16(char *buf, int16_t v) +static inline void +set_int16(char *buf, int16_t v) { bh_memcpy_s(buf, sizeof(int16_t), &v, sizeof(int16_t)); } -static inline void set_uint16(char *buf, uint16_t v) +static inline void +set_uint16(char *buf, uint16_t v) { bh_memcpy_s(buf, sizeof(uint16_t), &v, sizeof(uint16_t)); } -static inline void set_int32(char *buf, int32_t v) +static inline void +set_int32(char *buf, int32_t v) { bh_memcpy_s(buf, sizeof(int32_t), &v, sizeof(int32_t)); } -static inline void set_uint32(char *buf, uint32_t v) +static inline void +set_uint32(char *buf, uint32_t v) { bh_memcpy_s(buf, sizeof(uint32_t), &v, sizeof(uint32_t)); } -static inline void set_int64(char *buf, int64_t v) +static inline void +set_int64(char *buf, int64_t v) { bh_memcpy_s(buf, sizeof(int64_t), &v, sizeof(int64_t)); } -static inline void set_uint64(char *buf, uint64_t v) +static inline void +set_uint64(char *buf, uint64_t v) { bh_memcpy_s(buf, sizeof(uint64_t), &v, sizeof(uint64_t)); } -char* +char * attr_container_get_attr_begin(const attr_container_t *attr_cont, - uint32_t *p_total_length, uint16_t *p_attr_num) + uint32_t *p_total_length, uint16_t *p_attr_num) { - char *p = (char*) attr_cont->buf; + char *p = (char *)attr_cont->buf; uint16_t str_len, attr_num; uint32_t total_length; @@ -125,10 +135,10 @@ attr_container_get_attr_begin(const attr_container_t *attr_cont, return p; } -static char* +static char * attr_container_get_attr_next(const char *curr_attr) { - char *p = (char*) curr_attr; + char *p = (char *)curr_attr; uint8_t type; /* key length and key */ @@ -154,7 +164,7 @@ attr_container_get_attr_next(const char *curr_attr) return NULL; } -static const char* +static const char * attr_container_find_attr(const attr_container_t *attr_cont, const char *key) { uint32_t total_length; @@ -164,7 +174,8 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key) if (!key) return NULL; - if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num))) + if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, + &attr_num))) return NULL; for (i = 0; i < attr_num; i++) { @@ -173,9 +184,9 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key) return NULL; if (str_len == strlen(key) + 1 - && memcmp(p + sizeof(uint16_t), key, str_len) == 0) { - if ((uint32_t)(p + sizeof(uint16_t) + str_len - - attr_cont->buf) >= total_length) + && memcmp(p + sizeof(uint16_t), key, str_len) == 0) { + if ((uint32_t)(p + sizeof(uint16_t) + str_len - attr_cont->buf) + >= total_length) return NULL; return p; } @@ -187,14 +198,15 @@ attr_container_find_attr(const attr_container_t *attr_cont, const char *key) return NULL; } -char* +char * attr_container_get_attr_end(const attr_container_t *attr_cont) { uint32_t total_length; uint16_t attr_num, i; char *p; - if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, &attr_num))) + if (!(p = attr_container_get_attr_begin(attr_cont, &total_length, + &attr_num))) return NULL; for (i = 0; i < attr_num; i++) @@ -204,14 +216,15 @@ attr_container_get_attr_end(const attr_container_t *attr_cont) return p; } -static char* +static char * attr_container_get_msg_end(attr_container_t *attr_cont) { char *p = attr_cont->buf; return p + get_uint32(p); } -uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont) +uint16_t +attr_container_get_attr_num(const attr_container_t *attr_cont) { uint16_t str_len; /* skip total length */ @@ -225,7 +238,8 @@ uint16_t attr_container_get_attr_num(const attr_container_t *attr_cont) return get_uint16(p); } -static void attr_container_inc_attr_num(attr_container_t *attr_cont) +static void +attr_container_inc_attr_num(attr_container_t *attr_cont) { uint16_t str_len, attr_num; /* skip total length */ @@ -249,12 +263,12 @@ attr_container_create(const char *tag) tag_length = tag ? strlen(tag) + 1 : 1; length = offsetof(attr_container_t, buf) + - /* total length + tag length + tag + reserved 100 bytes */ - sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100; + /* total length + tag length + tag + reserved 100 bytes */ + sizeof(uint32_t) + sizeof(uint16_t) + tag_length + 100; if (!(attr_cont = attr_container_malloc(length))) { attr_container_printf( - "Create attr_container failed: allocate memory failed.\r\n"); + "Create attr_container failed: allocate memory failed.\r\n"); return NULL; } @@ -274,34 +288,37 @@ attr_container_create(const char *tag) return attr_cont; } -void attr_container_destroy(const attr_container_t *attr_cont) +void +attr_container_destroy(const attr_container_t *attr_cont) { if (attr_cont) - attr_container_free((char*) attr_cont); + attr_container_free((char *)attr_cont); } -static bool check_set_attr(attr_container_t **p_attr_cont, const char *key) +static bool +check_set_attr(attr_container_t **p_attr_cont, const char *key) { uint32_t flags; if (!p_attr_cont || !*p_attr_cont || !key || strlen(key) == 0) { attr_container_printf( - "Set attribute failed: invalid input arguments.\r\n"); + "Set attribute failed: invalid input arguments.\r\n"); return false; } - flags = get_uint32((char*) *p_attr_cont); + flags = get_uint32((char *)*p_attr_cont); if (flags & ATTR_CONT_READONLY_SHIFT) { attr_container_printf( - "Set attribute failed: attribute container is readonly.\r\n"); + "Set attribute failed: attribute container is readonly.\r\n"); return false; } return true; } -bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, - int type, const void *value, int value_length) +bool +attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, + int type, const void *value, int value_length) { attr_container_t *attr_cont, *attr_cont1; uint16_t str_len; @@ -351,13 +368,14 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, set_uint16(p, value_length); p += sizeof(uint16_t); bh_memcpy_s(p, value_length, value, value_length); - } else if (type == ATTR_TYPE_BYTEARRAY) { + } + else if (type == ATTR_TYPE_BYTEARRAY) { set_uint32(p, value_length); p += sizeof(uint32_t); bh_memcpy_s(p, value_length, value, value_length); } - if ((p = (char*) attr_container_find_attr(attr_cont, key))) { + if ((p = (char *)attr_container_find_attr(attr_cont, key))) { /* key found */ p1 = attr_container_get_attr_next(p); @@ -375,22 +393,21 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, } total_length += attr_len + 100; - if (!(attr_cont1 = attr_container_malloc( - offsetof(attr_container_t, buf) + total_length))) { + if (!(attr_cont1 = attr_container_malloc(offsetof(attr_container_t, buf) + + total_length))) { attr_container_printf( - "Set attr failed: allocate memory failed.\r\n"); + "Set attr failed: allocate memory failed.\r\n"); attr_container_free(attr_buf); return false; } - bh_memcpy_s(attr_cont1, p - (char* )attr_cont, attr_cont, - p - (char* )attr_cont); - bh_memcpy_s((char* )attr_cont1 + (unsigned )(p - (char* )attr_cont), - attr_end - p1, p1, attr_end - p1); - bh_memcpy_s( - (char* )attr_cont1 + (unsigned )(p - (char* )attr_cont) - + (unsigned )(attr_end - p1), attr_len, attr_buf, - attr_len); + bh_memcpy_s(attr_cont1, p - (char *)attr_cont, attr_cont, + p - (char *)attr_cont); + bh_memcpy_s((char *)attr_cont1 + (unsigned)(p - (char *)attr_cont), + attr_end - p1, p1, attr_end - p1); + bh_memcpy_s((char *)attr_cont1 + (unsigned)(p - (char *)attr_cont) + + (unsigned)(attr_end - p1), + attr_len, attr_buf, attr_len); p = attr_cont1->buf; set_uint32(p, total_length); *p_attr_cont = attr_cont1; @@ -398,7 +415,8 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, attr_container_free(attr_cont); attr_container_free(attr_buf); return true; - } else { + } + else { /* key not found */ if ((uint32_t)(msg_end - attr_end) >= attr_len) { bh_memcpy_s(attr_end, msg_end - attr_end, attr_buf, attr_len); @@ -408,19 +426,19 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, } total_length += attr_len + 100; - if (!(attr_cont1 = attr_container_malloc( - offsetof(attr_container_t, buf) + total_length))) { + if (!(attr_cont1 = attr_container_malloc(offsetof(attr_container_t, buf) + + total_length))) { attr_container_printf( - "Set attr failed: allocate memory failed.\r\n"); + "Set attr failed: allocate memory failed.\r\n"); attr_container_free(attr_buf); return false; } - bh_memcpy_s(attr_cont1, attr_end - (char* )attr_cont, attr_cont, - attr_end - (char* )attr_cont); - bh_memcpy_s( - (char* )attr_cont1 + (unsigned )(attr_end - (char* )attr_cont), - attr_len, attr_buf, attr_len); + bh_memcpy_s(attr_cont1, attr_end - (char *)attr_cont, attr_cont, + attr_end - (char *)attr_cont); + bh_memcpy_s((char *)attr_cont1 + + (unsigned)(attr_end - (char *)attr_cont), + attr_len, attr_buf, attr_len); attr_container_inc_attr_num(attr_cont1); p = attr_cont1->buf; set_uint32(p, total_length); @@ -434,88 +452,101 @@ bool attr_container_set_attr(attr_container_t **p_attr_cont, const char *key, return false; } -bool attr_container_set_short(attr_container_t **p_attr_cont, const char *key, - short value) +bool +attr_container_set_short(attr_container_t **p_attr_cont, const char *key, + short value) { - return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_SHORT, &value, 2); + return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_SHORT, &value, + 2); } -bool attr_container_set_int(attr_container_t **p_attr_cont, const char *key, - int value) +bool +attr_container_set_int(attr_container_t **p_attr_cont, const char *key, + int value) { return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT, &value, 4); } -bool attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, - int64_t value) +bool +attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, + int64_t value) { - return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT64, &value, 8); + return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT64, &value, + 8); } -bool attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, - int8_t value) +bool +attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, + int8_t value) { return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTE, &value, 1); } -bool attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, - uint16_t value) +bool +attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, + uint16_t value) { return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT16, &value, - 2); + 2); } -bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key, - float value) +bool +attr_container_set_float(attr_container_t **p_attr_cont, const char *key, + float value) { - return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_FLOAT, &value, 4); + return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_FLOAT, &value, + 4); } -bool attr_container_set_double(attr_container_t **p_attr_cont, const char *key, - double value) +bool +attr_container_set_double(attr_container_t **p_attr_cont, const char *key, + double value) { return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_DOUBLE, &value, - 8); + 8); } -bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, - bool value) +bool +attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, + bool value) { int8_t value1 = value ? 1 : 0; return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BOOLEAN, &value1, - 1); + 1); } -bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key, - const char *value) +bool +attr_container_set_string(attr_container_t **p_attr_cont, const char *key, + const char *value) { if (!value) { attr_container_printf("Set attr failed: invald input arguments.\r\n"); return false; } return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_STRING, value, - strlen(value) + 1);; + strlen(value) + 1); } -bool attr_container_set_bytearray(attr_container_t **p_attr_cont, - const char *key, const int8_t *value, unsigned length) +bool +attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key, + const int8_t *value, unsigned length) { if (!value) { attr_container_printf("Set attr failed: invald input arguments.\r\n"); return false; } return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTEARRAY, value, - length);; + length); } -static const char* +static const char * attr_container_get_attr(const attr_container_t *attr_cont, const char *key) { const char *attr_addr; if (!attr_cont || !key) { attr_container_printf( - "Get attribute failed: invalid input arguments.\r\n"); + "Get attribute failed: invalid input arguments.\r\n"); return NULL; } @@ -528,101 +559,103 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key) return attr_addr + 2 + strlen(key) + 1; } -#define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) do {\ - jvalue val; \ - const char *addr = attr_container_get_attr(attr, key); \ - uint8_t type; \ - if (!addr) \ - return 0; \ - val.j = 0; \ - type = *(uint8_t*)addr++; \ - switch (type) { \ - case ATTR_TYPE_SHORT: \ - case ATTR_TYPE_INT: \ - case ATTR_TYPE_INT64: \ - case ATTR_TYPE_BYTE: \ - case ATTR_TYPE_UINT16: \ - case ATTR_TYPE_FLOAT: \ - case ATTR_TYPE_DOUBLE: \ - case ATTR_TYPE_BOOLEAN: \ - bh_memcpy_s(&val, sizeof(val.var_name), addr, 1 << (type & 3)); \ - break; \ - case ATTR_TYPE_STRING: \ - { \ - unsigned len= get_uint16(addr); \ - addr += 2; \ - if (len > sizeof(val.var_name)) \ - len = sizeof(val.var_name); \ - bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ - break; \ - } \ - case ATTR_TYPE_BYTEARRAY: \ - { \ - unsigned len= get_uint32(addr); \ - addr += 4; \ - if (len > sizeof(val.var_name)) \ - len = sizeof(val.var_name); \ - bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ - break; \ - } \ - default: \ - bh_assert(0); \ - break; \ - } \ - return val.var_name; \ - } while (0) +#define TEMPLATE_ATTR_BUF_TO_VALUE(attr, key, var_name) \ + do { \ + jvalue val; \ + const char *addr = attr_container_get_attr(attr, key); \ + uint8_t type; \ + if (!addr) \ + return 0; \ + val.j = 0; \ + type = *(uint8_t *)addr++; \ + switch (type) { \ + case ATTR_TYPE_SHORT: \ + case ATTR_TYPE_INT: \ + case ATTR_TYPE_INT64: \ + case ATTR_TYPE_BYTE: \ + case ATTR_TYPE_UINT16: \ + case ATTR_TYPE_FLOAT: \ + case ATTR_TYPE_DOUBLE: \ + case ATTR_TYPE_BOOLEAN: \ + bh_memcpy_s(&val, sizeof(val.var_name), addr, \ + 1 << (type & 3)); \ + break; \ + case ATTR_TYPE_STRING: \ + { \ + unsigned len = get_uint16(addr); \ + addr += 2; \ + if (len > sizeof(val.var_name)) \ + len = sizeof(val.var_name); \ + bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ + break; \ + } \ + case ATTR_TYPE_BYTEARRAY: \ + { \ + unsigned len = get_uint32(addr); \ + addr += 4; \ + if (len > sizeof(val.var_name)) \ + len = sizeof(val.var_name); \ + bh_memcpy_s(&val.var_name, sizeof(val.var_name), addr, len); \ + break; \ + } \ + default: \ + bh_assert(0); \ + break; \ + } \ + return val.var_name; \ + } while (0) -short attr_container_get_as_short(const attr_container_t *attr_cont, - const char *key) +short +attr_container_get_as_short(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); } -int attr_container_get_as_int(const attr_container_t *attr_cont, - const char *key) +int +attr_container_get_as_int(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i); } -int64_t attr_container_get_as_int64(const attr_container_t *attr_cont, - const char *key) +int64_t +attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j); } -int8_t attr_container_get_as_byte(const attr_container_t *attr_cont, - const char *key) +int8_t +attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b); } -uint16_t attr_container_get_as_uint16(const attr_container_t *attr_cont, - const char *key) +uint16_t +attr_container_get_as_uint16(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); } -float attr_container_get_as_float(const attr_container_t *attr_cont, - const char *key) +float +attr_container_get_as_float(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, f); } -double attr_container_get_as_double(const attr_container_t *attr_cont, - const char *key) +double +attr_container_get_as_double(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, d); } -bool attr_container_get_as_bool(const attr_container_t *attr_cont, - const char *key) +bool +attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key) { TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, z); } -const int8_t* +const int8_t * attr_container_get_as_bytearray(const attr_container_t *attr_cont, - const char *key, unsigned *array_length) + const char *key, unsigned *array_length) { const char *addr = attr_container_get_attr(attr_cont, key); uint8_t type; @@ -636,68 +669,68 @@ attr_container_get_as_bytearray(const attr_container_t *attr_cont, return NULL; } - type = *(uint8_t*) addr++; + type = *(uint8_t *)addr++; switch (type) { - case ATTR_TYPE_SHORT: - case ATTR_TYPE_INT: - case ATTR_TYPE_INT64: - case ATTR_TYPE_BYTE: - case ATTR_TYPE_UINT16: - case ATTR_TYPE_FLOAT: - case ATTR_TYPE_DOUBLE: - case ATTR_TYPE_BOOLEAN: - length = 1 << (type & 3); - break; - case ATTR_TYPE_STRING: - length = get_uint16(addr); - addr += 2; - break; - case ATTR_TYPE_BYTEARRAY: - length = get_uint32(addr); - addr += 4; - break; - default: - return NULL; + case ATTR_TYPE_SHORT: + case ATTR_TYPE_INT: + case ATTR_TYPE_INT64: + case ATTR_TYPE_BYTE: + case ATTR_TYPE_UINT16: + case ATTR_TYPE_FLOAT: + case ATTR_TYPE_DOUBLE: + case ATTR_TYPE_BOOLEAN: + length = 1 << (type & 3); + break; + case ATTR_TYPE_STRING: + length = get_uint16(addr); + addr += 2; + break; + case ATTR_TYPE_BYTEARRAY: + length = get_uint32(addr); + addr += 4; + break; + default: + return NULL; } *array_length = length; - return (const int8_t*) addr; + return (const int8_t *)addr; } -char* +char * attr_container_get_as_string(const attr_container_t *attr_cont, const char *key) { unsigned array_length; - return (char*) attr_container_get_as_bytearray(attr_cont, key, - &array_length); + return (char *)attr_container_get_as_bytearray(attr_cont, key, + &array_length); } -const char* +const char * attr_container_get_tag(const attr_container_t *attr_cont) { - return attr_cont ? - attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t) : NULL; + return attr_cont ? attr_cont->buf + sizeof(uint32_t) + sizeof(uint16_t) + : NULL; } -bool attr_container_contain_key(const attr_container_t *attr_cont, - const char *key) +bool +attr_container_contain_key(const attr_container_t *attr_cont, const char *key) { if (!attr_cont || !key || !strlen(key)) { attr_container_printf( - "Check contain key failed: invalid input arguments.\r\n"); + "Check contain key failed: invalid input arguments.\r\n"); return false; } return attr_container_find_attr(attr_cont, key) ? true : false; } -unsigned int attr_container_get_serialize_length( - const attr_container_t *attr_cont) +unsigned int +attr_container_get_serialize_length(const attr_container_t *attr_cont) { const char *p; if (!attr_cont) { - attr_container_printf( - "Get container serialize length failed: invalid input arguments.\r\n"); + attr_container_printf("Get container serialize length failed: invalid " + "input arguments.\r\n"); return 0; } @@ -705,7 +738,8 @@ unsigned int attr_container_get_serialize_length( return sizeof(uint16_t) + get_uint32(p); } -bool attr_container_serialize(char *buf, const attr_container_t *attr_cont) +bool +attr_container_serialize(char *buf, const attr_container_t *attr_cont) { const char *p; uint16_t flags; @@ -713,7 +747,7 @@ bool attr_container_serialize(char *buf, const attr_container_t *attr_cont) if (!buf || !attr_cont) { attr_container_printf( - "Container serialize failed: invalid input arguments.\r\n"); + "Container serialize failed: invalid input arguments.\r\n"); return false; } @@ -721,27 +755,29 @@ bool attr_container_serialize(char *buf, const attr_container_t *attr_cont) length = sizeof(uint16_t) + get_uint32(p); bh_memcpy_s(buf, length, attr_cont, length); /* Set readonly */ - flags = get_uint16((const char*) attr_cont); + flags = get_uint16((const char *)attr_cont); set_uint16(buf, flags | (1 << ATTR_CONT_READONLY_SHIFT)); return true; } -bool attr_container_is_constant(const attr_container_t* attr_cont) +bool +attr_container_is_constant(const attr_container_t *attr_cont) { uint16_t flags; if (!attr_cont) { attr_container_printf( - "Container check const: invalid input arguments.\r\n"); + "Container check const: invalid input arguments.\r\n"); return false; } - flags = get_uint16((const char*) attr_cont); + flags = get_uint16((const char *)attr_cont); return (flags & (1 << ATTR_CONT_READONLY_SHIFT)) ? true : false; } -void attr_container_dump(const attr_container_t *attr_cont) +void +attr_container_dump(const attr_container_t *attr_cont) { uint32_t total_length; uint16_t attr_num, i, type; @@ -771,64 +807,64 @@ void attr_container_dump(const attr_container_t *attr_cont) attr_container_printf(" key: %s", key); switch (type) { - case ATTR_TYPE_SHORT: - bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); - attr_container_printf(", type: short, value: 0x%x\n", - value.s & 0xFFFF); - p += 2; - break; - case ATTR_TYPE_INT: - bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); - attr_container_printf(", type: int, value: 0x%x\n", value.i); - p += 4; - break; - case ATTR_TYPE_INT64: - bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); - attr_container_printf(", type: int64, value: 0x%llx\n", (long long unsigned int)(value.j)); - p += 8; - break; - case ATTR_TYPE_BYTE: - bh_memcpy_s(&value.b, 1, p, 1); - attr_container_printf(", type: byte, value: 0x%x\n", - value.b & 0xFF); - p++; - break; - case ATTR_TYPE_UINT16: - bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); - attr_container_printf(", type: uint16, value: 0x%x\n", value.c); - p += 2; - break; - case ATTR_TYPE_FLOAT: - bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); - attr_container_printf(", type: float, value: %f\n", value.f); - p += 4; - break; - case ATTR_TYPE_DOUBLE: - bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double)); - attr_container_printf(", type: double, value: %f\n", value.d); - p += 8; - break; - case ATTR_TYPE_BOOLEAN: - bh_memcpy_s(&value.z, 1, p, 1); - attr_container_printf(", type: bool, value: 0x%x\n", value.z); - p++; - break; - case ATTR_TYPE_STRING: - attr_container_printf(", type: string, value: %s\n", - p + sizeof(uint16_t)); - p += sizeof(uint16_t) + get_uint16(p); - break; - case ATTR_TYPE_BYTEARRAY: - attr_container_printf(", type: byte array, length: %d\n", - get_uint32(p)); - p += sizeof(uint32_t) + get_uint32(p); - break; - default: - bh_assert(0); - break; + case ATTR_TYPE_SHORT: + bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); + attr_container_printf(", type: short, value: 0x%x\n", + value.s & 0xFFFF); + p += 2; + break; + case ATTR_TYPE_INT: + bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); + attr_container_printf(", type: int, value: 0x%x\n", value.i); + p += 4; + break; + case ATTR_TYPE_INT64: + bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); + attr_container_printf(", type: int64, value: 0x%llx\n", + (long long unsigned int)(value.j)); + p += 8; + break; + case ATTR_TYPE_BYTE: + bh_memcpy_s(&value.b, 1, p, 1); + attr_container_printf(", type: byte, value: 0x%x\n", + value.b & 0xFF); + p++; + break; + case ATTR_TYPE_UINT16: + bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); + attr_container_printf(", type: uint16, value: 0x%x\n", value.c); + p += 2; + break; + case ATTR_TYPE_FLOAT: + bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); + attr_container_printf(", type: float, value: %f\n", value.f); + p += 4; + break; + case ATTR_TYPE_DOUBLE: + bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double)); + attr_container_printf(", type: double, value: %f\n", value.d); + p += 8; + break; + case ATTR_TYPE_BOOLEAN: + bh_memcpy_s(&value.z, 1, p, 1); + attr_container_printf(", type: bool, value: 0x%x\n", value.z); + p++; + break; + case ATTR_TYPE_STRING: + attr_container_printf(", type: string, value: %s\n", + p + sizeof(uint16_t)); + p += sizeof(uint16_t) + get_uint16(p); + break; + case ATTR_TYPE_BYTEARRAY: + attr_container_printf(", type: byte array, length: %d\n", + get_uint32(p)); + p += sizeof(uint32_t) + get_uint32(p); + break; + default: + bh_assert(0); + break; } } attr_container_printf("\n"); } - diff --git a/core/app-framework/app-native-shared/bi-inc/attr_container.h b/core/app-framework/app-native-shared/bi-inc/attr_container.h index e70d0e4c4..3b7be8062 100644 --- a/core/app-framework/app-native-shared/bi-inc/attr_container.h +++ b/core/app-framework/app-native-shared/bi-inc/attr_container.h @@ -34,7 +34,7 @@ enum { ATTR_TYPE_END = ATTR_TYPE_BYTEARRAY }; -#define ATTR_CONT_READONLY_SHIFT 2 +#define ATTR_CONT_READONLY_SHIFT 2 typedef struct attr_container { /* container flag: @@ -87,7 +87,7 @@ attr_container_destroy(const attr_container_t *attr_cont); */ bool attr_container_set_short(attr_container_t **p_attr_cont, const char *key, - short value); + short value); /** * Set int attribute in attribute container @@ -101,7 +101,7 @@ attr_container_set_short(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_int(attr_container_t **p_attr_cont, const char *key, - int value); + int value); /** * Set int64 attribute in attribute container @@ -115,7 +115,7 @@ attr_container_set_int(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, - int64_t value); + int64_t value); /** * Set byte attribute in attribute container @@ -129,7 +129,7 @@ attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, - int8_t value); + int8_t value); /** * Set uint16 attribute in attribute container @@ -143,7 +143,7 @@ attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, - uint16_t value); + uint16_t value); /** * Set float attribute in attribute container @@ -157,7 +157,7 @@ attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_float(attr_container_t **p_attr_cont, const char *key, - float value); + float value); /** * Set double attribute in attribute container @@ -171,7 +171,7 @@ attr_container_set_float(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_double(attr_container_t **p_attr_cont, const char *key, - double value); + double value); /** * Set bool attribute in attribute container @@ -185,7 +185,7 @@ attr_container_set_double(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, - bool value); + bool value); /** * Set string attribute in attribute container @@ -199,7 +199,7 @@ attr_container_set_bool(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_string(attr_container_t **p_attr_cont, const char *key, - const char *value); + const char *value); /** * Set bytearray attribute in attribute container @@ -214,7 +214,7 @@ attr_container_set_string(attr_container_t **p_attr_cont, const char *key, */ bool attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key, - const int8_t *value, unsigned length); + const int8_t *value, unsigned length); /** * Get tag of current attribute container @@ -223,7 +223,7 @@ attr_container_set_bytearray(attr_container_t **p_attr_cont, const char *key, * * @return tag of current attribute container */ -const char* +const char * attr_container_get_tag(const attr_container_t *attr_cont); /** @@ -306,7 +306,7 @@ attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key); */ uint16_t attr_container_get_as_uint16(const attr_container_t *attr_cont, - const char *key); + const char *key); /** * Get attribute from attribute container and return it as float value, @@ -331,7 +331,7 @@ attr_container_get_as_float(const attr_container_t *attr_cont, const char *key); */ double attr_container_get_as_double(const attr_container_t *attr_cont, - const char *key); + const char *key); /** * Get attribute from attribute container and return it as bool value, @@ -354,9 +354,9 @@ attr_container_get_as_bool(const attr_container_t *attr_cont, const char *key); * * @return the string value of the attribute, NULL if key isn't found */ -char* +char * attr_container_get_as_string(const attr_container_t *attr_cont, - const char *key); + const char *key); /** * Get attribute from attribute container and return it as bytearray value, @@ -367,9 +367,9 @@ attr_container_get_as_string(const attr_container_t *attr_cont, * * @return the bytearray value of the attribute, NULL if key isn't found */ -const int8_t* +const int8_t * attr_container_get_as_bytearray(const attr_container_t *attr_cont, - const char *key, unsigned *array_length); + const char *key, unsigned *array_length); /** * Get the buffer size of attribute container @@ -400,7 +400,7 @@ attr_container_serialize(char *buf, const attr_container_t *attr_cont); * @return true if const, false otherwise */ bool -attr_container_is_constant(const attr_container_t* attr_cont); +attr_container_is_constant(const attr_container_t *attr_cont); void attr_container_dump(const attr_container_t *attr_cont); @@ -422,4 +422,3 @@ attr_container_dump(const attr_container_t *attr_cont); #endif #endif /* end of _ATTR_CONTAINER_H_ */ - diff --git a/core/app-framework/app-native-shared/bi-inc/shared_utils.h b/core/app-framework/app-native-shared/bi-inc/shared_utils.h index 3154b4b5c..8155ea1f7 100644 --- a/core/app-framework/app-native-shared/bi-inc/shared_utils.h +++ b/core/app-framework/app-native-shared/bi-inc/shared_utils.h @@ -12,8 +12,8 @@ extern "C" { #endif -#define FMT_ATTR_CONTAINER 99 -#define FMT_APP_RAW_BINARY 98 +#define FMT_ATTR_CONTAINER 99 +#define FMT_APP_RAW_BINARY 98 /* the request structure */ typedef struct request { @@ -32,10 +32,10 @@ typedef struct request { // payload of the request, currently only support attr_container_t type void *payload; - //length in bytes of the payload + // length in bytes of the payload int payload_len; - //sender of the request + // sender of the request unsigned long sender; } request_t; @@ -53,21 +53,21 @@ typedef struct response { // payload of the response, void *payload; - //length in bytes of the payload + // length in bytes of the payload int payload_len; - //receiver of the response + // receiver of the response unsigned long reciever; } response_t; int -check_url_start(const char* url, int url_len, const char * leading_str); +check_url_start(const char *url, int url_len, const char *leading_str); bool -match_url(char * pattern, char * matched); +match_url(char *pattern, char *matched); char * -find_key_value(char * buffer, int buffer_len, char * key, char * value, +find_key_value(char *buffer, int buffer_len, char *key, char *value, int value_len, char delimiter); request_t * @@ -77,10 +77,10 @@ void request_cleaner(request_t *request); response_t * -clone_response(response_t * response); +clone_response(response_t *response); void -response_cleaner(response_t * response); +response_cleaner(response_t *response); /** * @brief Set fields of response. @@ -96,8 +96,8 @@ response_cleaner(response_t * response); * @warning the response pointer MUST NOT be NULL */ response_t * -set_response(response_t * response, int status, int fmt, - const char *payload, int payload_len); +set_response(response_t *response, int status, int fmt, const char *payload, + int payload_len); /** * @brief Make a response for a request. @@ -110,7 +110,7 @@ set_response(response_t * response, int status, int fmt, * @warning the request and response pointers MUST NOT be NULL */ response_t * -make_response_for_request(request_t * request, response_t * response); +make_response_for_request(request_t *request, response_t *response); /** * @brief Initialize a request. @@ -127,28 +127,27 @@ make_response_for_request(request_t * request, response_t * response); * @warning the request pointer MUST NOT be NULL */ request_t * -init_request(request_t * request, char *url, int action, int fmt, - void *payload, int payload_len); +init_request(request_t *request, char *url, int action, int fmt, void *payload, + int payload_len); char * -pack_request(request_t *request, int * size); +pack_request(request_t *request, int *size); request_t * -unpack_request(char * packet, int size, request_t * request); +unpack_request(char *packet, int size, request_t *request); char * -pack_response(response_t *response, int * size); +pack_response(response_t *response, int *size); response_t * -unpack_response(char * packet, int size, response_t * response); +unpack_response(char *packet, int size, response_t *response); void -free_req_resp_packet(char * packet); +free_req_resp_packet(char *packet); char * wa_strdup(const char *str); - #ifdef __cplusplus } #endif diff --git a/core/app-framework/app-native-shared/bi-inc/wgl_shared_utils.h b/core/app-framework/app-native-shared/bi-inc/wgl_shared_utils.h index 7a391a193..86d864e41 100644 --- a/core/app-framework/app-native-shared/bi-inc/wgl_shared_utils.h +++ b/core/app-framework/app-native-shared/bi-inc/wgl_shared_utils.h @@ -8,14 +8,12 @@ #include "bh_platform.h" - #ifdef __cplusplus extern "C" { #endif #include - /* Object native function IDs */ enum { OBJ_FUNC_ID_DEL, diff --git a/core/app-framework/app-native-shared/native_interface.h b/core/app-framework/app-native-shared/native_interface.h index 76af44482..ce9f24780 100644 --- a/core/app-framework/app-native-shared/native_interface.h +++ b/core/app-framework/app-native-shared/native_interface.h @@ -10,6 +10,4 @@ implemented by both [app] and [native] worlds */ #include "bh_platform.h" - #endif /* end of _NATIVE_INTERFACE_H */ - diff --git a/core/app-framework/app-native-shared/restful_utils.c b/core/app-framework/app-native-shared/restful_utils.c index 74bd59a52..03e86a699 100644 --- a/core/app-framework/app-native-shared/restful_utils.c +++ b/core/app-framework/app-native-shared/restful_utils.c @@ -20,22 +20,27 @@ * 4. attr-containers of our own * 5. customized serialization for request/response * - * Now we choose the #5 mainly because we need to quickly get the URL for dispatching - * and sometimes we want to change the URL in the original packet. the request format: - * fixed part: version: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), url_len(2 bytes), payload_len(4bytes) - * dynamic part: url (bytes in url_len), payload + * Now we choose the #5 mainly because we need to quickly get the URL for + * dispatching and sometimes we want to change the URL in the original packet. + * the request format: fixed part: version: (1 byte), code (1 byte), fmt(2 + * byte), mid (4 bytes), sender_id(4 bytes), url_len(2 bytes), + * payload_len(4bytes) dynamic part: url (bytes in url_len), payload * * response format: - * fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 bytes), payload_len(4bytes) - * dynamic part: payload + * fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4 + * bytes), payload_len(4bytes) dynamic part: payload */ #define REQUES_PACKET_VER 1 #define REQUEST_PACKET_FIX_PART_LEN 18 #define REQUEST_PACKET_URL_OFFSET REQUEST_PACKET_FIX_PART_LEN -#define REQUEST_PACKET_URL_LEN *((uint16*)((char*) buffer + 12)) /* to ensure little endian */ -#define REQUEST_PACKET_PAYLOAD_LEN *((uint32*)((char*) buffer + 14)) /* to ensure little endian */ -#define REQUEST_PACKET_URL(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET) -#define REQUEST_PACKET_PAYLOAD(buffer) ((char*) buffer + REQUEST_PACKET_URL_OFFSET + REQUEST_PACKET_URL_LEN(buffer)) +#define REQUEST_PACKET_URL_LEN \ + *((uint16 *)((char *)buffer + 12)) /* to ensure little endian */ +#define REQUEST_PACKET_PAYLOAD_LEN \ + *((uint32 *)((char *)buffer + 14)) /* to ensure little endian */ +#define REQUEST_PACKET_URL(buffer) ((char *)buffer + REQUEST_PACKET_URL_OFFSET) +#define REQUEST_PACKET_PAYLOAD(buffer) \ + ((char *)buffer + REQUEST_PACKET_URL_OFFSET \ + + REQUEST_PACKET_URL_LEN(buffer)) #define RESPONSE_PACKET_FIX_PART_LEN 16 @@ -48,17 +53,17 @@ pack_request(request_t *request, int *size) uint32 u32; char *packet; - if ((packet = (char*) WA_MALLOC(len)) == NULL) + if ((packet = (char *)WA_MALLOC(len)) == NULL) return NULL; /* TODO: ensure little endian for words and dwords */ *packet = REQUES_PACKET_VER; - *((uint8*) (packet + 1)) = request->action; + *((uint8 *)(packet + 1)) = request->action; u16 = htons(request->fmt); memcpy(packet + 2, &u16, 2); - u32 = htonl(request->mid); + u32 = htonl(request->mid); memcpy(packet + 4, &u32, 4); u32 = htonl(request->sender); @@ -72,7 +77,7 @@ pack_request(request_t *request, int *size) strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url); memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload, - request->payload_len); + request->payload_len); *size = len; return packet; @@ -111,7 +116,7 @@ unpack_request(char *packet, int size, request_t *request) return NULL; } - request->action = *((uint8*) (packet + 1)); + request->action = *((uint8 *)(packet + 1)); memcpy(&u16, packet + 2, 2); request->fmt = ntohs(u16); @@ -141,12 +146,12 @@ pack_response(response_t *response, int *size) uint32 u32; char *packet; - if ((packet = (char*) WA_MALLOC(len)) == NULL) + if ((packet = (char *)WA_MALLOC(len)) == NULL) return NULL; /* TODO: ensure little endian for words and dwords */ *packet = REQUES_PACKET_VER; - *((uint8*) (packet + 1)) = response->status; + *((uint8 *)(packet + 1)) = response->status; u16 = htons(response->fmt); memcpy(packet + 2, &u16, 2); @@ -161,7 +166,7 @@ pack_response(response_t *response, int *size) memcpy(packet + 12, &u32, 4); memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload, - response->payload_len); + response->payload_len); *size = len; return packet; @@ -184,7 +189,7 @@ unpack_response(char *packet, int size, response_t *response) if (size != (RESPONSE_PACKET_FIX_PART_LEN + payload_len)) return NULL; - response->status = *((uint8*) (packet + 1)); + response->status = *((uint8 *)(packet + 1)); memcpy(&u16, packet + 2, 2); response->fmt = ntohs(u16); @@ -208,7 +213,7 @@ request_t * clone_request(request_t *request) { /* deep clone */ - request_t *req = (request_t *) WA_MALLOC(sizeof(request_t)); + request_t *req = (request_t *)WA_MALLOC(sizeof(request_t)); if (req == NULL) return NULL; @@ -278,7 +283,7 @@ clone_response(response_t *response) clone->reciever = response->reciever; clone->payload_len = response->payload_len; if (clone->payload_len) { - clone->payload = (char *) WA_MALLOC(response->payload_len); + clone->payload = (char *)WA_MALLOC(response->payload_len); if (!clone->payload) goto fail; memcpy(clone->payload, response->payload, response->payload_len); @@ -296,8 +301,8 @@ fail: } response_t * -set_response(response_t *response, int status, int fmt, - const char *payload, int payload_len) +set_response(response_t *response, int status, int fmt, const char *payload, + int payload_len) { response->payload = (void *)payload; response->payload_len = payload_len; @@ -307,8 +312,7 @@ set_response(response_t *response, int status, int fmt, } response_t * -make_response_for_request(request_t *request, - response_t *response) +make_response_for_request(request_t *request, response_t *response) { response->mid = request->mid; response->reciever = request->sender; @@ -319,8 +323,8 @@ make_response_for_request(request_t *request, static unsigned int mid = 0; request_t * -init_request(request_t *request, char *url, int action, int fmt, - void *payload, int payload_len) +init_request(request_t *request, char *url, int action, int fmt, void *payload, + int payload_len) { request->url = url; request->action = action; @@ -334,10 +338,10 @@ init_request(request_t *request, char *url, int action, int fmt, /* check if the "url" is starting with "leading_str" - return: 0 - not match; >0 - the offset of matched url, include any "/" at the end - notes: - 1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail "/ab" and "/abcd". - leading_str "/abc/" can pass "/abc" + return: 0 - not match; >0 - the offset of matched url, include any "/" at the + end notes: + 1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail + "/ab" and "/abcd". leading_str "/abc/" can pass "/abc" 2. it omit the '/' at the first char 3. it ensure the leading_str "/abc" can pass "/abc?cde */ @@ -425,7 +429,6 @@ match_url(char *pattern, char *matched) return true; return false; - } else if (pattern[len - 1] == '*') { if (pattern[len - 2] == '/') { @@ -468,9 +471,9 @@ find_key_value(char *buffer, int buffer_len, char *key, char *value, if (0 == strncmp(p, key, key_len) && p[key_len] == '=') { p += (key_len + 1); remaining -= (key_len + 1); - char * v = value; + char *v = value; memset(value, 0, value_len); - value_len--; /* ensure last char is 0 */ + value_len--; /* ensure last char is 0 */ while (*p != delimiter && remaining > 0 && value_len > 0) { *v++ = *p++; remaining--; diff --git a/core/app-framework/app_ext_lib_export.c b/core/app-framework/app_ext_lib_export.c index 6e66fdfb6..532491b43 100644 --- a/core/app-framework/app_ext_lib_export.c +++ b/core/app-framework/app_ext_lib_export.c @@ -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); } - diff --git a/core/app-framework/base/app/bh_platform.c b/core/app-framework/base/app/bh_platform.c index 0dbf04752..1848d0792 100644 --- a/core/app-framework/base/app/bh_platform.c +++ b/core/app-framework/base/app/bh_platform.c @@ -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))) diff --git a/core/app-framework/base/app/bh_platform.h b/core/app-framework/base/app/bh_platform.h old mode 100755 new mode 100644 index 70760e9c1..8e10dcb64 --- a/core/app-framework/base/app/bh_platform.h +++ b/core/app-framework/base/app/bh_platform.h @@ -16,7 +16,7 @@ typedef unsigned int uint32; typedef int int32; #ifndef NULL -# define NULL ((void*) 0) +#define NULL ((void *)0) #endif #ifndef __cplusplus @@ -35,28 +35,31 @@ typedef int int32; #define WA_FREE free #endif - -uint32 htonl(uint32 value); -uint32 ntohl(uint32 value); -uint16 htons(uint16 value); -uint16 ntohs(uint16 value); - +uint32 +htonl(uint32 value); +uint32 +ntohl(uint32 value); +uint16 +htons(uint16 value); +uint16 +ntohs(uint16 value); // We are not worried for the WASM world since the sandbox will catch it. -#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len) +#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len) #ifdef NDEBUG #define bh_assert(v) (void)0 #else -#define bh_assert(v) do { \ - if (!(v)) { \ - int _count; \ - printf("ASSERTION FAILED: %s, at %s, line %d",\ - #v, __FILE__, __LINE__); \ - _count = printf("\n"); \ - printf("%d\n", _count / (_count - 1)); \ - } \ - } while (0) +#define bh_assert(v) \ + do { \ + if (!(v)) { \ + int _count; \ + printf("ASSERTION FAILED: %s, at %s, line %d", #v, __FILE__, \ + __LINE__); \ + _count = printf("\n"); \ + printf("%d\n", _count / (_count - 1)); \ + } \ + } while (0) #endif #endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */ diff --git a/core/app-framework/base/app/req_resp_api.h b/core/app-framework/base/app/req_resp_api.h index cbddab5f7..575c35732 100644 --- a/core/app-framework/base/app/req_resp_api.h +++ b/core/app-framework/base/app/req_resp_api.h @@ -29,4 +29,3 @@ wasm_sub_event(const char *url); #endif #endif /* end of _REQ_RESP_API_H_ */ - diff --git a/core/app-framework/base/app/request.c b/core/app-framework/base/app/request.c index 13fa22a8f..3ba44fbc7 100644 --- a/core/app-framework/base/app/request.c +++ b/core/app-framework/base/app/request.c @@ -13,13 +13,11 @@ #define TRANSACTION_TIMEOUT_MS 5000 -typedef enum { - Reg_Event, Reg_Request -} reg_type_t; +typedef enum { Reg_Event, Reg_Request } reg_type_t; typedef struct _res_register { struct _res_register *next; - const char * url; + const char *url; reg_type_t reg_type; void (*request_handler)(request_t *); } res_register_t; @@ -32,13 +30,14 @@ typedef struct transaction { void *user_data; } transaction_t; -static res_register_t * g_resources = NULL; +static res_register_t *g_resources = NULL; static transaction_t *g_transactions = NULL; static user_timer_t g_trans_timer = NULL; -static transaction_t *transaction_find(int mid) +static transaction_t * +transaction_find(int mid) { transaction_t *t = g_transactions; @@ -55,7 +54,8 @@ static transaction_t *transaction_find(int mid) * new transaction is added to the tail of the list, so the list * is sorted by expiry time naturally. */ -static void transaction_add(transaction_t *trans) +static void +transaction_add(transaction_t *trans) { transaction_t *t; @@ -73,7 +73,8 @@ static void transaction_add(transaction_t *trans) } } -static void transaction_remove(transaction_t *trans) +static void +transaction_remove(transaction_t *trans) { transaction_t *prev = NULL, *current = g_transactions; @@ -93,15 +94,17 @@ static void transaction_remove(transaction_t *trans) } } -static bool is_event_type(request_t * req) +static bool +is_event_type(request_t *req) { return req->action == COAP_EVENT; } -static bool register_url_handler(const char *url, - request_handler_f request_handler, reg_type_t reg_type) +static bool +register_url_handler(const char *url, request_handler_f request_handler, + reg_type_t reg_type) { - res_register_t * r = g_resources; + res_register_t *r = g_resources; while (r) { if (reg_type == r->reg_type && strcmp(r->url, url) == 0) { @@ -111,7 +114,7 @@ static bool register_url_handler(const char *url, r = r->next; } - r = (res_register_t *) malloc(sizeof(res_register_t)); + r = (res_register_t *)malloc(sizeof(res_register_t)); if (r == NULL) return false; @@ -137,13 +140,15 @@ static bool register_url_handler(const char *url, return true; } -bool api_register_resource_handler(const char *url, - request_handler_f request_handler) +bool +api_register_resource_handler(const char *url, + request_handler_f request_handler) { return register_url_handler(url, request_handler, Reg_Request); } -static void transaction_timeout_handler(user_timer_t timer) +static void +transaction_timeout_handler(user_timer_t timer) { transaction_t *cur, *expired = NULL; unsigned int elpased_ms, now = wasm_get_sys_tick_ms(); @@ -164,7 +169,8 @@ static void transaction_timeout_handler(user_timer_t timer) cur->next = expired; expired = cur; cur = g_transactions; - } else { + } + else { break; } } @@ -186,27 +192,30 @@ static void transaction_timeout_handler(user_timer_t timer) unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms(); if (now < g_transactions->time) { elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1; - } else { + } + else { elpased_ms = now - g_transactions->time; } ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms; api_timer_restart(g_trans_timer, ms_to_expiry); - } else { + } + else { api_timer_cancel(g_trans_timer); g_trans_timer = NULL; } } -void api_send_request(request_t * request, response_handler_f response_handler, - void * user_data) +void +api_send_request(request_t *request, response_handler_f response_handler, + void *user_data) { int size; char *buffer; transaction_t *trans; - if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) { + if ((trans = (transaction_t *)malloc(sizeof(transaction_t))) == NULL) { printf( - "send request: allocate memory for request transaction failed!\n"); + "send request: allocate memory for request transaction failed!\n"); return; } @@ -228,9 +237,8 @@ void api_send_request(request_t * request, response_handler_f response_handler, if (trans == g_transactions) { /* assert(g_trans_timer == NULL); */ if (g_trans_timer == NULL) { - g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, - false, - true, transaction_timeout_handler); + g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS, false, + true, transaction_timeout_handler); } } @@ -241,11 +249,13 @@ void api_send_request(request_t * request, response_handler_f response_handler, /* * - * APIs for the native layers to callback for request/response arrived to this app + * APIs for the native layers to callback for request/response arrived to this + * app * */ -void on_response(char * buffer, int size) +void +on_response(char *buffer, int size) { response_t response[1]; transaction_t *trans; @@ -262,7 +272,8 @@ void on_response(char * buffer, int size) /* * When the 1st transaction get response: - * 1. If the 2nd trans exist, restart the timer according to its expiry time; + * 1. If the 2nd trans exist, restart the timer according to its expiry + * time; * 2. Otherwise, stop the timer since there is no more transactions; */ if (trans == g_transactions) { @@ -270,12 +281,14 @@ void on_response(char * buffer, int size) unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms(); if (now < trans->next->time) { elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1; - } else { + } + else { elpased_ms = now - trans->next->time; } ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms; api_timer_restart(g_trans_timer, ms_to_expiry); - } else { + } + else { api_timer_cancel(g_trans_timer); g_trans_timer = NULL; } @@ -285,7 +298,8 @@ void on_response(char * buffer, int size) transaction_remove(trans); } -void on_request(char *buffer, int size) +void +on_request(char *buffer, int size) { request_t request[1]; bool is_event; @@ -300,9 +314,9 @@ void on_request(char *buffer, int size) while (r) { if ((is_event && r->reg_type == Reg_Event) - || (!is_event && r->reg_type == Reg_Request)) { + || (!is_event && r->reg_type == Reg_Request)) { if (check_url_start(request->url, strlen(request->url), r->url) - > 0) { + > 0) { r->request_handler(request); return; } @@ -314,10 +328,11 @@ void on_request(char *buffer, int size) printf("on_request: exit. no service handler\n"); } -void api_response_send(response_t *response) +void +api_response_send(response_t *response) { int size; - char * buffer = pack_response(response, &size); + char *buffer = pack_response(response, &size); if (buffer == NULL) return; @@ -327,12 +342,13 @@ void api_response_send(response_t *response) /// event api -bool api_publish_event(const char *url, int fmt, void *payload, int payload_len) +bool +api_publish_event(const char *url, int fmt, void *payload, int payload_len) { int size; request_t request[1]; init_request(request, (char *)url, COAP_EVENT, fmt, payload, payload_len); - char * buffer = pack_request(request, &size); + char *buffer = pack_request(request, &size); if (buffer == NULL) return false; wasm_post_request(buffer, size); @@ -342,8 +358,8 @@ bool api_publish_event(const char *url, int fmt, void *payload, int payload_len) return true; } -bool api_subscribe_event(const char * url, request_handler_f handler) +bool +api_subscribe_event(const char *url, request_handler_f handler) { return register_url_handler(url, handler, Reg_Event); } - diff --git a/core/app-framework/base/app/timer.c b/core/app-framework/base/app/timer.c index 41bd59f59..692626ca3 100644 --- a/core/app-framework/base/app/timer.c +++ b/core/app-framework/base/app/timer.c @@ -16,22 +16,23 @@ #endif struct user_timer { - struct user_timer * next; + struct user_timer *next; int timer_id; void (*user_timer_callback)(user_timer_t); }; -struct user_timer * g_timers = NULL; +struct user_timer *g_timers = NULL; -user_timer_t api_timer_create(int interval, bool is_period, bool auto_start, - on_user_timer_update_f on_timer_update) +user_timer_t +api_timer_create(int interval, bool is_period, bool auto_start, + on_user_timer_update_f on_timer_update) { int timer_id = wasm_create_timer(interval, is_period, auto_start); - //TODO - struct user_timer * timer = (struct user_timer *) malloc( - sizeof(struct user_timer)); + // TODO + struct user_timer *timer = + (struct user_timer *)malloc(sizeof(struct user_timer)); if (timer == NULL) { // TODO: remove the timer_id printf("### api_timer_create malloc faild!!! \n"); @@ -52,7 +53,8 @@ user_timer_t api_timer_create(int interval, bool is_period, bool auto_start, return timer; } -void api_timer_cancel(user_timer_t timer) +void +api_timer_cancel(user_timer_t timer) { user_timer_t t = g_timers, prev = NULL; @@ -63,26 +65,30 @@ void api_timer_cancel(user_timer_t timer) if (prev == NULL) { g_timers = t->next; free(t); - } else { + } + else { prev->next = t->next; free(t); } return; - } else { + } + else { prev = t; t = t->next; } } } -void api_timer_restart(user_timer_t timer, int interval) +void +api_timer_restart(user_timer_t timer, int interval) { wasm_timer_restart(timer->timer_id, interval); } -void on_timer_callback(int timer_id) +void +on_timer_callback(int timer_id) { - struct user_timer * t = g_timers; + struct user_timer *t = g_timers; while (t) { if (t->timer_id == timer_id) { @@ -92,4 +98,3 @@ void on_timer_callback(int timer_id) t = t->next; } } - diff --git a/core/app-framework/base/app/timer_api.h b/core/app-framework/base/app/timer_api.h index 0881bab95..1fc7555ef 100644 --- a/core/app-framework/base/app/timer_api.h +++ b/core/app-framework/base/app/timer_api.h @@ -34,4 +34,3 @@ wasm_get_sys_tick_ms(void); #endif #endif /* end of _TIMER_API_H_ */ - diff --git a/core/app-framework/base/app/wa-inc/request.h b/core/app-framework/base/app/wa-inc/request.h index c209fae7c..25830f0a4 100644 --- a/core/app-framework/base/app/wa-inc/request.h +++ b/core/app-framework/base/app/wa-inc/request.h @@ -12,7 +12,6 @@ extern "C" { #endif - /* CoAP request method codes */ typedef enum { COAP_GET = 1, @@ -26,39 +25,40 @@ typedef enum { typedef enum { NO_ERROR = 0, - CREATED_2_01 = 65, /* CREATED */ - DELETED_2_02 = 66, /* DELETED */ - VALID_2_03 = 67, /* NOT_MODIFIED */ - CHANGED_2_04 = 68, /* CHANGED */ - CONTENT_2_05 = 69, /* OK */ + CREATED_2_01 = 65, /* CREATED */ + DELETED_2_02 = 66, /* DELETED */ + VALID_2_03 = 67, /* NOT_MODIFIED */ + CHANGED_2_04 = 68, /* CHANGED */ + CONTENT_2_05 = 69, /* OK */ CONTINUE_2_31 = 95, /* CONTINUE */ - BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ - UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ - BAD_OPTION_4_02 = 130, /* BAD_OPTION */ - FORBIDDEN_4_03 = 131, /* FORBIDDEN */ - NOT_FOUND_4_04 = 132, /* NOT_FOUND */ - METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ - NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ - PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ + BAD_REQUEST_4_00 = 128, /* BAD_REQUEST */ + UNAUTHORIZED_4_01 = 129, /* UNAUTHORIZED */ + BAD_OPTION_4_02 = 130, /* BAD_OPTION */ + FORBIDDEN_4_03 = 131, /* FORBIDDEN */ + NOT_FOUND_4_04 = 132, /* NOT_FOUND */ + METHOD_NOT_ALLOWED_4_05 = 133, /* METHOD_NOT_ALLOWED */ + NOT_ACCEPTABLE_4_06 = 134, /* NOT_ACCEPTABLE */ + PRECONDITION_FAILED_4_12 = 140, /* BAD_REQUEST */ REQUEST_ENTITY_TOO_LARGE_4_13 = 141, /* REQUEST_ENTITY_TOO_LARGE */ - UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ + UNSUPPORTED_MEDIA_TYPE_4_15 = 143, /* UNSUPPORTED_MEDIA_TYPE */ - INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ - NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ - BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ - SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ - GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ + INTERNAL_SERVER_ERROR_5_00 = 160, /* INTERNAL_SERVER_ERROR */ + NOT_IMPLEMENTED_5_01 = 161, /* NOT_IMPLEMENTED */ + BAD_GATEWAY_5_02 = 162, /* BAD_GATEWAY */ + SERVICE_UNAVAILABLE_5_03 = 163, /* SERVICE_UNAVAILABLE */ + GATEWAY_TIMEOUT_5_04 = 164, /* GATEWAY_TIMEOUT */ PROXYING_NOT_SUPPORTED_5_05 = 165, /* PROXYING_NOT_SUPPORTED */ /* Erbium errors */ - MEMORY_ALLOCATION_ERROR = 192, PACKET_SERIALIZATION_ERROR, + MEMORY_ALLOCATION_ERROR = 192, + PACKET_SERIALIZATION_ERROR, /* Erbium hooks */ - MANUAL_RESPONSE, PING_RESPONSE + MANUAL_RESPONSE, + PING_RESPONSE } coap_status_t; - /** * @typedef request_handler_f * @@ -87,7 +87,6 @@ typedef void (*request_handler_f)(request_t *request); */ typedef void (*response_handler_f)(response_t *response, void *user_data); - /* ***************** * Request APIs @@ -102,7 +101,8 @@ typedef void (*response_handler_f)(response_t *response, void *user_data); * * @return true if success, false otherwise */ -bool api_register_resource_handler(const char *url, request_handler_f handler); +bool +api_register_resource_handler(const char *url, request_handler_f handler); /** * @brief Send request asynchronously. @@ -111,8 +111,9 @@ bool api_register_resource_handler(const char *url, request_handler_f handler); * @param response_handler callback function to handle the response * @param user_data user data */ -void api_send_request(request_t * request, response_handler_f response_handler, - void * user_data); +void +api_send_request(request_t *request, response_handler_f response_handler, + void *user_data); /** * @brief Send response. @@ -130,8 +131,8 @@ void api_send_request(request_t * request, response_handler_f response_handler, * } * @endcode */ -void api_response_send(response_t *response); - +void +api_response_send(response_t *response); /* ***************** @@ -149,9 +150,8 @@ void api_response_send(response_t *response); * * @return true if success, false otherwise */ -bool api_publish_event(const char *url, int fmt, void *payload, - int payload_len); - +bool +api_publish_event(const char *url, int fmt, void *payload, int payload_len); /** * @brief Subscribe an event. @@ -161,7 +161,8 @@ bool api_publish_event(const char *url, int fmt, void *payload, * * @return true if success, false otherwise */ -bool api_subscribe_event(const char * url, request_handler_f handler); +bool +api_subscribe_event(const char *url, request_handler_f handler); #ifdef __cplusplus } diff --git a/core/app-framework/base/app/wa-inc/timer_wasm_app.h b/core/app-framework/base/app/wa-inc/timer_wasm_app.h index 17e59bc03..cf158a365 100644 --- a/core/app-framework/base/app/wa-inc/timer_wasm_app.h +++ b/core/app-framework/base/app/wa-inc/timer_wasm_app.h @@ -14,7 +14,7 @@ extern "C" { /* board producer define user_timer */ struct user_timer; -typedef struct user_timer * user_timer_t; +typedef struct user_timer *user_timer_t; /** * @typedef on_user_timer_update_f @@ -43,15 +43,17 @@ typedef void (*on_user_timer_update_f)(user_timer_t timer); * * @return the timer created if success, NULL otherwise */ -user_timer_t api_timer_create(int interval, bool is_period, bool auto_start, - on_user_timer_update_f on_timer_update); +user_timer_t +api_timer_create(int interval, bool is_period, bool auto_start, + on_user_timer_update_f on_timer_update); /** * @brief Cancel timer. * * @param timer the timer to cancel */ -void api_timer_cancel(user_timer_t timer); +void +api_timer_cancel(user_timer_t timer); /** * @brief Restart timer. @@ -59,7 +61,8 @@ void api_timer_cancel(user_timer_t timer); * @param timer the timer to cancel * @param interval the timer interval */ -void api_timer_restart(user_timer_t timer, int interval); +void +api_timer_restart(user_timer_t timer, int interval); #ifdef __cplusplus } diff --git a/core/app-framework/base/app/wasm_app.h b/core/app-framework/base/app/wasm_app.h index a22e77ed4..e7be8a4c1 100644 --- a/core/app-framework/base/app/wasm_app.h +++ b/core/app-framework/base/app/wasm_app.h @@ -13,7 +13,6 @@ extern "C" { #endif - #ifdef __cplusplus } #endif diff --git a/core/app-framework/base/native/base_lib_export.c b/core/app-framework/base/native/base_lib_export.c index 0b716fc5e..19ac7185c 100644 --- a/core/app-framework/base/native/base_lib_export.c +++ b/core/app-framework/base/native/base_lib_export.c @@ -10,16 +10,15 @@ #include "req_resp_native_api.h" #include "timer_native_api.h" - static NativeSymbol extended_native_symbol_defs[] = { - /* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to - add functions to register. */ - #include "base_lib.inl" +/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to + add functions to register. */ +#include "base_lib.inl" }; -uint32 get_base_lib_export_apis(NativeSymbol **p_base_lib_apis) +uint32 +get_base_lib_export_apis(NativeSymbol **p_base_lib_apis) { *p_base_lib_apis = extended_native_symbol_defs; return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol); } - diff --git a/core/app-framework/base/native/req_resp_native_api.h b/core/app-framework/base/native/req_resp_native_api.h index 6fe0581b6..3e5938772 100644 --- a/core/app-framework/base/native/req_resp_native_api.h +++ b/core/app-framework/base/native/req_resp_native_api.h @@ -27,4 +27,3 @@ wasm_sub_event(wasm_exec_env_t exec_env, char *url); #endif #endif /* end of _REQ_RESP_API_H_ */ - diff --git a/core/app-framework/base/native/request_response.c b/core/app-framework/base/native/request_response.c index 4088c3885..674ba5e9d 100644 --- a/core/app-framework/base/native/request_response.c +++ b/core/app-framework/base/native/request_response.c @@ -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); } } - diff --git a/core/app-framework/base/native/runtime_lib.h b/core/app-framework/base/native/runtime_lib.h index 8fb9dbb34..ec4d84cb7 100644 --- a/core/app-framework/base/native/runtime_lib.h +++ b/core/app-framework/base/native/runtime_lib.h @@ -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_ */ diff --git a/core/app-framework/base/native/timer_native_api.h b/core/app-framework/base/native/timer_native_api.h index 4d1f58786..138e7c60d 100644 --- a/core/app-framework/base/native/timer_native_api.h +++ b/core/app-framework/base/native/timer_native_api.h @@ -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_ */ - diff --git a/core/app-framework/base/native/timer_wrapper.c b/core/app-framework/base/native/timer_wrapper.c index 07aca8b41..7ad350cf2 100644 --- a/core/app-framework/base/native/timer_wrapper.c +++ b/core/app-framework/base/native/timer_wrapper.c @@ -22,7 +22,7 @@ static korp_mutex g_timer_ctx_list_mutex; void wasm_timer_callback(timer_id_t id, unsigned int mod_id) { - module_data* module = module_data_list_lookup_id(mod_id); + module_data *module = module_data_list_lookup_id(mod_id); if (module == NULL) return; @@ -40,7 +40,8 @@ wasm_timer_callback(timer_id_t id, unsigned int mod_id) * the module from module id. It is for avoiding that situation. */ -void * thread_modulers_timer_check(void * arg) +void * +thread_modulers_timer_check(void *arg) { uint32 ms_to_expiry; uint64 us_to_wait; @@ -48,8 +49,8 @@ void * thread_modulers_timer_check(void * arg) while (timer_thread_run) { ms_to_expiry = (uint32)-1; os_mutex_lock(&g_timer_ctx_list_mutex); - timer_ctx_node_t* elem = (timer_ctx_node_t*) - bh_list_first_elem(&g_timer_ctx_list); + timer_ctx_node_t *elem = + (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list); while (elem) { uint32 next = check_app_timers(elem->timer_ctx); if (next != (uint32)-1) { @@ -57,7 +58,7 @@ void * thread_modulers_timer_check(void * arg) ms_to_expiry = next; } - elem = (timer_ctx_node_t*) bh_list_elem_next(elem); + elem = (timer_ctx_node_t *)bh_list_elem_next(elem); } os_mutex_unlock(&g_timer_ctx_list_mutex); @@ -93,8 +94,8 @@ init_wasm_timer() would recursive lock the mutex */ os_recursive_mutex_init(&g_timer_ctx_list_mutex); - os_thread_create(&tm_tid, thread_modulers_timer_check, - NULL, BH_APPLET_PRESERVED_STACK_SIZE); + os_thread_create(&tm_tid, thread_modulers_timer_check, NULL, + BH_APPLET_PRESERVED_STACK_SIZE); } void @@ -106,15 +107,15 @@ exit_wasm_timer() timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num) { - timer_ctx_t ctx = create_timer_ctx(wasm_timer_callback, - wakeup_modules_timer_thread, - prealloc_num, module_id); + timer_ctx_t ctx = + create_timer_ctx(wasm_timer_callback, wakeup_modules_timer_thread, + prealloc_num, module_id); if (ctx == NULL) return NULL; - timer_ctx_node_t * node = (timer_ctx_node_t*) - wasm_runtime_malloc(sizeof(timer_ctx_node_t)); + timer_ctx_node_t *node = + (timer_ctx_node_t *)wasm_runtime_malloc(sizeof(timer_ctx_node_t)); if (node == NULL) { destroy_timer_ctx(ctx); return NULL; @@ -132,11 +133,10 @@ create_wasm_timer_ctx(unsigned int module_id, int prealloc_num) void destroy_module_timer_ctx(unsigned int module_id) { - timer_ctx_node_t* elem; + timer_ctx_node_t *elem; os_mutex_lock(&g_timer_ctx_list_mutex); - elem = (timer_ctx_node_t*) - bh_list_first_elem(&g_timer_ctx_list); + elem = (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list); while (elem) { if (timer_ctx_get_owner(elem->timer_ctx) == module_id) { bh_list_remove(&g_timer_ctx_list, elem); @@ -145,7 +145,7 @@ destroy_module_timer_ctx(unsigned int module_id) break; } - elem = (timer_ctx_node_t*) bh_list_elem_next(elem); + elem = (timer_ctx_node_t *)bh_list_elem_next(elem); } os_mutex_unlock(&g_timer_ctx_list_mutex); } @@ -153,16 +153,15 @@ destroy_module_timer_ctx(unsigned int module_id) timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst) { - module_data * m = app_manager_get_module_data(Module_WASM_App, - module_inst); + module_data *m = app_manager_get_module_data(Module_WASM_App, module_inst); if (m == NULL) return NULL; return m->timer_ctx; } timer_id_t -wasm_create_timer(wasm_exec_env_t exec_env, - int interval, bool is_period, bool auto_start) +wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period, + bool auto_start) { wasm_module_inst_t module_inst = get_module_inst(exec_env); timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); @@ -189,8 +188,7 @@ wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id) } void -wasm_timer_restart(wasm_exec_env_t exec_env, - timer_id_t timer_id, int interval) +wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval) { wasm_module_inst_t module_inst = get_module_inst(exec_env); timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst); @@ -203,4 +201,3 @@ wasm_get_sys_tick_ms(wasm_exec_env_t exec_env) { return (uint32)bh_get_tick_ms(); } - diff --git a/core/app-framework/connection/app/connection.c b/core/app-framework/connection/app/connection.c index 775ee16f1..b5b2bfc54 100644 --- a/core/app-framework/connection/app/connection.c +++ b/core/app-framework/connection/app/connection.c @@ -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; } } - diff --git a/core/app-framework/connection/app/connection_api.h b/core/app-framework/connection/app/connection_api.h index 5697c8361..22bd5a182 100644 --- a/core/app-framework/connection/app/connection_api.h +++ b/core/app-framework/connection/app/connection_api.h @@ -28,5 +28,4 @@ wasm_config_connection(uint32 handle, const char *cfg_buf, uint32 cfg_buf_len); } #endif - #endif /* end of CONNECTION_API_H_ */ diff --git a/core/app-framework/connection/app/wa-inc/connection.h b/core/app-framework/connection/app/wa-inc/connection.h index 675befa59..823eaec74 100644 --- a/core/app-framework/connection/app/wa-inc/connection.h +++ b/core/app-framework/connection/app/wa-inc/connection.h @@ -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 } diff --git a/core/app-framework/connection/native/connection_lib.h b/core/app-framework/connection/native/connection_lib.h index 4a69f555f..3e182cbb8 100644 --- a/core/app-framework/connection/native/connection_lib.h +++ b/core/app-framework/connection/native/connection_lib.h @@ -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_ */ diff --git a/core/app-framework/connection/native/connection_native_api.h b/core/app-framework/connection/native/connection_native_api.h index 79e911d19..42a2508f1 100644 --- a/core/app-framework/connection/native/connection_native_api.h +++ b/core/app-framework/connection/native/connection_native_api.h @@ -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_ */ diff --git a/core/app-framework/connection/native/connection_wrapper.c b/core/app-framework/connection/native/connection_wrapper.c index 742a45b28..7c20b51d0 100644 --- a/core/app-framework/connection/native/connection_wrapper.c +++ b/core/app-framework/connection/native/connection_wrapper.c @@ -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; diff --git a/core/app-framework/connection/native/linux/conn_tcp.c b/core/app-framework/connection/native/linux/conn_tcp.c index e676e53a0..054eb59fd 100644 --- a/core/app-framework/connection/native/linux/conn_tcp.c +++ b/core/app-framework/connection/native/linux/conn_tcp.c @@ -11,7 +11,8 @@ #include #include -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); } diff --git a/core/app-framework/connection/native/linux/conn_tcp.h b/core/app-framework/connection/native/linux/conn_tcp.h index c1b560e32..c4d5cc86a 100644 --- a/core/app-framework/connection/native/linux/conn_tcp.h +++ b/core/app-framework/connection/native/linux/conn_tcp.h @@ -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 diff --git a/core/app-framework/connection/native/linux/conn_uart.c b/core/app-framework/connection/native/linux/conn_uart.c index 1f5c4e767..0bcdc93f7 100644 --- a/core/app-framework/connection/native/linux/conn_uart.c +++ b/core/app-framework/connection/native/linux/conn_uart.c @@ -9,7 +9,8 @@ #include #include -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); } diff --git a/core/app-framework/connection/native/linux/conn_uart.h b/core/app-framework/connection/native/linux/conn_uart.h index 9d40fb004..443167026 100644 --- a/core/app-framework/connection/native/linux/conn_uart.h +++ b/core/app-framework/connection/native/linux/conn_uart.h @@ -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 diff --git a/core/app-framework/connection/native/linux/conn_udp.c b/core/app-framework/connection/native/linux/conn_udp.c index 9d370475a..61652b14d 100644 --- a/core/app-framework/connection/native/linux/conn_udp.c +++ b/core/app-framework/connection/native/linux/conn_udp.c @@ -11,7 +11,8 @@ #include #include -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); } diff --git a/core/app-framework/connection/native/linux/conn_udp.h b/core/app-framework/connection/native/linux/conn_udp.h index 37b9db79a..377c26eb1 100644 --- a/core/app-framework/connection/native/linux/conn_udp.h +++ b/core/app-framework/connection/native/linux/conn_udp.h @@ -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 diff --git a/core/app-framework/connection/native/linux/connection_mgr.c b/core/app-framework/connection/native/linux/connection_mgr.c index df0fc8f07..001446206 100644 --- a/core/app-framework/connection/native/linux/connection_mgr.c +++ b/core/app-framework/connection/native/linux/connection_mgr.c @@ -76,23 +76,30 @@ static struct epoll_event epoll_events[MAX_EVENTS]; /* Buffer to receive data */ static char io_buf[IO_BUF_SIZE]; -static uint32 _conn_open(wasm_module_inst_t module_inst, - const char *name, attr_container_t *args); -static void _conn_close(uint32 handle); -static int _conn_send(uint32 handle, const char *data, int len); -static bool _conn_config(uint32 handle, attr_container_t *cfg); +static uint32 +_conn_open(wasm_module_inst_t module_inst, const char *name, + attr_container_t *args); +static void +_conn_close(uint32 handle); +static int +_conn_send(uint32 handle, const char *data, int len); +static bool +_conn_config(uint32 handle, attr_container_t *cfg); +/* clang-format off */ /* * Platform implementation of connection library */ connection_interface_t connection_impl = { - ._open = _conn_open, - ._close = _conn_close, - ._send = _conn_send, - ._config = _conn_config + ._open = _conn_open, + ._close = _conn_close, + ._send = _conn_send, + ._config = _conn_config }; +/* clang-format on */ -static void add_connection(sys_connection_t *conn) +static void +add_connection(sys_connection_t *conn) { os_mutex_lock(&g_lock); @@ -104,20 +111,23 @@ static void add_connection(sys_connection_t *conn) if (g_connections) { conn->next = g_connections; g_connections = conn; - } else { + } + else { g_connections = conn; } os_mutex_unlock(&g_lock); } -#define FREE_CONNECTION(conn) do { \ - if (conn->arg) \ - wasm_runtime_free(conn->arg); \ - wasm_runtime_free(conn); \ -} while (0) +#define FREE_CONNECTION(conn) \ + do { \ + if (conn->arg) \ + wasm_runtime_free(conn->arg); \ + wasm_runtime_free(conn); \ + } while (0) -static int get_app_conns_num(uint32 module_id) +static int +get_app_conns_num(uint32 module_id) { sys_connection_t *conn; int num = 0; @@ -136,7 +146,8 @@ static int get_app_conns_num(uint32 module_id) return num; } -static sys_connection_t *find_connection(uint32 handle, bool remove_found) +static sys_connection_t * +find_connection(uint32 handle, bool remove_found) { sys_connection_t *conn, *prev = NULL; @@ -148,13 +159,15 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found) if (remove_found) { if (prev != NULL) { prev->next = conn->next; - } else { + } + else { g_connections = conn->next; } } os_mutex_unlock(&g_lock); return conn; - } else { + } + else { prev = conn; conn = conn->next; } @@ -165,7 +178,8 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found) return NULL; } -static void cleanup_connections(uint32 module_id) +static void +cleanup_connections(uint32 module_id) { sys_connection_t *conn, *prev = NULL; @@ -181,12 +195,14 @@ static void cleanup_connections(uint32 module_id) prev->next = conn->next; FREE_CONNECTION(conn); conn = prev->next; - } else { + } + else { g_connections = conn->next; FREE_CONNECTION(conn); conn = g_connections; } - } else { + } + else { prev = conn; conn = conn->next; } @@ -195,7 +211,8 @@ static void cleanup_connections(uint32 module_id) os_mutex_unlock(&g_lock); } -static conn_type_t get_conn_type(const char *name) +static conn_type_t +get_conn_type(const char *name) { if (strcmp(name, "TCP") == 0) return CONN_TYPE_TCP; @@ -208,14 +225,14 @@ static conn_type_t get_conn_type(const char *name) } /* --- connection lib function --- */ -static uint32 _conn_open(wasm_module_inst_t module_inst, - const char *name, attr_container_t *args) +static uint32 +_conn_open(wasm_module_inst_t module_inst, const char *name, + attr_container_t *args) { int fd; sys_connection_t *conn; struct epoll_event ev; - uint32 module_id = app_manager_get_module_id(Module_WASM_App, - module_inst); + uint32 module_id = app_manager_get_module_id(Module_WASM_App, module_inst); bh_assert(module_id != ID_NONE); if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP) @@ -237,8 +254,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst, uint16 port; /* Check and parse connection parameters */ - if (!attr_container_contain_key(args, "address") || - !attr_container_contain_key(args, "port")) + if (!attr_container_contain_key(args, "address") + || !attr_container_contain_key(args, "port")) goto fail; address = attr_container_get_as_string(args, "address"); @@ -247,8 +264,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst, /* Connect to TCP server */ if (!address || (fd = tcp_open(address, port)) == -1) goto fail; - - } else if (conn->type == CONN_TYPE_UDP) { + } + else if (conn->type == CONN_TYPE_UDP) { uint16 port; /* Check and parse connection parameters */ @@ -259,14 +276,14 @@ static uint32 _conn_open(wasm_module_inst_t module_inst, /* Bind port */ if ((fd = udp_open(port)) == -1) goto fail; - - } else if (conn->type == CONN_TYPE_UART) { + } + else if (conn->type == CONN_TYPE_UART) { char *device; int baud; /* Check and parse connection parameters */ - if (!attr_container_contain_key(args, "device") || - !attr_container_contain_key(args, "baudrate")) + if (!attr_container_contain_key(args, "device") + || !attr_container_contain_key(args, "baudrate")) goto fail; device = attr_container_get_as_string(args, "device"); baud = attr_container_get_as_int(args, "baudrate"); @@ -274,7 +291,8 @@ static uint32 _conn_open(wasm_module_inst_t module_inst, /* Open device */ if (!device || (fd = uart_open(device, baud)) == -1) goto fail; - } else { + } + else { goto fail; } @@ -299,7 +317,8 @@ fail: } /* --- connection lib function --- */ -static void _conn_close(uint32 handle) +static void +_conn_close(uint32 handle) { sys_connection_t *conn = find_connection(handle, true); @@ -311,7 +330,8 @@ static void _conn_close(uint32 handle) } /* --- connection lib function --- */ -static int _conn_send(uint32 handle, const char *data, int len) +static int +_conn_send(uint32 handle, const char *data, int len) { sys_connection_t *conn = find_connection(handle, false); @@ -333,7 +353,8 @@ static int _conn_send(uint32 handle, const char *data, int len) } /* --- connection lib function --- */ -static bool _conn_config(uint32 handle, attr_container_t *cfg) +static bool +_conn_config(uint32 handle, attr_container_t *cfg) { sys_connection_t *conn = find_connection(handle, false); @@ -346,8 +367,8 @@ static bool _conn_config(uint32 handle, attr_container_t *cfg) struct sockaddr_in *addr; /* Parse remote address/port */ - if (!attr_container_contain_key(cfg, "address") || - !attr_container_contain_key(cfg, "port")) + if (!attr_container_contain_key(cfg, "address") + || !attr_container_contain_key(cfg, "port")) return false; if (!(address = attr_container_get_as_string(cfg, "address"))) return false; @@ -365,7 +386,8 @@ static bool _conn_config(uint32 handle, attr_container_t *cfg) /* Set remote address as connection arg */ conn->arg = addr; - } else { + } + else { addr = (struct sockaddr_in *)conn->arg; addr->sin_addr.s_addr = inet_addr(address); addr->sin_port = htons(port); @@ -385,16 +407,16 @@ typedef struct connection_event { uint32 len; } connection_event_t; -static void connection_event_cleaner(connection_event_t *conn_event) +static void +connection_event_cleaner(connection_event_t *conn_event) { if (conn_event->data != NULL) wasm_runtime_free(conn_event->data); wasm_runtime_free(conn_event); } -static void post_msg_to_module(sys_connection_t *conn, - char *data, - uint32 len) +static void +post_msg_to_module(sys_connection_t *conn, char *data, uint32 len) { module_data *module = module_data_list_lookup_id(conn->module_id); char *data_copy = NULL; @@ -404,7 +426,8 @@ static void post_msg_to_module(sys_connection_t *conn, if (module == NULL) return; - conn_data_event = (connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event)); + conn_data_event = + (connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event)); if (conn_data_event == NULL) return; @@ -422,10 +445,8 @@ static void post_msg_to_module(sys_connection_t *conn, conn_data_event->data = data_copy; conn_data_event->len = len; - msg = bh_new_msg(CONNECTION_EVENT_WASM, - conn_data_event, - sizeof(*conn_data_event), - connection_event_cleaner); + msg = bh_new_msg(CONNECTION_EVENT_WASM, conn_data_event, + sizeof(*conn_data_event), connection_event_cleaner); if (!msg) { connection_event_cleaner(conn_data_event); return; @@ -434,7 +455,8 @@ static void post_msg_to_module(sys_connection_t *conn, bh_post_msg2(module->queue, msg); } -static void* polling_thread_routine (void *arg) +static void * +polling_thread_routine(void *arg) { while (polling_thread_run) { int i, n; @@ -445,8 +467,8 @@ static void* polling_thread_routine (void *arg) continue; for (i = 0; i < n; i++) { - sys_connection_t *conn - = (sys_connection_t *)epoll_events[i].data.ptr; + sys_connection_t *conn = + (sys_connection_t *)epoll_events[i].data.ptr; if (conn->type == CONN_TYPE_TCP) { int count = tcp_recv(conn->fd, io_buf, IO_BUF_SIZE); @@ -454,15 +476,18 @@ static void* polling_thread_routine (void *arg) /* Connection is closed by peer */ post_msg_to_module(conn, NULL, 0); _conn_close(conn->handle); - } else { + } + else { /* Data is received */ post_msg_to_module(conn, io_buf, count); } - } else if (conn->type == CONN_TYPE_UDP) { + } + else if (conn->type == CONN_TYPE_UDP) { int count = udp_recv(conn->fd, io_buf, IO_BUF_SIZE); if (count > 0) post_msg_to_module(conn, io_buf, count); - } else if (conn->type == CONN_TYPE_UART) { + } + else if (conn->type == CONN_TYPE_UART) { int count = uart_recv(conn->fd, io_buf, IO_BUF_SIZE); if (count > 0) post_msg_to_module(conn, io_buf, count); @@ -473,25 +498,26 @@ static void* polling_thread_routine (void *arg) return NULL; } -void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg) +void +app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg) { uint32 argv[3]; wasm_function_inst_t func_on_conn_data; bh_assert(CONNECTION_EVENT_WASM == bh_message_type(msg)); - wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; + wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; - connection_event_t *conn_event - = (connection_event_t *)bh_message_payload(msg); + connection_event_t *conn_event = + (connection_event_t *)bh_message_payload(msg); int32 data_offset; if (conn_event == NULL) return; - func_on_conn_data = wasm_runtime_lookup_function(inst, "_on_connection_data", - "(i32i32i32)"); + func_on_conn_data = wasm_runtime_lookup_function( + inst, "_on_connection_data", "(i32i32i32)"); if (!func_on_conn_data) - func_on_conn_data = wasm_runtime_lookup_function(inst, "on_connection_data", - "(i32i32i32)"); + func_on_conn_data = wasm_runtime_lookup_function( + inst, "on_connection_data", "(i32i32i32)"); if (!func_on_conn_data) { printf("Cannot find function on_connection_data\n"); return; @@ -506,34 +532,31 @@ void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg) 3, argv)) { const char *exception = wasm_runtime_get_exception(inst); bh_assert(exception); - printf(":Got exception running wasm code: %s\n", - exception); + printf(":Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); return; } - } else { - data_offset = wasm_runtime_module_dup_data(inst, - conn_event->data, + } + else { + data_offset = wasm_runtime_module_dup_data(inst, conn_event->data, conn_event->len); if (data_offset == 0) { const char *exception = wasm_runtime_get_exception(inst); if (exception) { - printf("Got exception running wasm code: %s\n", - exception); + printf("Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); } return; } argv[0] = conn_event->handle; - argv[1] = (uint32) data_offset; + argv[1] = (uint32)data_offset; argv[2] = conn_event->len; if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_on_conn_data, 3, argv)) { const char *exception = wasm_runtime_get_exception(inst); bh_assert(exception); - printf(":Got exception running wasm code: %s\n", - exception); + printf(":Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); wasm_runtime_module_free(inst, data_offset); return; @@ -542,7 +565,8 @@ void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg) } } -bool init_connection_framework() +bool +init_connection_framework() { korp_tid tid; @@ -560,14 +584,13 @@ bool init_connection_framework() } if (!wasm_register_msg_callback(CONNECTION_EVENT_WASM, - app_mgr_connection_event_callback)) { + app_mgr_connection_event_callback)) { goto fail; } - if (os_thread_create(&tid, - polling_thread_routine, - NULL, - BH_APPLET_PRESERVED_STACK_SIZE) != 0) { + if (os_thread_create(&tid, polling_thread_routine, NULL, + BH_APPLET_PRESERVED_STACK_SIZE) + != 0) { goto fail; } @@ -579,7 +602,8 @@ fail: return false; } -void exit_connection_framework() +void +exit_connection_framework() { polling_thread_run = false; } diff --git a/core/app-framework/connection/native/zephyr/connection_lib_impl.c b/core/app-framework/connection/native/zephyr/connection_lib_impl.c index 9c30edcaf..a812a71a2 100644 --- a/core/app-framework/connection/native/zephyr/connection_lib_impl.c +++ b/core/app-framework/connection/native/zephyr/connection_lib_impl.c @@ -12,12 +12,14 @@ #include "connection_lib.h" +/* clang-format off */ /* * Platform implementation of connection library */ connection_interface_t connection_impl = { - ._open = NULL, - ._close = NULL, - ._send = NULL, - ._config = NULL + ._open = NULL, + ._close = NULL, + ._send = NULL, + ._config = NULL }; +/* clang-format on */ diff --git a/core/app-framework/sensor/app/sensor.c b/core/app-framework/sensor/app/sensor.c index 2af23fafb..d898a1d3a 100644 --- a/core/app-framework/sensor/app/sensor.c +++ b/core/app-framework/sensor/app/sensor.c @@ -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; diff --git a/core/app-framework/sensor/app/sensor_api.h b/core/app-framework/sensor/app/sensor_api.h index 9f6767702..094f11085 100644 --- a/core/app-framework/sensor/app/sensor_api.h +++ b/core/app-framework/sensor/app/sensor_api.h @@ -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_ */ - diff --git a/core/app-framework/sensor/app/wa-inc/sensor.h b/core/app-framework/sensor/app/wa-inc/sensor.h index 8abc1addf..109f895d3 100644 --- a/core/app-framework/sensor/app/wa-inc/sensor.h +++ b/core/app-framework/sensor/app/wa-inc/sensor.h @@ -30,8 +30,8 @@ typedef struct _sensor *sensor_t; * @see sensor_open */ typedef void (*sensor_event_handler_f)(sensor_t sensor, - attr_container_t *sensor_event, - void *user_data); + attr_container_t *sensor_event, + void *user_data); /* ***************** @@ -49,10 +49,9 @@ typedef void (*sensor_event_handler_f)(sensor_t sensor, * * @return the sensor opened if success, NULL otherwise */ -sensor_t sensor_open(const char* name, - int index, - sensor_event_handler_f handler, - void *user_data); +sensor_t +sensor_open(const char *name, int index, sensor_event_handler_f handler, + void *user_data); /** * @brief Configure sensor with interval/bit_cfg/delay values. @@ -64,7 +63,8 @@ sensor_t sensor_open(const char* name, * * @return true if success, false otherwise */ -bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay); +bool +sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay); /** * @brief Configure sensor with attr_container_t object. @@ -74,7 +74,8 @@ bool sensor_config(sensor_t sensor, int interval, int bit_cfg, int delay); * * @return true if success, false otherwise */ -bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg); +bool +sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg); /** * @brief Close sensor. @@ -83,7 +84,8 @@ bool sensor_config_with_attr_container(sensor_t sensor, attr_container_t *cfg); * * @return true if success, false otherwise */ -bool sensor_close(sensor_t sensor); +bool +sensor_close(sensor_t sensor); #ifdef __cplusplus } diff --git a/core/app-framework/sensor/native/runtime_sensor.c b/core/app-framework/sensor/native/runtime_sensor.c index 89f7ea8bb..2e2fed929 100644 --- a/core/app-framework/sensor/native/runtime_sensor.c +++ b/core/app-framework/sensor/native/runtime_sensor.c @@ -8,12 +8,12 @@ #include "module_wasm_app.h" #include "bh_platform.h" -static sys_sensor_t * g_sys_sensors = NULL; +static sys_sensor_t *g_sys_sensors = NULL; static int g_sensor_id_max = 0; static sensor_client_t * -find_sensor_client(sys_sensor_t * sensor, - unsigned int client_id, bool remove_if_found); +find_sensor_client(sys_sensor_t *sensor, unsigned int client_id, + bool remove_if_found); void (*rechedule_sensor_callback)() = NULL; @@ -38,30 +38,32 @@ sensor_event_cleaner(sensor_event_data_t *sensor_event) static void wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data) { - attr_container_t *sensor_data = (attr_container_t *) user_data; + attr_container_t *sensor_data = (attr_container_t *)user_data; attr_container_t *sensor_data_clone; int sensor_data_len; sensor_event_data_t *sensor_event; bh_message_t msg; - sensor_client_t *c = (sensor_client_t *) client; + sensor_client_t *c = (sensor_client_t *)client; module_data *module = module_data_list_lookup_id(c->client_id); if (module == NULL) return; if (sensor_data == NULL) - return; + return; sensor_data_len = attr_container_get_serialize_length(sensor_data); - sensor_data_clone = (attr_container_t *)wasm_runtime_malloc(sensor_data_len); + sensor_data_clone = + (attr_container_t *)wasm_runtime_malloc(sensor_data_len); if (sensor_data_clone == NULL) return; /* multiple sensor clients may use/free the sensor data, so make a copy */ - bh_memcpy_s(sensor_data_clone, sensor_data_len, - sensor_data, sensor_data_len); + bh_memcpy_s(sensor_data_clone, sensor_data_len, sensor_data, + sensor_data_len); - sensor_event = (sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event)); + sensor_event = + (sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event)); if (sensor_event == NULL) { wasm_runtime_free(sensor_data_clone); return; @@ -72,9 +74,7 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data) sensor_event->data = sensor_data_clone; sensor_event->data_fmt = FMT_ATTR_CONTAINER; - msg = bh_new_msg(SENSOR_EVENT_WASM, - sensor_event, - sizeof(*sensor_event), + msg = bh_new_msg(SENSOR_EVENT_WASM, sensor_event, sizeof(*sensor_event), sensor_event_cleaner); if (!msg) { sensor_event_cleaner(sensor_event); @@ -85,19 +85,18 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data) } bool -wasm_sensor_config(wasm_exec_env_t exec_env, - uint32 sensor, int interval, +wasm_sensor_config(wasm_exec_env_t exec_env, uint32 sensor, int interval, int bit_cfg, int delay) { wasm_module_inst_t module_inst = get_module_inst(exec_env); - attr_container_t * attr_cont; - sensor_client_t * c; + attr_container_t *attr_cont; + sensor_client_t *c; sensor_obj_t s = find_sys_sensor_id(sensor); if (s == NULL) return false; - unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, - module_inst); + unsigned int mod_id = + app_manager_get_module_id(Module_WASM_App, module_inst); bh_assert(mod_id != ID_NONE); os_mutex_lock(&s->lock); @@ -131,8 +130,7 @@ wasm_sensor_config(wasm_exec_env_t exec_env, } uint32 -wasm_sensor_open(wasm_exec_env_t exec_env, - char *name, int instance) +wasm_sensor_open(wasm_exec_env_t exec_env, char *name, int instance) { wasm_module_inst_t module_inst = get_module_inst(exec_env); @@ -142,8 +140,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env, if (s == NULL) return -1; - unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, - module_inst); + unsigned int mod_id = + app_manager_get_module_id(Module_WASM_App, module_inst); bh_assert(mod_id != ID_NONE); os_mutex_lock(&s->lock); @@ -155,8 +153,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env, return -1; } - sensor_client_t * client = (sensor_client_t*) wasm_runtime_malloc( - sizeof(sensor_client_t)); + sensor_client_t *client = + (sensor_client_t *)wasm_runtime_malloc(sizeof(sensor_client_t)); if (client == NULL) { os_mutex_unlock(&s->lock); return -1; @@ -182,8 +180,8 @@ wasm_sensor_open(wasm_exec_env_t exec_env, } bool -wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, - uint32 sensor, char *buffer, int len) +wasm_sensor_config_with_attr_container(wasm_exec_env_t exec_env, uint32 sensor, + char *buffer, int len) { if (buffer != NULL) { attr_container_t *cfg = (attr_container_t *)buffer; @@ -204,8 +202,8 @@ bool wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor) { wasm_module_inst_t module_inst = get_module_inst(exec_env); - unsigned int mod_id = app_manager_get_module_id(Module_WASM_App, - module_inst); + unsigned int mod_id = + app_manager_get_module_id(Module_WASM_App, module_inst); unsigned int client_id = mod_id; sensor_obj_t s = find_sys_sensor_id(sensor); sensor_client_t *c; @@ -232,19 +230,22 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor) * sensor framework API - don't expose to the applications * */ -void set_sensor_reshceduler(void (*callback)()) +void +set_sensor_reshceduler(void (*callback)()) { rechedule_sensor_callback = callback; } // used for other threads to wakeup the sensor read thread -void reschedule_sensor_read() +void +reschedule_sensor_read() { if (rechedule_sensor_callback) rechedule_sensor_callback(); } -void refresh_read_interval(sensor_obj_t sensor) +void +refresh_read_interval(sensor_obj_t sensor) { sensor_client_t *c; uint32 interval = sensor->default_interval; @@ -266,10 +267,10 @@ void refresh_read_interval(sensor_obj_t sensor) } sensor_obj_t -add_sys_sensor(char * name, char * description, int instance, - uint32 default_interval, void * read_func, void * config_func) +add_sys_sensor(char *name, char *description, int instance, + uint32 default_interval, void *read_func, void *config_func) { - sys_sensor_t * s = (sys_sensor_t *) wasm_runtime_malloc(sizeof(sys_sensor_t)); + sys_sensor_t *s = (sys_sensor_t *)wasm_runtime_malloc(sizeof(sys_sensor_t)); if (s == NULL) return NULL; @@ -302,7 +303,8 @@ add_sys_sensor(char * name, char * description, int instance, if (g_sys_sensors == NULL) { g_sys_sensors = s; - } else { + } + else { s->next = g_sys_sensors; g_sys_sensors = s; } @@ -312,9 +314,10 @@ add_sys_sensor(char * name, char * description, int instance, return s; } -sensor_obj_t find_sys_sensor(const char* name, int instance) +sensor_obj_t +find_sys_sensor(const char *name, int instance) { - sys_sensor_t * s = g_sys_sensors; + sys_sensor_t *s = g_sys_sensors; while (s) { if (strcmp(s->name, name) == 0 && s->sensor_instance == instance) return s; @@ -324,9 +327,10 @@ sensor_obj_t find_sys_sensor(const char* name, int instance) return NULL; } -sensor_obj_t find_sys_sensor_id(uint32 sensor_id) +sensor_obj_t +find_sys_sensor_id(uint32 sensor_id) { - sys_sensor_t * s = g_sys_sensors; + sys_sensor_t *s = g_sys_sensors; while (s) { if (s->sensor_id == sensor_id) return s; @@ -336,8 +340,9 @@ sensor_obj_t find_sys_sensor_id(uint32 sensor_id) return NULL; } -sensor_client_t *find_sensor_client(sys_sensor_t * sensor, - unsigned int client_id, bool remove_if_found) +sensor_client_t * +find_sensor_client(sys_sensor_t *sensor, unsigned int client_id, + bool remove_if_found) { sensor_client_t *prev = NULL, *c = sensor->clients; @@ -351,7 +356,8 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor, sensor->clients = next; } return c; - } else { + } + else { c = c->next; } } @@ -360,12 +366,13 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor, } // return the milliseconds to next check -int check_sensor_timers() +int +check_sensor_timers() { int ms_to_next_check = -1; uint32 now = (uint32)bh_get_tick_ms(); - sys_sensor_t * s = g_sys_sensors; + sys_sensor_t *s = g_sys_sensors; while (s) { uint32 last_read = s->last_read; uint32 elpased_ms = bh_get_elpased_ms(&last_read); @@ -376,9 +383,9 @@ int check_sensor_timers() } if (elpased_ms >= s->read_interval) { - attr_container_t * data = s->read(s); + attr_container_t *data = s->read(s); if (data) { - sensor_client_t * client = s->clients; + sensor_client_t *client = s->clients; while (client) { client->client_callback(client, s->sensor_id, data); client = client->next; @@ -390,11 +397,11 @@ int check_sensor_timers() if (ms_to_next_check == -1 || (ms_to_next_check < s->read_interval)) ms_to_next_check = s->read_interval; - } else { + } + else { int remaining = s->read_interval - elpased_ms; if (ms_to_next_check == -1 || (ms_to_next_check < remaining)) ms_to_next_check = remaining; - } s = s->next; @@ -403,9 +410,10 @@ int check_sensor_timers() return ms_to_next_check; } -void sensor_cleanup_callback(uint32 module_id) +void +sensor_cleanup_callback(uint32 module_id) { - sys_sensor_t * s = g_sys_sensors; + sys_sensor_t *s = g_sys_sensors; while (s) { sensor_client_t *c; diff --git a/core/app-framework/sensor/native/runtime_sensor.h b/core/app-framework/sensor/native/runtime_sensor.h index 8154d6d91..22f625df5 100644 --- a/core/app-framework/sensor/native/runtime_sensor.h +++ b/core/app-framework/sensor/native/runtime_sensor.h @@ -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_ */ diff --git a/core/app-framework/sensor/native/sensor_mgr_ref.c b/core/app-framework/sensor/native/sensor_mgr_ref.c index 80646bee6..cbbc6df53 100644 --- a/core/app-framework/sensor/native/sensor_mgr_ref.c +++ b/core/app-framework/sensor/native/sensor_mgr_ref.c @@ -19,60 +19,62 @@ static korp_cond cond; static korp_mutex mutex; static bool sensor_check_thread_run = true; -void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg) +void +app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg) { uint32 argv[3]; wasm_function_inst_t func_onSensorEvent; bh_assert(SENSOR_EVENT_WASM == bh_message_type(msg)); - wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; + wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; - sensor_event_data_t *payload = (sensor_event_data_t*) - bh_message_payload(msg); + sensor_event_data_t *payload = + (sensor_event_data_t *)bh_message_payload(msg); if (payload == NULL) return; - func_onSensorEvent = wasm_runtime_lookup_function(inst, "_on_sensor_event", - "(i32i32i32)"); + func_onSensorEvent = + wasm_runtime_lookup_function(inst, "_on_sensor_event", "(i32i32i32)"); if (!func_onSensorEvent) - func_onSensorEvent = wasm_runtime_lookup_function(inst, "on_sensor_event", - "(i32i32i32)"); + func_onSensorEvent = wasm_runtime_lookup_function( + inst, "on_sensor_event", "(i32i32i32)"); if (!func_onSensorEvent) { printf("Cannot find function on_sensor_event\n"); - } else { + } + else { int32 sensor_data_offset; uint32 sensor_data_len; if (payload->data_fmt == FMT_ATTR_CONTAINER) { - sensor_data_len = attr_container_get_serialize_length(payload->data); - } else { + sensor_data_len = + attr_container_get_serialize_length(payload->data); + } + else { printf("Unsupported sensor data format: %d\n", payload->data_fmt); return; } - sensor_data_offset = wasm_runtime_module_dup_data(inst, payload->data, - sensor_data_len); + sensor_data_offset = + wasm_runtime_module_dup_data(inst, payload->data, sensor_data_len); if (sensor_data_offset == 0) { const char *exception = wasm_runtime_get_exception(inst); if (exception) { - printf("Got exception running wasm code: %s\n", - exception); + printf("Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); } return; } argv[0] = payload->sensor_id; - argv[1] = (uint32) sensor_data_offset; + argv[1] = (uint32)sensor_data_offset; argv[2] = sensor_data_len; if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onSensorEvent, 3, argv)) { const char *exception = wasm_runtime_get_exception(inst); bh_assert(exception); - printf(":Got exception running wasm code: %s\n", - exception); + printf(":Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); wasm_runtime_module_free(inst, sensor_data_offset); return; @@ -82,8 +84,8 @@ void app_mgr_sensor_event_callback(module_data *m_data, bh_message_t msg) } } - -static void thread_sensor_check(void * arg) +static void +thread_sensor_check(void *arg) { while (sensor_check_thread_run) { int ms_to_expiry = check_sensor_timers(); @@ -95,46 +97,44 @@ static void thread_sensor_check(void * arg) } } -static void cb_wakeup_thread() +static void +cb_wakeup_thread() { os_cond_signal(&cond); } -void set_sensor_reshceduler(void (*callback)()); +void +set_sensor_reshceduler(void (*callback)()); -void init_sensor_framework() +void +init_sensor_framework() { // init the mutext and conditions os_cond_init(&cond); os_mutex_init(&mutex); - set_sensor_reshceduler(cb_wakeup_thread); wasm_register_msg_callback(SENSOR_EVENT_WASM, app_mgr_sensor_event_callback); wasm_register_cleanup_callback(sensor_cleanup_callback); - - } -void start_sensor_framework() +void +start_sensor_framework() { korp_tid tid; - os_thread_create(&tid, - (void *)thread_sensor_check, - NULL, - BH_APPLET_PRESERVED_STACK_SIZE); + os_thread_create(&tid, (void *)thread_sensor_check, NULL, + BH_APPLET_PRESERVED_STACK_SIZE); } - -void exit_sensor_framework() +void +exit_sensor_framework() { sensor_check_thread_run = false; reschedule_sensor_read(); - //todo: wait the sensor thread termination + // todo: wait the sensor thread termination } - diff --git a/core/app-framework/sensor/native/sensor_native_api.h b/core/app-framework/sensor/native/sensor_native_api.h index ed323174e..8fbaa3e86 100644 --- a/core/app-framework/sensor/native/sensor_native_api.h +++ b/core/app-framework/sensor/native/sensor_native_api.h @@ -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_ */ - diff --git a/core/app-framework/wgl/app/gui_api.h b/core/app-framework/wgl/app/gui_api.h index a258474cf..7547cdcdc 100644 --- a/core/app-framework/wgl/app/gui_api.h +++ b/core/app-framework/wgl/app/gui_api.h @@ -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_ */ diff --git a/core/app-framework/wgl/app/src/wgl_btn.c b/core/app-framework/wgl/app/src/wgl_btn.c index 89dbbca67..680124e5f 100644 --- a/core/app-framework/wgl/app/src/wgl_btn.c +++ b/core/app-framework/wgl/app/src/wgl_btn.c @@ -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); diff --git a/core/app-framework/wgl/app/src/wgl_cb.c b/core/app-framework/wgl/app/src/wgl_cb.c index 83a541ab3..bd172d3b0 100644 --- a/core/app-framework/wgl/app/src/wgl_cb.c +++ b/core/app-framework/wgl/app/src/wgl_cb.c @@ -3,18 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ - #include "wa-inc/lvgl/lvgl.h" #include "gui_api.h" #include -#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; //} // - - diff --git a/core/app-framework/wgl/app/src/wgl_label.c b/core/app-framework/wgl/app/src/wgl_label.c index cecd9910f..81c6dcf5f 100644 --- a/core/app-framework/wgl/app/src/wgl_label.c +++ b/core/app-framework/wgl/app/src/wgl_label.c @@ -3,19 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ - - #include "wa-inc/lvgl/lvgl.h" #include "gui_api.h" #include - -#define ARGC sizeof(argv)/sizeof(uint32) +#define ARGC sizeof(argv) / sizeof(uint32) #define CALL_LABEL_NATIVE_FUNC(id) wasm_label_native_call(id, argv, ARGC) -lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy) +lv_obj_t * +lv_label_create(lv_obj_t *par, const lv_obj_t *copy) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)par; argv[1] = (uint32)copy; @@ -23,109 +21,112 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy) return (lv_obj_t *)argv[0]; } -void lv_label_set_text(lv_obj_t * label, const char * text) +void +lv_label_set_text(lv_obj_t *label, const char *text) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = (uint32)text; argv[2] = strlen(text) + 1; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT); } - -void lv_label_set_array_text(lv_obj_t * label, const char * array, uint16_t size) +void +lv_label_set_array_text(lv_obj_t *label, const char *array, uint16_t size) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = (uint32)array; argv[2] = size; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ARRAY_TEXT); } - -void lv_label_set_static_text(lv_obj_t * label, const char * text) +void +lv_label_set_static_text(lv_obj_t *label, const char *text) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = (uint32)text; argv[2] = strlen(text) + 1; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_STATIC_TEXT); } - -void lv_label_set_long_mode(lv_obj_t * label, lv_label_long_mode_t long_mode) +void +lv_label_set_long_mode(lv_obj_t *label, lv_label_long_mode_t long_mode) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = long_mode; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_LONG_MODE); } - -void lv_label_set_align(lv_obj_t * label, lv_label_align_t align) +void +lv_label_set_align(lv_obj_t *label, lv_label_align_t align) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = align; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ALIGN); } - -void lv_label_set_recolor(lv_obj_t * label, bool en) +void +lv_label_set_recolor(lv_obj_t *label, bool en) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = en; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_RECOLOR); } - -void lv_label_set_body_draw(lv_obj_t * label, bool en) +void +lv_label_set_body_draw(lv_obj_t *label, bool en) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = en; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_BODY_DRAW); } - -void lv_label_set_anim_speed(lv_obj_t * label, uint16_t anim_speed) +void +lv_label_set_anim_speed(lv_obj_t *label, uint16_t anim_speed) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = anim_speed; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_ANIM_SPEED); } - -void lv_label_set_text_sel_start(lv_obj_t * label, uint16_t index) +void +lv_label_set_text_sel_start(lv_obj_t *label, uint16_t index) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = index; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_START); } - -void lv_label_set_text_sel_end(lv_obj_t * label, uint16_t index) +void +lv_label_set_text_sel_end(lv_obj_t *label, uint16_t index) { - uint32 argv[2] = {0}; + uint32 argv[2] = { 0 }; argv[0] = (uint32)label; argv[1] = index; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_SET_TEXT_SEL_END); } -unsigned int wgl_label_get_text_length(lv_obj_t * label) +unsigned int +wgl_label_get_text_length(lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_LENGTH); return argv[0]; } -char * wgl_label_get_text(lv_obj_t * label, char *buffer, int buffer_len) +char * +wgl_label_get_text(lv_obj_t *label, char *buffer, int buffer_len) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = (uint32)buffer; argv[2] = buffer_len; @@ -133,63 +134,63 @@ char * wgl_label_get_text(lv_obj_t * label, char *buffer, int buffer_len) return (char *)argv[0]; } -// TODO: -char * lv_label_get_text(const lv_obj_t * label) +// TODO: +char * +lv_label_get_text(const lv_obj_t *label) { return NULL; - } - -lv_label_long_mode_t lv_label_get_long_mode(const lv_obj_t * label) +lv_label_long_mode_t +lv_label_get_long_mode(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LONG_MODE); return (lv_label_long_mode_t)argv[0]; } - -lv_label_align_t lv_label_get_align(const lv_obj_t * label) +lv_label_align_t +lv_label_get_align(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ALIGN); return (lv_label_align_t)argv[0]; } - -bool lv_label_get_recolor(const lv_obj_t * label) +bool +lv_label_get_recolor(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_RECOLOR); return (bool)argv[0]; } - -bool lv_label_get_body_draw(const lv_obj_t * label) +bool +lv_label_get_body_draw(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_BODY_DRAW); return (bool)argv[0]; } - -uint16_t lv_label_get_anim_speed(const lv_obj_t * label) +uint16_t +lv_label_get_anim_speed(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_ANIM_SPEED); return (uint16_t)argv[0]; } - -void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t index, lv_point_t * pos) +void +lv_label_get_letter_pos(const lv_obj_t *label, uint16_t index, lv_point_t *pos) { - uint32 argv[4] = {0}; + uint32 argv[4] = { 0 }; argv[0] = (uint32)label; argv[1] = index; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_LETTER_POS); @@ -197,10 +198,10 @@ void lv_label_get_letter_pos(const lv_obj_t * label, uint16_t index, lv_point_t pos->y = argv[3]; } - -uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) +uint16_t +lv_label_get_letter_on(const lv_obj_t *label, lv_point_t *pos) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = pos->x; argv[2] = pos->y; @@ -208,10 +209,10 @@ uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos) return (uint16_t)argv[0]; } - -bool lv_label_is_char_under_pos(const lv_obj_t * label, lv_point_t * pos) +bool +lv_label_is_char_under_pos(const lv_obj_t *label, lv_point_t *pos) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = pos->x; argv[2] = pos->y; @@ -219,28 +220,28 @@ bool lv_label_is_char_under_pos(const lv_obj_t * label, lv_point_t * pos) return (bool)argv[0]; } - -uint16_t lv_label_get_text_sel_start(const lv_obj_t * label) +uint16_t +lv_label_get_text_sel_start(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_START); return (uint16_t)argv[0]; } - -uint16_t lv_label_get_text_sel_end(const lv_obj_t * label) +uint16_t +lv_label_get_text_sel_end(const lv_obj_t *label) { - uint32 argv[1] = {0}; + uint32 argv[1] = { 0 }; argv[0] = (uint32)label; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_GET_TEXT_SEL_END); return (uint16_t)argv[0]; } - -void lv_label_ins_text(lv_obj_t * label, uint32_t pos, const char * txt) +void +lv_label_ins_text(lv_obj_t *label, uint32_t pos, const char *txt) { - uint32 argv[4] = {0}; + uint32 argv[4] = { 0 }; argv[0] = (uint32)label; argv[1] = pos; argv[2] = (uint32)txt; @@ -248,14 +249,12 @@ void lv_label_ins_text(lv_obj_t * label, uint32_t pos, const char * txt) CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_INS_TEXT); } - -void lv_label_cut_text(lv_obj_t * label, uint32_t pos, uint32_t cnt) +void +lv_label_cut_text(lv_obj_t *label, uint32_t pos, uint32_t cnt) { - uint32 argv[3] = {0}; + uint32 argv[3] = { 0 }; argv[0] = (uint32)label; argv[1] = pos; argv[2] = cnt; CALL_LABEL_NATIVE_FUNC(LABEL_FUNC_ID_CUT_TEXT); } - - diff --git a/core/app-framework/wgl/app/src/wgl_list.c b/core/app-framework/wgl/app/src/wgl_list.c index 1ac9b32ed..1ca95a6e8 100644 --- a/core/app-framework/wgl/app/src/wgl_list.c +++ b/core/app-framework/wgl/app/src/wgl_list.c @@ -3,19 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ - #include "wa-inc/lvgl/lvgl.h" #include "gui_api.h" #include -#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); //} // - - diff --git a/core/app-framework/wgl/app/src/wgl_obj.c b/core/app-framework/wgl/app/src/wgl_obj.c index ee72beb62..e1fe152c5 100644 --- a/core/app-framework/wgl/app/src/wgl_obj.c +++ b/core/app-framework/wgl/app/src/wgl_obj.c @@ -3,19 +3,18 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ - #include "wa-inc/lvgl/lvgl.h" #include "gui_api.h" #include #include -#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; diff --git a/core/app-framework/wgl/app/wa-inc/lvgl/lv_obj.h b/core/app-framework/wgl/app/wa-inc/lvgl/lv_obj.h index c79838e22..5497b0b62 100644 --- a/core/app-framework/wgl/app/wa-inc/lvgl/lv_obj.h +++ b/core/app-framework/wgl/app/wa-inc/lvgl/lv_obj.h @@ -55,12 +55,12 @@ extern "C" { struct _lv_obj_t; - /** Design modes */ enum { - LV_DESIGN_DRAW_MAIN, /**< Draw the main portion of the object */ - LV_DESIGN_DRAW_POST, /**< Draw extras on the object */ - LV_DESIGN_COVER_CHK, /**< Check if the object fully covers the 'mask_p' area */ + LV_DESIGN_DRAW_MAIN, /* Draw the main portion of the object */ + LV_DESIGN_DRAW_POST, /* Draw extras on the object */ + LV_DESIGN_COVER_CHK, /* Check if the object fully covers the 'mask_p' + area */ }; typedef uint8_t lv_design_mode_t; @@ -68,71 +68,92 @@ typedef uint8_t lv_design_mode_t; * The design callback is used to draw the object on the screen. * It accepts the object, a mask area, and the mode in which to draw the object. */ -typedef bool (*lv_design_cb_t)(struct _lv_obj_t * obj, const lv_area_t * mask_p, lv_design_mode_t mode); +typedef bool (*lv_design_cb_t)(struct _lv_obj_t *obj, const lv_area_t *mask_p, + lv_design_mode_t mode); enum { - LV_EVENT_PRESSED, /**< The object has been pressed*/ - LV_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/ - LV_EVENT_PRESS_LOST, /**< User is still pressing but slid cursor/finger off of the object */ - LV_EVENT_SHORT_CLICKED, /**< User pressed object for a short period of time, then released it. Not called if dragged. */ - LV_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `LV_INDEV_LONG_PRESS_TIME`. Not called if dragged.*/ - LV_EVENT_LONG_PRESSED_REPEAT, /**< Called after `LV_INDEV_LONG_PRESS_TIME` in every - `LV_INDEV_LONG_PRESS_REP_TIME` ms. Not called if dragged.*/ - LV_EVENT_CLICKED, /**< Called on release if not dragged (regardless to long press)*/ - LV_EVENT_RELEASED, /**< Called in every cases when the object has been released*/ - LV_EVENT_DRAG_BEGIN, + LV_EVENT_PRESSED, /* The object has been pressed */ + LV_EVENT_PRESSING, /* The object is being pressed (called continuously + while pressing) */ + LV_EVENT_PRESS_LOST, /* User is still pressing but slid cursor/finger off + of the object */ + LV_EVENT_SHORT_CLICKED, /* User pressed object for a short period of time, + then released it. Not called if dragged. */ + LV_EVENT_LONG_PRESSED, /* Object has been pressed for at least + `LV_INDEV_LONG_PRESS_TIME`. Not called if + dragged. */ + LV_EVENT_LONG_PRESSED_REPEAT, /* Called after `LV_INDEV_LONG_PRESS_TIME` + in every `LV_INDEV_LONG_PRESS_REP_TIME` + ms. Not called if dragged.*/ + LV_EVENT_CLICKED, /* Called on release if not dragged (regardless to long + press) */ + LV_EVENT_RELEASED, /* Called in every cases when the object has been + released */ + LV_EVENT_DRAG_BEGIN, LV_EVENT_DRAG_END, LV_EVENT_DRAG_THROW_BEGIN, LV_EVENT_KEY, LV_EVENT_FOCUSED, LV_EVENT_DEFOCUSED, - LV_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved) */ + LV_EVENT_VALUE_CHANGED, /* The object's value has changed (i.e. slider + moved) */ LV_EVENT_INSERT, LV_EVENT_REFRESH, - LV_EVENT_APPLY, /**< "Ok", "Apply" or similar specific button has clicked*/ - LV_EVENT_CANCEL, /**< "Close", "Cancel" or similar specific button has clicked*/ - LV_EVENT_DELETE, /**< Object is being deleted */ + LV_EVENT_APPLY, /* "Ok", "Apply" or similar specific button has clicked */ + LV_EVENT_CANCEL, /* "Close", "Cancel" or similar specific button has + clicked */ + LV_EVENT_DELETE, /* Object is being deleted */ }; -typedef uint8_t lv_event_t; /**< Type of event being sent to the object. */ + +typedef uint8_t lv_event_t; /* Type of event being sent to the object. */ /** * @brief Event callback. * Events are used to notify the user of some action being taken on the object. * For details, see ::lv_event_t. */ -typedef void (*lv_event_cb_t)(struct _lv_obj_t * obj, lv_event_t event); +typedef void (*lv_event_cb_t)(struct _lv_obj_t *obj, lv_event_t event); -/** Signals are for use by the object itself or to extend the object's functionality. - * Applications should use ::lv_obj_set_event_cb to be notified of events that occur - * on the object. */ +/** Signals are for use by the object itself or to extend the object's + * functionality. Applications should use ::lv_obj_set_event_cb to be notified + * of events that occur on the object. */ enum { /*General signals*/ - LV_SIGNAL_CLEANUP, /**< Object is being deleted */ - LV_SIGNAL_CHILD_CHG, /**< Child was removed/added */ - LV_SIGNAL_CORD_CHG, /**< Object coordinates/size have changed */ - LV_SIGNAL_PARENT_SIZE_CHG, /**< Parent's size has changed */ - LV_SIGNAL_STYLE_CHG, /**< Object's style has changed */ - LV_SIGNAL_REFR_EXT_DRAW_PAD, /**< Object's extra padding has changed */ - LV_SIGNAL_GET_TYPE, /**< LittlevGL needs to retrieve the object's type */ + LV_SIGNAL_CLEANUP, /* Object is being deleted */ + LV_SIGNAL_CHILD_CHG, /* Child was removed/added */ + LV_SIGNAL_CORD_CHG, /* Object coordinates/size have changed */ + LV_SIGNAL_PARENT_SIZE_CHG, /* Parent's size has changed */ + LV_SIGNAL_STYLE_CHG, /* Object's style has changed */ + LV_SIGNAL_REFR_EXT_DRAW_PAD, /* Object's extra padding has changed */ + LV_SIGNAL_GET_TYPE, /* LittlevGL needs to retrieve the object's type */ /*Input device related*/ - LV_SIGNAL_PRESSED, /**< The object has been pressed*/ - LV_SIGNAL_PRESSING, /**< The object is being pressed (called continuously while pressing)*/ - LV_SIGNAL_PRESS_LOST, /**< User is still pressing but slid cursor/finger off of the object */ - LV_SIGNAL_RELEASED, /**< User pressed object for a short period of time, then released it. Not called if dragged. */ - LV_SIGNAL_LONG_PRESS, /**< Object has been pressed for at least `LV_INDEV_LONG_PRESS_TIME`. Not called if dragged.*/ - LV_SIGNAL_LONG_PRESS_REP, /**< Called after `LV_INDEV_LONG_PRESS_TIME` in every `LV_INDEV_LONG_PRESS_REP_TIME` ms. Not called if dragged.*/ - LV_SIGNAL_DRAG_BEGIN, - LV_SIGNAL_DRAG_END, + LV_SIGNAL_PRESSED, /* The object has been pressed*/ + LV_SIGNAL_PRESSING, /* The object is being pressed (called continuously + while pressing)*/ + LV_SIGNAL_PRESS_LOST, /* User is still pressing but slid cursor/finger off + of the object */ + LV_SIGNAL_RELEASED, /* User pressed object for a short period of time, + then released it. Not called if dragged. */ + LV_SIGNAL_LONG_PRESS, /* Object has been pressed for at least + `LV_INDEV_LONG_PRESS_TIME`. Not called if + dragged.*/ + LV_SIGNAL_LONG_PRESS_REP, /* Called after `LV_INDEV_LONG_PRESS_TIME` in + every `LV_INDEV_LONG_PRESS_REP_TIME` ms. Not + called if dragged.*/ + LV_SIGNAL_DRAG_BEGIN, + LV_SIGNAL_DRAG_END, /*Group related*/ LV_SIGNAL_FOCUS, LV_SIGNAL_DEFOCUS, LV_SIGNAL_CONTROL, LV_SIGNAL_GET_EDITABLE, }; + typedef uint8_t lv_signal_t; -typedef lv_res_t (*lv_signal_cb_t)(struct _lv_obj_t * obj, lv_signal_t sign, void * param); +typedef lv_res_t (*lv_signal_cb_t)(struct _lv_obj_t *obj, lv_signal_t sign, + void *param); /** Object alignment. */ enum { @@ -161,22 +182,21 @@ enum { typedef uint8_t lv_align_t; #if LV_USE_OBJ_REALIGN -typedef struct -{ - const struct _lv_obj_t * base; +typedef struct { + const struct _lv_obj_t *base; lv_coord_t xofs; lv_coord_t yofs; lv_align_t align; uint8_t auto_realign : 1; - uint8_t origo_align : 1; /**< 1: the origo (center of the object) was aligned with - `lv_obj_align_origo`*/ + uint8_t origo_align : 1; /* 1: the origo (center of the object) was + aligned with `lv_obj_align_origo`*/ } lv_reailgn_t; #endif enum { - LV_DRAG_DIR_HOR = 0x1, /**< Object can be dragged horizontally. */ - LV_DRAG_DIR_VER = 0x2, /**< Object can be dragged vertically. */ - LV_DRAG_DIR_ALL = 0x3, /**< Object can be dragged in all directions. */ + LV_DRAG_DIR_HOR = 0x1, /* Object can be dragged horizontally. */ + LV_DRAG_DIR_VER = 0x2, /* Object can be dragged vertically. */ + LV_DRAG_DIR_ALL = 0x3, /* Object can be dragged in all directions. */ }; typedef uint8_t lv_drag_dir_t; @@ -184,7 +204,6 @@ typedef uint8_t lv_drag_dir_t; typedef void lv_obj_t; typedef void lv_obj_type_t; - /********************** * GLOBAL PROTOTYPES **********************/ @@ -192,7 +211,8 @@ typedef void lv_obj_type_t; /** * Init. the 'lv' library. */ -void lv_init(void); +void +lv_init(void); /*-------------------- * Create and delete @@ -201,38 +221,46 @@ void lv_init(void); /** * Create a basic object * @param parent pointer to a parent object. - * If NULL then a screen will be created - * @param copy pointer to a base object, if not NULL then the new object will be copied from it + * If NULL then a screen will be created + * @param copy pointer to a base object, if not NULL then + * the new object will be copied from it * @return pointer to the new object */ -lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy); +lv_obj_t * +lv_obj_create(lv_obj_t *parent, const lv_obj_t *copy); /** * Delete 'obj' and all of its children * @param obj pointer to an object to delete * @return LV_RES_INV because the object is deleted */ -lv_res_t lv_obj_del(lv_obj_t * obj); +lv_res_t +lv_obj_del(lv_obj_t *obj); /** * Helper function for asynchronously deleting objects. - * Useful for cases where you can't delete an object directly in an `LV_EVENT_DELETE` handler (i.e. parent). + * Useful for cases where you can't delete an object directly in an + * `LV_EVENT_DELETE` handler (i.e. parent). * @param obj object to delete * @see lv_async_call */ -void lv_obj_del_async(struct _lv_obj_t *obj); +void +lv_obj_del_async(struct _lv_obj_t *obj); /** * Delete all children of an object * @param obj pointer to an object */ -void lv_obj_clean(lv_obj_t * obj); +void +lv_obj_clean(lv_obj_t *obj); /** - * Mark the object as invalid therefore its current position will be redrawn by 'lv_refr_task' + * Mark the object as invalid therefore its current position will be redrawn by + * 'lv_refr_task' * @param obj pointer to an object */ -void lv_obj_invalidate(const lv_obj_t * obj); +void +lv_obj_invalidate(const lv_obj_t *obj); /*===================== * Setter functions @@ -247,19 +275,22 @@ void lv_obj_invalidate(const lv_obj_t * obj); * @param obj pointer to an object. Can't be a screen. * @param parent pointer to the new parent object. (Can't be NULL) */ -void lv_obj_set_parent(lv_obj_t * obj, lv_obj_t * parent); +void +lv_obj_set_parent(lv_obj_t *obj, lv_obj_t *parent); /** * Move and object to the foreground * @param obj pointer to an object */ -void lv_obj_move_foreground(lv_obj_t * obj); +void +lv_obj_move_foreground(lv_obj_t *obj); /** * Move and object to the background * @param obj pointer to an object */ -void lv_obj_move_background(lv_obj_t * obj); +void +lv_obj_move_background(lv_obj_t *obj); /*-------------------- * Coordinate set @@ -271,21 +302,24 @@ void lv_obj_move_background(lv_obj_t * obj); * @param x new distance from the left side of the parent * @param y new distance from the top of the parent */ -void lv_obj_set_pos(lv_obj_t * obj, lv_coord_t x, lv_coord_t y); +void +lv_obj_set_pos(lv_obj_t *obj, lv_coord_t x, lv_coord_t y); /** * Set the x coordinate of a object * @param obj pointer to an object * @param x new distance from the left side from the parent */ -void lv_obj_set_x(lv_obj_t * obj, lv_coord_t x); +void +lv_obj_set_x(lv_obj_t *obj, lv_coord_t x); /** * Set the y coordinate of a object * @param obj pointer to an object * @param y new distance from the top of the parent */ -void lv_obj_set_y(lv_obj_t * obj, lv_coord_t y); +void +lv_obj_set_y(lv_obj_t *obj, lv_coord_t y); /** * Set the size of an object @@ -293,55 +327,66 @@ void lv_obj_set_y(lv_obj_t * obj, lv_coord_t y); * @param w new width * @param h new height */ -void lv_obj_set_size(lv_obj_t * obj, lv_coord_t w, lv_coord_t h); +void +lv_obj_set_size(lv_obj_t *obj, lv_coord_t w, lv_coord_t h); /** * Set the width of an object * @param obj pointer to an object * @param w new width */ -void lv_obj_set_width(lv_obj_t * obj, lv_coord_t w); +void +lv_obj_set_width(lv_obj_t *obj, lv_coord_t w); /** * Set the height of an object * @param obj pointer to an object * @param h new height */ -void lv_obj_set_height(lv_obj_t * obj, lv_coord_t h); +void +lv_obj_set_height(lv_obj_t *obj, lv_coord_t h); /** * Align an object to an other object. * @param obj pointer to an object to align - * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param base pointer to an object (if NULL the parent is used). 'obj' will be + * aligned to it. * @param align type of alignment (see 'lv_align_t' enum) * @param x_mod x coordinate shift after alignment * @param y_mod y coordinate shift after alignment */ -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); /** * Align an object to an other object. * @param obj pointer to an object to align - * @param base pointer to an object (if NULL the parent is used). 'obj' will be aligned to it. + * @param base pointer to an object (if NULL the parent is used). 'obj' will be + * aligned to it. * @param align type of alignment (see 'lv_align_t' enum) * @param x_mod x coordinate shift after alignment * @param y_mod y coordinate shift after alignment */ -void lv_obj_align_origo(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_origo(lv_obj_t *obj, const lv_obj_t *base, lv_align_t align, + lv_coord_t x_mod, lv_coord_t y_mod); /** * Realign the object based on the last `lv_obj_align` parameters. * @param obj pointer to an object */ -void lv_obj_realign(lv_obj_t * obj); +void +lv_obj_realign(lv_obj_t *obj); /** - * Enable the automatic realign of the object when its size has changed based on the last - * `lv_obj_align` parameters. + * Enable the automatic realign of the object when its size has changed based on + * the last `lv_obj_align` parameters. * @param obj pointer to an object * @param en true: enable auto realign; false: disable auto realign */ -void lv_obj_set_auto_realign(lv_obj_t * obj, bool en); +void +lv_obj_set_auto_realign(lv_obj_t *obj, bool en); /** * Set the size of an extended clickable area @@ -351,7 +396,9 @@ void lv_obj_set_auto_realign(lv_obj_t * obj, bool en); * @param top extended clickable are on the top [px] * @param bottom extended clickable are on the bottom [px] */ -void lv_obj_set_ext_click_area(lv_obj_t * obj, lv_coord_t left, lv_coord_t right, lv_coord_t top, lv_coord_t bottom); +void +lv_obj_set_ext_click_area(lv_obj_t *obj, lv_coord_t left, lv_coord_t right, + lv_coord_t top, lv_coord_t bottom); /*--------------------- * Appearance set @@ -362,20 +409,23 @@ void lv_obj_set_ext_click_area(lv_obj_t * obj, lv_coord_t left, lv_coord_t right * @param obj pointer to an object * @param style_p pointer to the new style */ -void lv_obj_set_style(lv_obj_t * obj, const lv_style_t * style); +void +lv_obj_set_style(lv_obj_t *obj, const lv_style_t *style); /** * Notify an object about its style is modified * @param obj pointer to an object */ -void lv_obj_refresh_style(lv_obj_t * obj); +void +lv_obj_refresh_style(lv_obj_t *obj); /** * Notify all object if a style is modified - * @param style pointer to a style. Only the objects with this style will be notified - * (NULL to notify all objects) + * @param style pointer to a style. Only the objects with this style will be + * notified (NULL to notify all objects) */ -void lv_obj_report_style_mod(lv_style_t * style); +void +lv_obj_report_style_mod(lv_style_t *style); /*----------------- * Attribute set @@ -386,14 +436,16 @@ void lv_obj_report_style_mod(lv_style_t * style); * @param obj pointer to an object * @param en true: hide the object */ -void lv_obj_set_hidden(lv_obj_t * obj, bool en); +void +lv_obj_set_hidden(lv_obj_t *obj, bool en); /** * Enable or disable the clicking of an object * @param obj pointer to an object * @param en true: make the object clickable */ -void lv_obj_set_click(lv_obj_t * obj, bool en); +void +lv_obj_set_click(lv_obj_t *obj, bool en); /** * Enable to bring this object to the foreground if it @@ -401,28 +453,32 @@ void lv_obj_set_click(lv_obj_t * obj, bool en); * @param obj pointer to an object * @param en true: enable the auto top feature */ -void lv_obj_set_top(lv_obj_t * obj, bool en); +void +lv_obj_set_top(lv_obj_t *obj, bool en); /** * Enable the dragging of an object * @param obj pointer to an object * @param en true: make the object dragable */ -void lv_obj_set_drag(lv_obj_t * obj, bool en); +void +lv_obj_set_drag(lv_obj_t *obj, bool en); /** * Set the directions an object can be dragged in * @param obj pointer to an object * @param drag_dir bitwise OR of allowed drag directions */ -void lv_obj_set_drag_dir(lv_obj_t * obj, lv_drag_dir_t drag_dir); +void +lv_obj_set_drag_dir(lv_obj_t *obj, lv_drag_dir_t drag_dir); /** * Enable the throwing of an object after is is dragged * @param obj pointer to an object * @param en true: enable the drag throw */ -void lv_obj_set_drag_throw(lv_obj_t * obj, bool en); +void +lv_obj_set_drag_throw(lv_obj_t *obj, bool en); /** * Enable to use parent for drag related operations. @@ -430,45 +486,54 @@ void lv_obj_set_drag_throw(lv_obj_t * obj, bool en); * @param obj pointer to an object * @param en true: enable the 'drag parent' for the object */ -void lv_obj_set_drag_parent(lv_obj_t * obj, bool en); +void +lv_obj_set_drag_parent(lv_obj_t *obj, bool en); /** * Propagate the events to the parent too * @param obj pointer to an object * @param en true: enable the event propagation */ -void lv_obj_set_parent_event(lv_obj_t * obj, bool en); +void +lv_obj_set_parent_event(lv_obj_t *obj, bool en); /** - * Set the opa scale enable parameter (required to set opa_scale with `lv_obj_set_opa_scale()`) + * Set the opa scale enable parameter (required to set opa_scale with + * `lv_obj_set_opa_scale()`) * @param obj pointer to an object - * @param en true: opa scaling is enabled for this object and all children; false: no opa scaling + * @param en true: opa scaling is enabled for this object and all children; + * false: no opa scaling */ -void lv_obj_set_opa_scale_enable(lv_obj_t * obj, bool en); +void +lv_obj_set_opa_scale_enable(lv_obj_t *obj, bool en); /** * Set the opa scale of an object. - * The opacity of this object and all it's children will be scaled down with this factor. - * `lv_obj_set_opa_scale_enable(obj, true)` needs to be called to enable it. - * (not for all children just for the parent where to start the opa scaling) + * The opacity of this object and all it's children will be scaled down with + * this factor. `lv_obj_set_opa_scale_enable(obj, true)` needs to be called to + * enable it. (not for all children just for the parent where to start the opa + * scaling) * @param obj pointer to an object * @param opa_scale a factor to scale down opacity [0..255] */ -void lv_obj_set_opa_scale(lv_obj_t * obj, lv_opa_t opa_scale); +void +lv_obj_set_opa_scale(lv_obj_t *obj, lv_opa_t opa_scale); /** * Set a bit or bits in the protect filed * @param obj pointer to an object * @param prot 'OR'-ed values from `lv_protect_t` */ -void lv_obj_set_protect(lv_obj_t * obj, uint8_t prot); +void +lv_obj_set_protect(lv_obj_t *obj, uint8_t prot); /** * Clear a bit or bits in the protect filed * @param obj pointer to an object * @param prot 'OR'-ed values from `lv_protect_t` */ -void lv_obj_clear_protect(lv_obj_t * obj, uint8_t prot); +void +lv_obj_clear_protect(lv_obj_t *obj, uint8_t prot); /** * Set a an event handler function for an object. @@ -476,34 +541,44 @@ void lv_obj_clear_protect(lv_obj_t * obj, uint8_t prot); * @param obj pointer to an object * @param event_cb the new event function */ -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); /** * Send an event to the object * @param obj pointer to an object * @param event the type of the event from `lv_event_t`. - * @param data arbitrary data depending on the object type and the event. (Usually `NULL`) - * @return LV_RES_OK: `obj` was not deleted in the event; LV_RES_INV: `obj` was deleted in the event + * @param data arbitrary data depending on the object type and the event. + * (Usually `NULL`) + * @return LV_RES_OK: `obj` was not deleted in the event; LV_RES_INV: `obj` + * was deleted in the event */ -lv_res_t lv_event_send(lv_obj_t * obj, lv_event_t event, const void * data); +lv_res_t +lv_event_send(lv_obj_t *obj, lv_event_t event, const void *data); /** * Call an event function with an object, event, and data. - * @param event_xcb an event callback function. If `NULL` `LV_RES_OK` will return without any actions. - * (the 'x' in the argument name indicates that its not a fully generic function because it not follows - * the `func_name(object, callback, ...)` convention) - * @param obj pointer to an object to associate with the event (can be `NULL` to simply call the `event_cb`) + * @param event_xcb an event callback function. If `NULL` `LV_RES_OK` will + * return without any actions. (the 'x' in the argument name indicates + * that its not a fully generic function because it not follows the + * `func_name(object, callback, ...)` convention) + * @param obj pointer to an object to associate with the event (can be `NULL` + * to simply call the `event_cb`) * @param event an event * @param data pointer to a custom data - * @return LV_RES_OK: `obj` was not deleted in the event; LV_RES_INV: `obj` was deleted in the event + * @return LV_RES_OK: `obj` was not deleted in the event; LV_RES_INV: `obj` + * was deleted in the event */ -lv_res_t lv_event_send_func(lv_event_cb_t event_xcb, lv_obj_t * obj, lv_event_t event, const void * data); +lv_res_t +lv_event_send_func(lv_event_cb_t event_xcb, lv_obj_t *obj, lv_event_t event, + const void *data); /** * Get the `data` parameter of the current event * @return the `data` parameter */ -const void * lv_event_get_data(void); +const void * +lv_event_get_data(void); /** * Set the a signal function of an object. Used internally by the library. @@ -511,21 +586,24 @@ const void * lv_event_get_data(void); * @param obj pointer to an object * @param signal_cb the new signal function */ -void lv_obj_set_signal_cb(lv_obj_t * obj, lv_signal_cb_t signal_cb); +void +lv_obj_set_signal_cb(lv_obj_t *obj, lv_signal_cb_t signal_cb); /** * Send an event to the object * @param obj pointer to an object * @param event the type of the event from `lv_event_t`. */ -void lv_signal_send(lv_obj_t * obj, lv_signal_t signal, void * param); +void +lv_signal_send(lv_obj_t *obj, lv_signal_t signal, void *param); /** * Set a new design function for an object * @param obj pointer to an object * @param design_cb the new design function */ -void lv_obj_set_design_cb(lv_obj_t * obj, lv_design_cb_t design_cb); +void +lv_obj_set_design_cb(lv_obj_t *obj, lv_design_cb_t design_cb); /*---------------- * Other set @@ -537,13 +615,15 @@ void lv_obj_set_design_cb(lv_obj_t * obj, lv_design_cb_t design_cb); * @param ext_size the size of the new ext. data * @return pointer to the allocated ext */ -void * lv_obj_allocate_ext_attr(lv_obj_t * obj, uint16_t ext_size); +void * +lv_obj_allocate_ext_attr(lv_obj_t *obj, uint16_t ext_size); /** * Send a 'LV_SIGNAL_REFR_EXT_SIZE' signal to the object * @param obj pointer to an object */ -void lv_obj_refresh_ext_draw_pad(lv_obj_t * obj); +void +lv_obj_refresh_ext_draw_pad(lv_obj_t *obj); /*======================= * Getter functions @@ -554,14 +634,16 @@ void lv_obj_refresh_ext_draw_pad(lv_obj_t * obj); * @param obj pointer to an object * @return pointer to a screen */ -lv_obj_t * lv_obj_get_screen(const lv_obj_t * obj); +lv_obj_t * +lv_obj_get_screen(const lv_obj_t *obj); /** * Get the display of an object * @param scr pointer to an object * @return pointer the object's display */ -lv_disp_t * lv_obj_get_disp(const lv_obj_t * obj); +lv_disp_t * +lv_obj_get_disp(const lv_obj_t *obj); /*--------------------- * Parent/children get @@ -572,38 +654,45 @@ lv_disp_t * lv_obj_get_disp(const lv_obj_t * obj); * @param obj pointer to an object * @return pointer to the parent of 'obj' */ -lv_obj_t * lv_obj_get_parent(const lv_obj_t * obj); +lv_obj_t * +lv_obj_get_parent(const lv_obj_t *obj); /** - * Iterate through the children of an object (start from the "youngest, lastly created") + * Iterate through the children of an object (start from the "youngest, lastly + * created") * @param obj pointer to an object * @param child NULL at first call to get the next children - * and the previous return value later + * and the previous return value later * @return the child after 'act_child' or NULL if no more child */ -lv_obj_t * lv_obj_get_child(const lv_obj_t * obj, const lv_obj_t * child); +lv_obj_t * +lv_obj_get_child(const lv_obj_t *obj, const lv_obj_t *child); /** - * Iterate through the children of an object (start from the "oldest", firstly created) + * Iterate through the children of an object (start from the "oldest", firstly + * created) * @param obj pointer to an object * @param child NULL at first call to get the next children - * and the previous return value later + * and the previous return value later * @return the child after 'act_child' or NULL if no more child */ -lv_obj_t * lv_obj_get_child_back(const lv_obj_t * obj, const lv_obj_t * child); +lv_obj_t * +lv_obj_get_child_back(const lv_obj_t *obj, const lv_obj_t *child); /** * Count the children of an object (only children directly on 'obj') * @param obj pointer to an object * @return children number of 'obj' */ -uint16_t lv_obj_count_children(const lv_obj_t * obj); +uint16_t +lv_obj_count_children(const lv_obj_t *obj); /** Recursively count the children of an object * @param obj pointer to an object * @return children number of 'obj' */ -uint16_t lv_obj_count_children_recursive(const lv_obj_t * obj); +uint16_t +lv_obj_count_children_recursive(const lv_obj_t *obj); /*--------------------- * Coordinate get @@ -614,98 +703,113 @@ uint16_t lv_obj_count_children_recursive(const lv_obj_t * obj); * @param obj pointer to an object * @param cords_p pointer to an area to store the coordinates */ -void lv_obj_get_coords(const lv_obj_t * obj, lv_area_t * cords_p); +void +lv_obj_get_coords(const lv_obj_t *obj, lv_area_t *cords_p); /** - * Reduce area retried by `lv_obj_get_coords()` the get graphically usable area of an object. - * (Without the size of the border or other extra graphical elements) + * Reduce area retried by `lv_obj_get_coords()` the get graphically usable area + * of an object. (Without the size of the border or other extra graphical + * elements) * @param coords_p store the result area here */ -void lv_obj_get_inner_coords(const lv_obj_t * obj, lv_area_t * coords_p); +void +lv_obj_get_inner_coords(const lv_obj_t *obj, lv_area_t *coords_p); /** * Get the x coordinate of object * @param obj pointer to an object * @return distance of 'obj' from the left side of its parent */ -lv_coord_t lv_obj_get_x(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_x(const lv_obj_t *obj); /** * Get the y coordinate of object * @param obj pointer to an object * @return distance of 'obj' from the top of its parent */ -lv_coord_t lv_obj_get_y(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_y(const lv_obj_t *obj); /** * Get the width of an object * @param obj pointer to an object * @return the width */ -lv_coord_t lv_obj_get_width(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_width(const lv_obj_t *obj); /** * Get the height of an object * @param obj pointer to an object * @return the height */ -lv_coord_t lv_obj_get_height(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_height(const lv_obj_t *obj); /** * Get that width reduced by the left and right padding. * @param obj pointer to an object * @return the width which still fits into the container */ -lv_coord_t lv_obj_get_width_fit(lv_obj_t * obj); +lv_coord_t +lv_obj_get_width_fit(lv_obj_t *obj); /** * Get that height reduced by the top an bottom padding. * @param obj pointer to an object * @return the height which still fits into the container */ -lv_coord_t lv_obj_get_height_fit(lv_obj_t * obj); +lv_coord_t +lv_obj_get_height_fit(lv_obj_t *obj); /** * Get the automatic realign property of the object. * @param obj pointer to an object * @return true: auto realign is enabled; false: auto realign is disabled */ -bool lv_obj_get_auto_realign(lv_obj_t * obj); +bool +lv_obj_get_auto_realign(lv_obj_t *obj); /** * Get the left padding of extended clickable area * @param obj pointer to an object * @return the extended left padding */ -lv_coord_t lv_obj_get_ext_click_pad_left(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_ext_click_pad_left(const lv_obj_t *obj); /** * Get the right padding of extended clickable area * @param obj pointer to an object * @return the extended right padding */ -lv_coord_t lv_obj_get_ext_click_pad_right(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_ext_click_pad_right(const lv_obj_t *obj); /** * Get the top padding of extended clickable area * @param obj pointer to an object * @return the extended top padding */ -lv_coord_t lv_obj_get_ext_click_pad_top(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_ext_click_pad_top(const lv_obj_t *obj); /** * Get the bottom padding of extended clickable area * @param obj pointer to an object * @return the extended bottom padding */ -lv_coord_t lv_obj_get_ext_click_pad_bottom(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_ext_click_pad_bottom(const lv_obj_t *obj); /** * Get the extended size attribute of an object * @param obj pointer to an object * @return the extended size attribute */ -lv_coord_t lv_obj_get_ext_draw_pad(const lv_obj_t * obj); +lv_coord_t +lv_obj_get_ext_draw_pad(const lv_obj_t *obj); /*----------------- * Appearance get @@ -716,7 +820,8 @@ lv_coord_t lv_obj_get_ext_draw_pad(const lv_obj_t * obj); * @param obj pointer to an object * @return pointer to a style */ -const lv_style_t * lv_obj_get_style(const lv_obj_t * obj); +const lv_style_t * +lv_obj_get_style(const lv_obj_t *obj); /*----------------- * Attribute get @@ -727,77 +832,89 @@ const lv_style_t * lv_obj_get_style(const lv_obj_t * obj); * @param obj pointer to an object * @return true: the object is hidden */ -bool lv_obj_get_hidden(const lv_obj_t * obj); +bool +lv_obj_get_hidden(const lv_obj_t *obj); /** * Get the click enable attribute of an object * @param obj pointer to an object * @return true: the object is clickable */ -bool lv_obj_get_click(const lv_obj_t * obj); +bool +lv_obj_get_click(const lv_obj_t *obj); /** * Get the top enable attribute of an object * @param obj pointer to an object * @return true: the auto top feature is enabled */ -bool lv_obj_get_top(const lv_obj_t * obj); +bool +lv_obj_get_top(const lv_obj_t *obj); /** * Get the drag enable attribute of an object * @param obj pointer to an object * @return true: the object is dragable */ -bool lv_obj_get_drag(const lv_obj_t * obj); +bool +lv_obj_get_drag(const lv_obj_t *obj); /** * Get the directions an object can be dragged * @param obj pointer to an object * @return bitwise OR of allowed directions an object can be dragged in */ -lv_drag_dir_t lv_obj_get_drag_dir(const lv_obj_t * obj); +lv_drag_dir_t +lv_obj_get_drag_dir(const lv_obj_t *obj); /** * Get the drag throw enable attribute of an object * @param obj pointer to an object * @return true: drag throw is enabled */ -bool lv_obj_get_drag_throw(const lv_obj_t * obj); +bool +lv_obj_get_drag_throw(const lv_obj_t *obj); /** * Get the drag parent attribute of an object * @param obj pointer to an object * @return true: drag parent is enabled */ -bool lv_obj_get_drag_parent(const lv_obj_t * obj); +bool +lv_obj_get_drag_parent(const lv_obj_t *obj); /** * Get the drag parent attribute of an object * @param obj pointer to an object * @return true: drag parent is enabled */ -bool lv_obj_get_parent_event(const lv_obj_t * obj); +bool +lv_obj_get_parent_event(const lv_obj_t *obj); /** * Get the opa scale enable parameter * @param obj pointer to an object - * @return true: opa scaling is enabled for this object and all children; false: no opa scaling + * @return true: opa scaling is enabled for this object and all children; false: + * no opa scaling */ -lv_opa_t lv_obj_get_opa_scale_enable(const lv_obj_t * obj); +lv_opa_t +lv_obj_get_opa_scale_enable(const lv_obj_t *obj); /** * Get the opa scale parameter of an object * @param obj pointer to an object * @return opa scale [0..255] */ -lv_opa_t lv_obj_get_opa_scale(const lv_obj_t * obj); +lv_opa_t +lv_obj_get_opa_scale(const lv_obj_t *obj); /** * Get the protect field of an object * @param obj pointer to an object * @return protect field ('OR'ed values of `lv_protect_t`) */ -uint8_t lv_obj_get_protect(const lv_obj_t * obj); +uint8_t +lv_obj_get_protect(const lv_obj_t *obj); /** * Check at least one bit of a given protect bitfield is set @@ -805,28 +922,32 @@ uint8_t lv_obj_get_protect(const lv_obj_t * obj); * @param prot protect bits to test ('OR'ed values of `lv_protect_t`) * @return false: none of the given bits are set, true: at least one bit is set */ -bool lv_obj_is_protected(const lv_obj_t * obj, uint8_t prot); +bool +lv_obj_is_protected(const lv_obj_t *obj, uint8_t prot); /** * Get the signal function of an object * @param obj pointer to an object * @return the signal function */ -lv_signal_cb_t lv_obj_get_signal_cb(const lv_obj_t * obj); +lv_signal_cb_t +lv_obj_get_signal_cb(const lv_obj_t *obj); /** * Get the design function of an object * @param obj pointer to an object * @return the design function */ -lv_design_cb_t lv_obj_get_design_cb(const lv_obj_t * obj); +lv_design_cb_t +lv_obj_get_design_cb(const lv_obj_t *obj); /** * Get the event function of an object * @param obj pointer to an object * @return the event function */ -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); /*------------------ * Other get @@ -838,15 +959,18 @@ lv_event_cb_t lv_obj_get_event_cb(const lv_obj_t * obj); * @return the ext pointer but not the dynamic version * Use it as ext->data1, and NOT da(ext)->data1 */ -void * lv_obj_get_ext_attr(const lv_obj_t * obj); +void * +lv_obj_get_ext_attr(const lv_obj_t *obj); /** - * Get object's and its ancestors type. Put their name in `type_buf` starting with the current type. - * E.g. buf.type[0]="lv_btn", buf.type[1]="lv_cont", buf.type[2]="lv_obj" + * Get object's and its ancestors type. Put their name in `type_buf` starting + * with the current type. E.g. buf.type[0]="lv_btn", buf.type[1]="lv_cont", + * buf.type[2]="lv_obj" * @param obj pointer to an object which type should be get * @param buf pointer to an `lv_obj_type_t` buffer to store the types */ -void lv_obj_get_type(lv_obj_t * obj, lv_obj_type_t * buf); +void +lv_obj_get_type(lv_obj_t *obj, lv_obj_type_t *buf); #if LV_USE_USER_DATA /** @@ -854,21 +978,24 @@ void lv_obj_get_type(lv_obj_t * obj, lv_obj_type_t * buf); * @param obj pointer to an object * @return user data */ -lv_obj_user_data_t lv_obj_get_user_data(lv_obj_t * obj); +lv_obj_user_data_t +lv_obj_get_user_data(lv_obj_t *obj); /** * Get a pointer to the object's user data * @param obj pointer to an object * @return pointer to the user data */ -lv_obj_user_data_t * lv_obj_get_user_data_ptr(lv_obj_t * obj); +lv_obj_user_data_t * +lv_obj_get_user_data_ptr(lv_obj_t *obj); /** * Set the object's user data. The data will be copied. * @param obj pointer to an object * @param data user data */ -void lv_obj_set_user_data(lv_obj_t * obj, lv_obj_user_data_t data); +void +lv_obj_set_user_data(lv_obj_t *obj, lv_obj_user_data_t data); #endif @@ -878,14 +1005,17 @@ void lv_obj_set_user_data(lv_obj_t * obj, lv_obj_user_data_t data); * @param obj pointer to an object * @return the pointer to group of the object */ -void * lv_obj_get_group(const lv_obj_t * obj); +void * +lv_obj_get_group(const lv_obj_t *obj); /** * Tell whether the object is the focused object of a group or not. * @param obj pointer to an object - * @return true: the object is focused, false: the object is not focused or not in a group + * @return true: the object is focused, + * false: the object is not focused or not in a group */ -bool lv_obj_is_focused(const lv_obj_t * obj); +bool +lv_obj_is_focused(const lv_obj_t *obj); #endif @@ -907,7 +1037,7 @@ bool lv_obj_is_focused(const lv_obj_t * obj); * } * } */ -#define LV_EVENT_CB_DECLARE(name) void name(lv_obj_t * obj, lv_event_t e) +#define LV_EVENT_CB_DECLARE(name) void name(lv_obj_t *obj, lv_event_t e) #ifdef __cplusplus } /* extern "C" */ diff --git a/core/app-framework/wgl/app/wa-inc/lvgl/lvgl.h b/core/app-framework/wgl/app/wa-inc/lvgl/lvgl.h index c70dd49be..7c4c7b2fe 100644 --- a/core/app-framework/wgl/app/wa-inc/lvgl/lvgl.h +++ b/core/app-framework/wgl/app/wa-inc/lvgl/lvgl.h @@ -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" diff --git a/core/app-framework/wgl/app/wa-inc/lvgl/test.c b/core/app-framework/wgl/app/wa-inc/lvgl/test.c index ceafbd512..2262aa551 100644 --- a/core/app-framework/wgl/app/wa-inc/lvgl/test.c +++ b/core/app-framework/wgl/app/wa-inc/lvgl/test.c @@ -1,11 +1,9 @@ #include "lvgl.h" - -int main() +int +main() { - - return 0; - + return 0; } diff --git a/core/app-framework/wgl/native/gui_native_api.h b/core/app-framework/wgl/native/gui_native_api.h index f61d9b51a..ee91b0eaa 100644 --- a/core/app-framework/wgl/native/gui_native_api.h +++ b/core/app-framework/wgl/native/gui_native_api.h @@ -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_ */ diff --git a/core/app-framework/wgl/native/wgl.h b/core/app-framework/wgl/native/wgl.h index dc01191c2..ad5843115 100644 --- a/core/app-framework/wgl/native/wgl.h +++ b/core/app-framework/wgl/native/wgl.h @@ -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 } diff --git a/core/app-framework/wgl/native/wgl_btn_wrapper.c b/core/app-framework/wgl/native/wgl_btn_wrapper.c index dbe622476..4c3a23239 100644 --- a/core/app-framework/wgl/native/wgl_btn_wrapper.c +++ b/core/app-framework/wgl/native/wgl_btn_wrapper.c @@ -19,7 +19,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_create_wrapper) wgl_native_get_arg(uint32, copy_obj_id); wasm_module_inst_t module_inst = get_module_inst(exec_env); - res = wgl_native_wigdet_create(WIDGET_TYPE_BTN, par_obj_id, copy_obj_id, module_inst); + res = wgl_native_wigdet_create(WIDGET_TYPE_BTN, par_obj_id, copy_obj_id, + module_inst); wgl_native_set_return(res); } @@ -131,33 +132,30 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_btn_toggle_wrapper) lv_btn_toggle(btn); } +/* clang-format off */ static WGLNativeFuncDef btn_native_func_defs[] = { - { BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, 2, false }, - { BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, 2, true }, - { BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, 2, true }, - { BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, 2, true }, - { BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, 2, true }, - { BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, 2, true }, - { BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, 1, true }, - { BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, 1, true }, - { BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, 1, true }, - { BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, 1, true }, - { BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, 1, true }, - { BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true }, - + { BTN_FUNC_ID_CREATE, lv_btn_create_wrapper, 2, false }, + { BTN_FUNC_ID_SET_TOGGLE, lv_btn_set_toggle_wrapper, 2, true }, + { BTN_FUNC_ID_SET_STATE, lv_btn_set_state_wrapper, 2, true }, + { BTN_FUNC_ID_SET_INK_IN_TIME, lv_btn_set_ink_in_time_wrapper, 2, true }, + { BTN_FUNC_ID_SET_INK_OUT_TIME, lv_btn_set_ink_out_time_wrapper, 2, true }, + { BTN_FUNC_ID_SET_INK_WAIT_TIME, lv_btn_set_ink_wait_time_wrapper, 2, true }, + { BTN_FUNC_ID_GET_INK_IN_TIME, lv_btn_get_ink_in_time_wrapper, 1, true }, + { BTN_FUNC_ID_GET_INK_OUT_TIME, lv_btn_get_ink_out_time_wrapper, 1, true }, + { BTN_FUNC_ID_GET_INK_WAIT_TIME, lv_btn_get_ink_wait_time_wrapper, 1, true }, + { BTN_FUNC_ID_GET_STATE, lv_btn_get_state_wrapper, 1, true }, + { BTN_FUNC_ID_GET_TOGGLE, lv_btn_get_toggle_wrapper, 1, true }, + { BTN_FUNC_ID_TOGGLE, lv_btn_toggle_wrapper, 1, true }, }; +/* clang-format on */ /*************** Native Interface to Wasm App ***********/ void -wasm_btn_native_call(wasm_exec_env_t exec_env, - int32 func_id, uint32 *argv, uint32 argc) +wasm_btn_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv, + uint32 argc) { uint32 size = sizeof(btn_native_func_defs) / sizeof(WGLNativeFuncDef); - wgl_native_func_call(exec_env, - btn_native_func_defs, - size, - func_id, - argv, + wgl_native_func_call(exec_env, btn_native_func_defs, size, func_id, argv, argc); } diff --git a/core/app-framework/wgl/native/wgl_cb_wrapper.c b/core/app-framework/wgl/native/wgl_cb_wrapper.c index e306b1045..51510b997 100644 --- a/core/app-framework/wgl/native/wgl_cb_wrapper.c +++ b/core/app-framework/wgl/native/wgl_cb_wrapper.c @@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_create_wrapper) wgl_native_get_arg(uint32, copy_obj_id); wasm_module_inst_t module_inst = get_module_inst(exec_env); - res = wgl_native_wigdet_create(WIDGET_TYPE_CB, par_obj_id, copy_obj_id, module_inst); + res = wgl_native_wigdet_create(WIDGET_TYPE_CB, par_obj_id, copy_obj_id, + module_inst); wgl_native_set_return(res); } @@ -63,7 +64,7 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_length_wrapper) (void)exec_env; text = lv_cb_get_text(cb); - wgl_native_set_return(text ? strlen(text): 0); + wgl_native_set_return(text ? strlen(text) : 0); } DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper) @@ -89,24 +90,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_cb_get_text_wrapper) } static WGLNativeFuncDef cb_native_func_defs[] = { - { CB_FUNC_ID_CREATE, lv_cb_create_wrapper, 2, false }, - { CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, 3, true }, - { CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, 3, true }, - { CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, 1, true }, - { CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, 3, true }, + { CB_FUNC_ID_CREATE, lv_cb_create_wrapper, 2, false }, + { CB_FUNC_ID_SET_TEXT, lv_cb_set_text_wrapper, 3, true }, + { CB_FUNC_ID_SET_STATIC_TEXT, lv_cb_set_static_text_wrapper, 3, true }, + { CB_FUNC_ID_GET_TEXT_LENGTH, lv_cb_get_text_length_wrapper, 1, true }, + { CB_FUNC_ID_GET_TEXT, lv_cb_get_text_wrapper, 3, true }, }; /*************** Native Interface to Wasm App ***********/ void -wasm_cb_native_call(wasm_exec_env_t exec_env, - int32 func_id, uint32 *argv, uint32 argc) +wasm_cb_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv, + uint32 argc) { uint32 size = sizeof(cb_native_func_defs) / sizeof(WGLNativeFuncDef); - wgl_native_func_call(exec_env, - cb_native_func_defs, - size, - func_id, - argv, + wgl_native_func_call(exec_env, cb_native_func_defs, size, func_id, argv, argc); } diff --git a/core/app-framework/wgl/native/wgl_label_wrapper.c b/core/app-framework/wgl/native/wgl_label_wrapper.c index c1327e4db..dbdefec14 100644 --- a/core/app-framework/wgl/native/wgl_label_wrapper.c +++ b/core/app-framework/wgl/native/wgl_label_wrapper.c @@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_create_wrapper) wgl_native_get_arg(uint32, copy_obj_id); wasm_module_inst_t module_inst = get_module_inst(exec_env); - res = wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par_obj_id, copy_obj_id, module_inst); + res = wgl_native_wigdet_create(WIDGET_TYPE_LABEL, par_obj_id, copy_obj_id, + module_inst); wgl_native_set_return(res); } @@ -73,24 +74,22 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_label_get_text_wrapper) wgl_native_set_return(buffer_offset); } +/* clang-format off */ static WGLNativeFuncDef label_native_func_defs[] = { - { LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, 2, false }, - { LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, 3, true }, - { LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, 1, true }, - { LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, 3, true }, + { LABEL_FUNC_ID_CREATE, lv_label_create_wrapper, 2, false }, + { LABEL_FUNC_ID_SET_TEXT, lv_label_set_text_wrapper, 3, true }, + { LABEL_FUNC_ID_GET_TEXT_LENGTH, lv_label_get_text_length_wrapper, 1, true }, + { LABEL_FUNC_ID_GET_TEXT, lv_label_get_text_wrapper, 3, true }, }; +/* clang-format on */ /*************** Native Interface to Wasm App ***********/ void -wasm_label_native_call(wasm_exec_env_t exec_env, - int32 func_id, uint32 *argv, uint32 argc) +wasm_label_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv, + uint32 argc) { uint32 size = sizeof(label_native_func_defs) / sizeof(WGLNativeFuncDef); - wgl_native_func_call(exec_env, - label_native_func_defs, - size, - func_id, - argv, + wgl_native_func_call(exec_env, label_native_func_defs, size, func_id, argv, argc); } diff --git a/core/app-framework/wgl/native/wgl_list_wrapper.c b/core/app-framework/wgl/native/wgl_list_wrapper.c index 926573ec6..c77f44930 100644 --- a/core/app-framework/wgl/native/wgl_list_wrapper.c +++ b/core/app-framework/wgl/native/wgl_list_wrapper.c @@ -20,7 +20,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_create_wrapper) wgl_native_get_arg(uint32, copy_obj_id); wasm_module_inst_t module_inst = get_module_inst(exec_env); - res = wgl_native_wigdet_create(WIDGET_TYPE_LIST, par_obj_id, copy_obj_id, module_inst); + res = wgl_native_wigdet_create(WIDGET_TYPE_LIST, par_obj_id, copy_obj_id, + module_inst); wgl_native_set_return(res); } @@ -49,7 +50,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper) bh_assert(mod_id != ID_NONE); if (!wgl_native_add_object(btn, mod_id, &btn_obj_id)) { - wasm_runtime_set_exception(module_inst, "add button to object list fail."); + wasm_runtime_set_exception(module_inst, + "add button to object list fail."); return; } @@ -57,21 +59,17 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_list_add_btn_wrapper) } static WGLNativeFuncDef list_native_func_defs[] = { - { LIST_FUNC_ID_CREATE, lv_list_create_wrapper, 2, false }, - { LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, 3, true }, + { LIST_FUNC_ID_CREATE, lv_list_create_wrapper, 2, false }, + { LIST_FUNC_ID_ADD_BTN, lv_list_add_btn_wrapper, 3, true }, }; /*************** Native Interface to Wasm App ***********/ void -wasm_list_native_call(wasm_exec_env_t exec_env, - int32 func_id, uint32 *argv, uint32 argc) +wasm_list_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv, + uint32 argc) { uint32 size = sizeof(list_native_func_defs) / sizeof(WGLNativeFuncDef); - wgl_native_func_call(exec_env, - list_native_func_defs, - size, - func_id, - argv, + wgl_native_func_call(exec_env, list_native_func_defs, size, func_id, argv, argc); } diff --git a/core/app-framework/wgl/native/wgl_native_utils.c b/core/app-framework/wgl/native/wgl_native_utils.c index 081a31a28..6683fff97 100644 --- a/core/app-framework/wgl/native/wgl_native_utils.c +++ b/core/app-framework/wgl/native/wgl_native_utils.c @@ -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!"); } - diff --git a/core/app-framework/wgl/native/wgl_native_utils.h b/core/app-framework/wgl/native/wgl_native_utils.h index 1150f0120..f550dcc71 100644 --- a/core/app-framework/wgl/native/wgl_native_utils.h +++ b/core/app-framework/wgl/native/wgl_native_utils.h @@ -15,21 +15,22 @@ extern "C" { #include "wasm_export.h" #include "bi-inc/wgl_shared_utils.h" -#define wgl_native_return_type(type) type *wgl_ret = (type*)(args_ret) -#define wgl_native_get_arg(type, name) type name = *((type*)(args++)) +#define wgl_native_return_type(type) type *wgl_ret = (type *)(args_ret) +#define wgl_native_get_arg(type, name) type name = *((type *)(args++)) #define wgl_native_set_return(val) *wgl_ret = (val) -#define DEFINE_WGL_NATIVE_WRAPPER(func_name) \ -static void func_name(wasm_exec_env_t exec_env, uint64 *args, uint32 *args_ret) +#define DEFINE_WGL_NATIVE_WRAPPER(func_name) \ + static void func_name(wasm_exec_env_t exec_env, uint64 *args, \ + uint32 *args_ret) enum { - WIDGET_TYPE_BTN, - WIDGET_TYPE_LABEL, - WIDGET_TYPE_CB, - WIDGET_TYPE_LIST, - WIDGET_TYPE_DDLIST, + WIDGET_TYPE_BTN, + WIDGET_TYPE_LABEL, + WIDGET_TYPE_CB, + WIDGET_TYPE_LIST, + WIDGET_TYPE_DDLIST, - _WIDGET_TYPE_NUM, + _WIDGET_TYPE_NUM, }; typedef struct WGLNativeFuncDef { @@ -46,21 +47,19 @@ typedef struct WGLNativeFuncDef { bool check_obj; } WGLNativeFuncDef; -bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj); +bool +wgl_native_validate_object(int32 obj_id, lv_obj_t **obj); -bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id); +bool +wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id); -uint32 wgl_native_wigdet_create(int8 widget_type, - uint32 par_obj_id, - uint32 copy_obj_id, - wasm_module_inst_t module_inst); +uint32 +wgl_native_wigdet_create(int8 widget_type, uint32 par_obj_id, + uint32 copy_obj_id, wasm_module_inst_t module_inst); -void wgl_native_func_call(wasm_exec_env_t exec_env, - WGLNativeFuncDef *funcs, - uint32 size, - int32 func_id, - uint32 *argv, - uint32 argc); +void +wgl_native_func_call(wasm_exec_env_t exec_env, WGLNativeFuncDef *funcs, + uint32 size, int32 func_id, uint32 *argv, uint32 argc); #ifdef __cplusplus } diff --git a/core/app-framework/wgl/native/wgl_obj_wrapper.c b/core/app-framework/wgl/native/wgl_obj_wrapper.c index 801c55d2c..41f605b48 100644 --- a/core/app-framework/wgl/native/wgl_obj_wrapper.c +++ b/core/app-framework/wgl/native/wgl_obj_wrapper.c @@ -10,7 +10,6 @@ #include "wgl_native_utils.h" #include "wgl.h" - typedef struct { bh_list_link l; @@ -42,24 +41,24 @@ static korp_mutex task_handler_lock; static korp_cond task_handler_cond; -static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg) +static void +app_mgr_object_event_callback(module_data *m_data, bh_message_t msg) { uint32 argv[2]; wasm_function_inst_t func_on_object_event; bh_assert(WIDGET_EVENT_WASM == bh_message_type(msg)); - wasm_data *wasm_app_data = (wasm_data*)m_data->internal_data; + wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data; wasm_module_inst_t inst = wasm_app_data->wasm_module_inst; - object_event_t *object_event - = (object_event_t *)bh_message_payload(msg); + object_event_t *object_event = (object_event_t *)bh_message_payload(msg); if (object_event == NULL) return; - func_on_object_event = wasm_runtime_lookup_function(inst, "_on_widget_event", - "(i32i32)"); + func_on_object_event = + wasm_runtime_lookup_function(inst, "_on_widget_event", "(i32i32)"); if (!func_on_object_event) - func_on_object_event = wasm_runtime_lookup_function(inst, "on_widget_event", - "(i32i32)"); + func_on_object_event = + wasm_runtime_lookup_function(inst, "on_widget_event", "(i32i32)"); if (!func_on_object_event) { printf("Cannot find function on_widget_event\n"); return; @@ -71,14 +70,14 @@ static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg) 2, argv)) { const char *exception = wasm_runtime_get_exception(inst); bh_assert(exception); - printf(":Got exception running wasm code: %s\n", - exception); + printf(":Got exception running wasm code: %s\n", exception); wasm_runtime_clear_exception(inst); return; } } -static void cleanup_object_list(uint32 module_id) +static void +cleanup_object_list(uint32 module_id) { object_node_t *elem; @@ -89,8 +88,8 @@ static void cleanup_object_list(uint32 module_id) elem = (object_node_t *)bh_list_first_elem(&g_object_list); while (elem) { /* delete the leaf node belongs to the module firstly */ - if (module_id == elem->module_id && - lv_obj_count_children(elem->obj) == 0) { + if (module_id == elem->module_id + && lv_obj_count_children(elem->obj) == 0) { object_node_t *next = (object_node_t *)bh_list_elem_next(elem); found = true; @@ -98,7 +97,8 @@ static void cleanup_object_list(uint32 module_id) bh_list_remove(&g_object_list, elem); wasm_runtime_free(elem); elem = next; - } else { + } + else { elem = (object_node_t *)bh_list_elem_next(elem); } } @@ -110,7 +110,8 @@ static void cleanup_object_list(uint32 module_id) os_mutex_unlock(&g_object_list_mutex); } -static bool init_object_event_callback_framework() +static bool +init_object_event_callback_framework() { if (!wasm_register_cleanup_callback(cleanup_object_list)) { goto fail; @@ -127,7 +128,8 @@ fail: return false; } -bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj) +bool +wgl_native_validate_object(int32 obj_id, lv_obj_t **obj) { object_node_t *elem; @@ -141,7 +143,7 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj) os_mutex_unlock(&g_object_list_mutex); return true; } - elem = (object_node_t *) bh_list_elem_next(elem); + elem = (object_node_t *)bh_list_elem_next(elem); } os_mutex_unlock(&g_object_list_mutex); @@ -149,11 +151,12 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj) return false; } -bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id) +bool +wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id) { object_node_t *node; - node = (object_node_t *) wasm_runtime_malloc(sizeof(object_node_t)); + node = (object_node_t *)wasm_runtime_malloc(sizeof(object_node_t)); if (node == NULL) return false; @@ -178,11 +181,12 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id) return true; } -static void _obj_del_recursive(lv_obj_t *obj) +static void +_obj_del_recursive(lv_obj_t *obj) { object_node_t *elem; - lv_obj_t * i; - lv_obj_t * i_next; + lv_obj_t *i; + lv_obj_t *i_next; i = lv_ll_get_head(&(obj->child_ll)); @@ -207,16 +211,17 @@ static void _obj_del_recursive(lv_obj_t *obj) os_mutex_unlock(&g_object_list_mutex); return; } - elem = (object_node_t *) bh_list_elem_next(elem); + elem = (object_node_t *)bh_list_elem_next(elem); } os_mutex_unlock(&g_object_list_mutex); } -static void _obj_clean_recursive(lv_obj_t *obj) +static void +_obj_clean_recursive(lv_obj_t *obj) { - lv_obj_t * i; - lv_obj_t * i_next; + lv_obj_t *i; + lv_obj_t *i_next; i = lv_ll_get_head(&(obj->child_ll)); @@ -232,7 +237,8 @@ static void _obj_clean_recursive(lv_obj_t *obj) } } -static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t event) +static void +post_widget_msg_to_module(object_node_t *object_node, lv_event_t event) { module_data *module = module_data_list_lookup_id(object_node->module_id); object_event_t *object_event; @@ -248,13 +254,12 @@ static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t eve object_event->obj_id = object_node->obj_id; object_event->event = event; - bh_post_msg(module->queue, - WIDGET_EVENT_WASM, - object_event, + bh_post_msg(module->queue, WIDGET_EVENT_WASM, object_event, sizeof(*object_event)); } -static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event) +static void +internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event) { object_node_t *elem; @@ -267,13 +272,14 @@ static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event) os_mutex_unlock(&g_object_list_mutex); return; } - elem = (object_node_t *) bh_list_elem_next(elem); + elem = (object_node_t *)bh_list_elem_next(elem); } os_mutex_unlock(&g_object_list_mutex); } -static void* lv_task_handler_thread_routine (void *arg) +static void * +lv_task_handler_thread_routine(void *arg) { os_mutex_lock(&task_handler_lock); @@ -287,7 +293,8 @@ static void* lv_task_handler_thread_routine (void *arg) return NULL; } -void wgl_init(void) +void +wgl_init(void) { korp_tid tid; @@ -306,13 +313,12 @@ void wgl_init(void) init_object_event_callback_framework(); /* new a thread, call lv_task_handler periodically */ - os_thread_create(&tid, - lv_task_handler_thread_routine, - NULL, + os_thread_create(&tid, lv_task_handler_thread_routine, NULL, BH_APPLET_PRESERVED_STACK_SIZE); } -void wgl_exit(void) +void +wgl_exit(void) { lv_task_handler_thread_run = false; os_cond_destroy(&task_handler_cond); @@ -371,7 +377,8 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_obj_align_wrapper) /* validate the base object id if not equal to 0 */ if (base_obj_id != 0 && !wgl_native_validate_object(base_obj_id, &base)) { - wasm_runtime_set_exception(module_inst, "align with invalid base object."); + wasm_runtime_set_exception(module_inst, + "align with invalid base object."); return; } @@ -388,24 +395,20 @@ DEFINE_WGL_NATIVE_WRAPPER(lv_obj_set_event_cb_wrapper) /* ------------------------------------------------------------------------- */ static WGLNativeFuncDef obj_native_func_defs[] = { - { OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, 1, true }, - { OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, 1, true }, - { OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, 1, true }, - { OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, 5, true }, - { OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, 1, true }, + { OBJ_FUNC_ID_DEL, lv_obj_del_wrapper, 1, true }, + { OBJ_FUNC_ID_DEL_ASYNC, lv_obj_del_async_wrapper, 1, true }, + { OBJ_FUNC_ID_CLEAN, lv_obj_clean_wrapper, 1, true }, + { OBJ_FUNC_ID_ALIGN, lv_obj_align_wrapper, 5, true }, + { OBJ_FUNC_ID_SET_EVT_CB, lv_obj_set_event_cb_wrapper, 1, true }, }; /*************** Native Interface to Wasm App ***********/ void -wasm_obj_native_call(wasm_exec_env_t exec_env, - int32 func_id, uint32 *argv, uint32 argc) +wasm_obj_native_call(wasm_exec_env_t exec_env, int32 func_id, uint32 *argv, + uint32 argc) { uint32 size = sizeof(obj_native_func_defs) / sizeof(WGLNativeFuncDef); - wgl_native_func_call(exec_env, - obj_native_func_defs, - size, - func_id, - argv, + wgl_native_func_call(exec_env, obj_native_func_defs, size, func_id, argv, argc); } diff --git a/core/app-mgr/app-manager/app_manager.c b/core/app-mgr/app-manager/app_manager.c index daf5bca64..6cf88cd44 100644 --- a/core/app-mgr/app-manager/app_manager.c +++ b/core/app-mgr/app-manager/app_manager.c @@ -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; } @@ -140,10 +144,9 @@ static bool app_manager_query_applets(request_t *msg, const char *name) i++; 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: " - "set attr container key failed."); + m_data->module_name))) { + SEND_ERR_RESPONSE(msg->mid, "Query Applets failed: " + "set attr container key failed."); goto fail; } snprintf(buf, sizeof(buf), "%s%d", "heap", i); @@ -157,13 +160,13 @@ static bool app_manager_query_applets(request_t *msg, const char *name) else if (!strcmp(name, m_data->module_name)) { found = true; if (!(attr_container_set_string(&attr_cont, "name", - m_data->module_name))) { - SEND_ERR_RESPONSE(msg->mid, - "Query Applet failed: " - "set attr container key failed."); + m_data->module_name))) { + 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."); @@ -176,15 +179,15 @@ static bool app_manager_query_applets(request_t *msg, const char *name) if (name != NULL && !found) { SEND_ERR_RESPONSE(msg->mid, - "Query Applet failed: the app is not found."); + "Query Applet failed: the app is not found."); goto fail; } 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,13 +220,14 @@ 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 }; find_key_value(kv_str, strlen(kv_str), "type", type_str, - sizeof(type_str) - 1, '&'); + sizeof(type_str) - 1, '&'); if (strlen(type_str) == 0) module_type = Module_WASM_App; @@ -240,34 +245,37 @@ 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")) - > 0) { + if ((offset = + check_url_start(request->url, strlen(request->url), "/applet")) + > 0) { module_type = get_module_type(request->url + offset); if (module_type == -1) { SEND_ERR_RESPONSE(mid, - "Applet Management failed: invalid module type."); + "Applet Management failed: invalid module type."); goto fail; } /* Install Applet */ if (request->action == COAP_PUT) { if (get_applets_count() >= MAX_APP_INSTALLATIONS) { - SEND_ERR_RESPONSE(mid, - "Install Applet failed: exceed max app installations."); + SEND_ERR_RESPONSE( + mid, + "Install Applet failed: exceed max app installations."); goto fail; } if (!request->payload) { SEND_ERR_RESPONSE(mid, - "Install Applet failed: invalid payload."); + "Install Applet failed: invalid payload."); goto fail; } if (g_module_interfaces[module_type] @@ -280,14 +288,15 @@ static void app_manager_queue_callback(void *message, void *arg) else if (request->action == COAP_DELETE) { 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)) + && g_module_interfaces[module_type]->module_uninstall) { + 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(); @@ -390,21 +401,20 @@ fail1: module_interface *g_module_interfaces[Module_Max] = { #if ENABLE_MODULE_JEFF != 0 - &jeff_module_interface, + &jeff_module_interface, #else - NULL, + NULL, #endif #if ENABLE_MODULE_WASM_APP != 0 - &wasm_app_module_interface, + &wasm_app_module_interface, #else - NULL, + NULL, #endif #if ENABLE_MODULE_WASM_LIB != 0 - &wasm_lib_module_interface + &wasm_lib_module_interface #else - NULL + NULL #endif }; - diff --git a/core/app-mgr/app-manager/app_manager.h b/core/app-mgr/app-manager/app_manager.h index fb2a42f71..ce83bd170 100644 --- a/core/app-mgr/app-manager/app_manager.h +++ b/core/app-mgr/app-manager/app_manager.h @@ -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 { \ - app_manager_printf("%s\n", err_msg); \ - send_error_response_to_host(mid, INTERNAL_SERVER_ERROR_5_00, err_msg); \ -} while (0) +#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) extern module_interface *g_module_interfaces[Module_Max]; @@ -49,14 +50,15 @@ 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*), - watchdog_timer *wd_timer); +void * +app_manager_timer_create(void (*timer_callback)(void *), + watchdog_timer *wd_timer); void app_manager_timer_destroy(void *timer); @@ -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); + 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 - diff --git a/core/app-mgr/app-manager/app_manager_host.c b/core/app-mgr/app-manager/app_manager_host.c index c7429da2a..6b1c3ba45 100644 --- a/core/app-mgr/app-manager/app_manager_host.c +++ b/core/app-mgr/app-manager/app_manager_host.c @@ -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; @@ -158,7 +159,7 @@ static int on_imrt_link_byte_arrive(unsigned char ch, recv_context_t *ctx) if (ctx->message.message_type == INSTALL_WASM_APP) { int received_size; module_on_install_request_byte_arrive_func module_on_install = - g_module_interfaces[Module_WASM_App]->module_on_install; + g_module_interfaces[Module_WASM_App]->module_on_install; ctx->size_in_phase++; @@ -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)); @@ -267,12 +272,13 @@ bool app_manager_host_init(host_interface *interface) host_commu.destroy = interface->destroy; if (host_commu.init != NULL) - return host_commu.init(); + return host_commu.init(); 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); diff --git a/core/app-mgr/app-manager/app_manager_host.h b/core/app-mgr/app-manager/app_manager_host.h index cc663725b..b19404f91 100644 --- a/core/app-mgr/app-manager/app_manager_host.h +++ b/core/app-mgr/app-manager/app_manager_host.h @@ -12,7 +12,7 @@ extern "C" { #endif -#define HOST_MODE_AON 1 +#define HOST_MODE_AON 1 #define HOST_MODE_UART 2 #define HOST_MODE_TEST 3 @@ -21,4 +21,3 @@ extern "C" { #endif #endif - diff --git a/core/app-mgr/app-manager/ble_msg.c b/core/app-mgr/app-manager/ble_msg.c index 179eb78ba..1d19dddaf 100644 --- a/core/app-mgr/app-manager/ble_msg.c +++ b/core/app-mgr/app-manager/ble_msg.c @@ -5,7 +5,7 @@ #if 0 -#define BLUETOOTH_INTERFACE_ADVERTISMENT_DATA_LENGTH 31 +#define BLUETOOTH_INTERFACE_ADVERTISMENT_DATA_LENGTH 31 /* ble_device_info */ typedef struct ble_device_info { diff --git a/core/app-mgr/app-manager/event.c b/core/app-mgr/app-manager/event.c index f2c49dfd0..a21065fab 100644 --- a/core/app-mgr/app-manager/event.c +++ b/core/app-mgr/app-manager/event.c @@ -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,8 +80,8 @@ bool am_register_event(const char *url, uint32_t reg_client) if (current == NULL) { if (NULL - == (current = (event_reg_t *) APP_MGR_MALLOC( - offsetof(event_reg_t, url) + strlen(url) + 1))) { + == (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; @@ -100,29 +105,30 @@ bool am_register_event(const char *url, uint32_t reg_client) s->next = current->subscribers; current->subscribers = s; app_manager_printf("client: %d registered event (%s)\n", reg_client, - url); + url); } return true; } // @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); + reg_client, current->url); } // remove the registration if no client subscribe it if (current->subscribers == NULL) { app_manager_printf("unregister for event deleted url:(%s)\n", - current->url); + current->url); if (pre) pre->next = next; else @@ -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, - uint32_t reg_client) +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; diff --git a/core/app-mgr/app-manager/event.h b/core/app-mgr/app-manager/event.h index a9c3de3b2..36ced521d 100644 --- a/core/app-mgr/app-manager/event.h +++ b/core/app-mgr/app-manager/event.h @@ -22,7 +22,7 @@ extern "C" { */ bool event_handle_event_request(uint8_t code, const char *event_url, - uint32_t register); + uint32_t register); /** * Test whether the event is registered diff --git a/core/app-mgr/app-manager/message.c b/core/app-mgr/app-manager/message.c index e9c594ec2..aac7a2364 100644 --- a/core/app-mgr/app-manager/message.c +++ b/core/app-mgr/app-manager/message.c @@ -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; } - diff --git a/core/app-mgr/app-manager/module_jeff.c b/core/app-mgr/app-manager/module_jeff.c index 7e1a8be34..7c7f9510d 100644 --- a/core/app-mgr/app-manager/module_jeff.c +++ b/core/app-mgr/app-manager/module_jeff.c @@ -45,7 +45,7 @@ typedef struct jeff_applet_data { JeffFileHeaderLinked *main_file; /* Permissions of the Java Applet */ char *perms; -}jeff_applet_data; +} jeff_applet_data; /* Jeff class com.intel.aee.AEEApplet */ static JeffClassHeaderLinked *class_AEEApplet; @@ -113,9 +113,8 @@ app_manager_get_tool_agent_queue() static bool is_tool_agent_running(module_data *m_data) { - jeff_applet_data *applet_data = (jeff_applet_data*)m_data->internal_data; - return (applet_data->debug_mode - && applet_data->tool_agent_queue + jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data; + return (applet_data->debug_mode && applet_data->tool_agent_queue && applet_data->vm_instance->tool_agent); } #endif @@ -123,12 +122,12 @@ is_tool_agent_running(module_data *m_data) static char * get_class_qname(const JeffString *pname, const JeffString *cname) { - unsigned int length = pname->length ? pname->length + 2 + cname->length - : cname->length + 1; + unsigned int length = + pname->length ? pname->length + 2 + cname->length : cname->length + 1; char *buf = APP_MGR_MALLOC(length), *p; if (!buf) - return NULL; + return NULL; p = buf; if (pname->length) { @@ -159,8 +158,8 @@ send_exception_event_to_host(const char *applet_name, const char *exc_name) } if (!attr_container_set_string(&payload, "exception name", exc_name) - || !attr_container_set_string(&payload, "stack trace", "TODO") - || !attr_container_set_string(&payload, "applet name", applet_name)) { + || !attr_container_set_string(&payload, "stack trace", "TODO") + || !attr_container_set_string(&payload, "applet name", applet_name)) { app_manager_printf("Send exception to host fail: set attr"); goto fail; } @@ -184,7 +183,7 @@ send_exception_event_to_host(const char *applet_name, const char *exc_name) APP_MGR_FREE(url); - fail: +fail: attr_container_destroy(payload); } @@ -193,1541 +192,1692 @@ check_exception() { if (jeff_runtime_get_exception()) { jeff_printf("V1.Exception thrown when running applet '%s':\n", - app_manager_get_module_name(Module_Jeff)); + app_manager_get_module_name(Module_Jeff)); jeff_runtime_print_exception(); jeff_printf("\n"); jeff_printf(NULL); + } - if (!app_manager_is_interrupting_module(Module_Jeff)) { - attr_container_t *payload; - int payload_len; - JeffClassHeaderLinked *exc_class = jeff_object_class_pointer(jeff_runtime_get_exception()); - char *qname_buf = get_class_qname(jeff_get_class_pname(exc_class), - jeff_get_class_cname(exc_class)); + if (!app_manager_is_interrupting_module(Module_Jeff)) { + attr_container_t *payload; + int payload_len; + JeffClassHeaderLinked *exc_class = + jeff_object_class_pointer(jeff_runtime_get_exception()); + char *qname_buf = get_class_qname(jeff_get_class_pname(exc_class), + jeff_get_class_cname(exc_class)); - /* Send exception event to host */ - if (qname_buf) { - send_exception_event_to_host(app_manager_get_module_name(Module_Jeff), qname_buf); - APP_MGR_FREE(qname_buf); - } + /* Send exception event to host */ + if (qname_buf) { + send_exception_event_to_host( + app_manager_get_module_name(Module_Jeff), qname_buf); + APP_MGR_FREE(qname_buf); + } - /* Uninstall the applet */ - if ((payload = attr_container_create("uninstall myself"))) { - if (attr_container_set_string(&payload, "name", app_manager_get_module_name(Module_Jeff)) - /* Set special flag to prevent app manager making response since this is an internal message */ - && attr_container_set_bool(&payload, "do not reply me", true)) { - request_t request = {0}; - payload_len = attr_container_get_serialize_length(payload); + /* Uninstall the applet */ + if ((payload = attr_container_create("uninstall myself"))) { + if (attr_container_set_string( + &payload, "name", app_manager_get_module_name(Module_Jeff)) + /* Set special flag to prevent app manager making response + since this is an internal message */ + && attr_container_set_bool(&payload, "do not reply me", true)) { + request_t request = { 0 }; + payload_len = attr_container_get_serialize_length(payload); init_request(request, "/applet", COAP_DELETE, (char *)payload, payload_len)); app_mgr_lookup_resource(&request); // TODO: confirm this is right attr_container_destroy(payload); - } } - - jeff_runtime_set_exception(NULL); - return true; } - return false; + jeff_runtime_set_exception(NULL); + return true; } - static bool - app_manager_initialize_class(JeffClassHeaderLinked *c) - { - jeff_runtime_initialize_class(c); - return !check_exception(); - } + return false; +} - static bool - app_manager_initialize_object(JeffObjectRef obj) - { - jeff_runtime_initialize_object(obj); - return !check_exception(); - } +static bool +app_manager_initialize_class(JeffClassHeaderLinked *c) +{ + jeff_runtime_initialize_class(c); + return !check_exception(); +} - static bool - app_manager_call_java(JeffMethodLinked *method, - unsigned int argc, uint32 argv[], uint8 argt[]) - { - module_data *m_data = app_manager_get_module_data(Module_Jeff); - watchdog_timer *wd_timer = &m_data->wd_timer; - bool is_wd_started = false; +static bool +app_manager_initialize_object(JeffObjectRef obj) +{ + jeff_runtime_initialize_object(obj); + return !check_exception(); +} + +static bool +app_manager_call_java(JeffMethodLinked *method, unsigned int argc, + uint32 argv[], uint8 argt[]) +{ + module_data *m_data = app_manager_get_module_data(Module_Jeff); + watchdog_timer *wd_timer = &m_data->wd_timer; + bool is_wd_started = false; #if BEIHAI_ENABLE_TOOL_AGENT != 0 - /* Only start watchdog when debugger is not running */ - if (!is_tool_agent_running(m_data)) { + /* Only start watchdog when debugger is not running */ + if (!is_tool_agent_running(m_data)) { #endif - watchdog_timer_start(wd_timer); - is_wd_started = true; + watchdog_timer_start(wd_timer); + is_wd_started = true; #if BEIHAI_ENABLE_TOOL_AGENT != 0 - } + } #endif - jeff_runtime_call_java(method, argc, argv, argt); + jeff_runtime_call_java(method, argc, argv, argt); - if (is_wd_started) { - os_mutex_lock(&wd_timer->lock); - if (!wd_timer->is_interrupting) { - wd_timer->is_stopped = true; - watchdog_timer_stop(wd_timer); - } - os_mutex_unlock(&wd_timer->lock); + if (is_wd_started) { + os_mutex_lock(&wd_timer->lock); + if (!wd_timer->is_interrupting) { + wd_timer->is_stopped = true; + watchdog_timer_stop(wd_timer); } - - return !check_exception(); + os_mutex_unlock(&wd_timer->lock); } - static AEEBLEDevice - create_object_BLEDevice(ble_device_info *dev_info) - { - JeffLocalObjectRef ref; - AEEBLEDevice dev_struct; + return !check_exception(); +} - jeff_runtime_push_local_object_ref(&ref); +static AEEBLEDevice +create_object_BLEDevice(ble_device_info *dev_info) +{ + JeffLocalObjectRef ref; + AEEBLEDevice dev_struct; - ref.val = jeff_runtime_new_object(class_BLEDevice); + jeff_runtime_push_local_object_ref(&ref); - if (!ref.val) { - jeff_runtime_pop_local_object_ref(1); - return NULL; - } + ref.val = jeff_runtime_new_object(class_BLEDevice); - dev_struct = (AEEBLEDevice) (ref.val); - dev_struct->rssi = dev_info->rssi; - dev_struct->mac = (jbyteArray) jeff_runtime_create_byte_array((int8 *)dev_info->mac, 6); - - app_manager_printf("adv_data_len:%d,scan_response_len:%d\n", dev_info->adv_data_len, dev_info->scan_response_len); - - dev_struct->advData = (jbyteArray) jeff_runtime_create_byte_array((int8 *)dev_info->adv_data, dev_info->adv_data_len); - dev_struct->scanResponse = (jbyteArray) jeff_runtime_create_byte_array((int8 *)dev_info->scan_response, dev_info->scan_response_len); - dev_struct->addressType = dev_info->address_type; - jeff_runtime_initialize_object(ref.val); + if (!ref.val) { jeff_runtime_pop_local_object_ref(1); - if ((dev_struct->mac == NULL) || (dev_struct->advData == NULL) || (dev_struct->scanResponse == NULL)) { - return NULL; - } - return (AEEBLEDevice) ref.val; + return NULL; } - static void - app_instance_process_ble_msg(char *msg) - { - bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg; - unsigned int argv[5]; - uint8 argt[5]; + dev_struct = (AEEBLEDevice)(ref.val); + dev_struct->rssi = dev_info->rssi; + dev_struct->mac = + (jbyteArray)jeff_runtime_create_byte_array((int8 *)dev_info->mac, 6); - ble_device_info *dev_info; + app_manager_printf("adv_data_len:%d,scan_response_len:%d\n", + dev_info->adv_data_len, dev_info->scan_response_len); - dev_info = (ble_device_info *) ble_msg->payload; - AEEBLEDevice ble_dev; + dev_struct->advData = (jbyteArray)jeff_runtime_create_byte_array( + (int8 *)dev_info->adv_data, dev_info->adv_data_len); + dev_struct->scanResponse = (jbyteArray)jeff_runtime_create_byte_array( + (int8 *)dev_info->scan_response, dev_info->scan_response_len); + dev_struct->addressType = dev_info->address_type; + jeff_runtime_initialize_object(ref.val); + jeff_runtime_pop_local_object_ref(1); + if ((dev_struct->mac == NULL) || (dev_struct->advData == NULL) + || (dev_struct->scanResponse == NULL)) { + return NULL; + } + return (AEEBLEDevice)ref.val; +} - argv[0] = (unsigned int) (jbyteArray) jeff_runtime_create_byte_array((int8 *)dev_info->mac, 6); - argt[0] = 1; - if (!app_manager_call_java(method_callOnBLEManagerGetBLEDevice, 1, argv, argt)) { - app_manager_printf("app_manager_call_java BLEManagerGetBLEDevice fail error\n"); +static void +app_instance_process_ble_msg(char *msg) +{ + bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg; + unsigned int argv[5]; + uint8 argt[5]; + + ble_device_info *dev_info; + + dev_info = (ble_device_info *)ble_msg->payload; + AEEBLEDevice ble_dev; + + argv[0] = (unsigned int)(jbyteArray)jeff_runtime_create_byte_array( + (int8 *)dev_info->mac, 6); + argt[0] = 1; + if (!app_manager_call_java(method_callOnBLEManagerGetBLEDevice, 1, argv, + argt)) { + app_manager_printf( + "app_manager_call_java BLEManagerGetBLEDevice fail error\n"); + goto fail; + } + ble_dev = (AEEBLEDevice)argv[0]; + if (ble_dev == NULL) { + ble_dev = create_object_BLEDevice(dev_info); + if (ble_dev == NULL) { goto fail; } - ble_dev = (AEEBLEDevice) argv[0]; - if (ble_dev == NULL) { - ble_dev = create_object_BLEDevice(dev_info); - if (ble_dev == NULL) { + } + + switch (ble_msg->type) { + case BLE_SUB_EVENT_DISCOVERY: + { + argv[0] = (unsigned int)ble_dev; + argt[0] = 1; + ble_dev->rssi = dev_info->rssi; + if (!app_manager_call_java(method_callOnBLEStartDiscovery, 1, argv, + argt)) { + app_manager_printf( + "app_manager_call_java method_callOnBLEStartDiscovery " + "fail error\n"); goto fail; } + break; } - switch (ble_msg->type) { - case BLE_SUB_EVENT_DISCOVERY: - { - argv[0] = (unsigned int) ble_dev; + case BLE_SUB_EVENT_CONNECTED: + { + if (ble_dev) { + argv[0] = (unsigned int)ble_dev; + argv[1] = 0; argt[0] = 1; - ble_dev->rssi = dev_info->rssi; - if (!app_manager_call_java(method_callOnBLEStartDiscovery, 1, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLEStartDiscovery fail error\n"); + argt[1] = 1; + if (!app_manager_call_java(method_callOnBLEConnected, 2, argv, + argt)) { + app_manager_printf( + "app_manager_call_java method_callOnBLEConnected " + "fail error\n"); goto fail; } } break; + } - case BLE_SUB_EVENT_CONNECTED: - { - if (ble_dev) { - argv[0] = (unsigned int) ble_dev; - argv[1] = 0; - argt[0] = 1; - argt[1] = 1; - if (!app_manager_call_java(method_callOnBLEConnected, 2, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLEConnected fail error\n"); - goto fail; - } + case BLE_SUB_EVENT_DISCONNECTED: + { + app_manager_printf("app instance received disconnected\n"); + + if (ble_dev) { + argv[0] = (unsigned int)ble_dev; + argv[1] = 0; + argt[0] = 1; + argt[1] = 1; + ble_dev->rssi = dev_info->rssi; + if (!app_manager_call_java(method_callOnBLEDisconnected, 2, + argv, argt)) { + app_manager_printf( + "app_manager_call_java " + "method_callOnBLEDisconnected fail error\n"); + goto fail; } } break; - - case BLE_SUB_EVENT_DISCONNECTED: - { - app_manager_printf("app instance received disconnected\n"); - - if (ble_dev) { - argv[0] = (unsigned int) ble_dev; - argv[1] = 0; - argt[0] = 1; - argt[1] = 1; - ble_dev->rssi = dev_info->rssi; - if (!app_manager_call_java(method_callOnBLEDisconnected, 2, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLEDisconnected fail error\n"); - goto fail; - } - } - } - break; - - case BLE_SUB_EVENT_NOTIFICATION: - { - if (ble_dev) { - argv[0] = (unsigned int) ble_dev; - argv[1] = (unsigned int) (jbyteArray) jeff_runtime_create_byte_array( - (int8 *)dev_info->private_data, dev_info->private_data_length); - argv[2] = dev_info->value_handle; - argv[3] = dev_info->ccc_handle; - argt[1] = 1; - argt[2] = 0; - argt[3] = 0; - ble_dev->rssi = dev_info->rssi; - if (!app_manager_call_java(method_callOnBLENotification, 4, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLENotification fail error\n"); - goto fail; - } - } - } - break; - - case BLE_SUB_EVENT_INDICATION: - { - if (ble_dev) { - argv[0] = (unsigned int) ble_dev; - argv[1] = (unsigned int) (jbyteArray) jeff_runtime_create_byte_array( - (int8 *)dev_info->private_data, dev_info->private_data_length); - argv[2] = dev_info->value_handle; - argv[3] = dev_info->ccc_handle; - argt[0] = 1; - argt[1] = 1; - argt[2] = 0; - argt[3] = 0; - ble_dev->rssi = dev_info->rssi; - if (!app_manager_call_java(method_callOnBLEIndication, 4, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLEIndication fail error\n"); - goto fail; - } - } - } - break; - - case BLE_SUB_EVENT_PASSKEYENTRY: - { - - if (ble_dev) { - argv[0] = (unsigned int) ble_dev; - argt[0] = 1; - argt[1] = 1; - ble_dev->rssi = dev_info->rssi; - if (!app_manager_call_java(method_callOnBLEPasskeyEntry, 1, argv, argt)) { - app_manager_printf("app_manager_call_java method_callOnBLEPasskeyEntry fail error\n"); - goto fail; - } - } - } - break; - - case BLE_SUB_EVENT_SECURITY_LEVEL_CHANGE: - { - if (ble_dev) { - ble_dev->securityLevel = dev_info->security_level; - } - } - break; - - default: - break; } - fail: - if (dev_info->scan_response != NULL) { - APP_MGR_FREE(dev_info->scan_response); - } - if (dev_info->private_data != NULL) { - APP_MGR_FREE(dev_info->private_data); + case BLE_SUB_EVENT_NOTIFICATION: + { + if (ble_dev) { + argv[0] = (unsigned int)ble_dev; + argv[1] = + (unsigned int)(jbyteArray)jeff_runtime_create_byte_array( + (int8 *)dev_info->private_data, + dev_info->private_data_length); + argv[2] = dev_info->value_handle; + argv[3] = dev_info->ccc_handle; + argt[1] = 1; + argt[2] = 0; + argt[3] = 0; + ble_dev->rssi = dev_info->rssi; + if (!app_manager_call_java(method_callOnBLENotification, 4, + argv, argt)) { + app_manager_printf( + "app_manager_call_java " + "method_callOnBLENotification fail error\n"); + goto fail; + } + } + break; } - if (dev_info->adv_data != NULL) { - APP_MGR_FREE(dev_info->adv_data); - } - if (dev_info != NULL) { - APP_MGR_FREE(dev_info); + case BLE_SUB_EVENT_INDICATION: + { + if (ble_dev) { + argv[0] = (unsigned int)ble_dev; + argv[1] = + (unsigned int)(jbyteArray)jeff_runtime_create_byte_array( + (int8 *)dev_info->private_data, + dev_info->private_data_length); + argv[2] = dev_info->value_handle; + argv[3] = dev_info->ccc_handle; + argt[0] = 1; + argt[1] = 1; + argt[2] = 0; + argt[3] = 0; + ble_dev->rssi = dev_info->rssi; + if (!app_manager_call_java(method_callOnBLEIndication, 4, argv, + argt)) { + app_manager_printf( + "app_manager_call_java method_callOnBLEIndication " + "fail error\n"); + goto fail; + } + } + break; } + case BLE_SUB_EVENT_PASSKEYENTRY: + { + + if (ble_dev) { + argv[0] = (unsigned int)ble_dev; + argt[0] = 1; + argt[1] = 1; + ble_dev->rssi = dev_info->rssi; + if (!app_manager_call_java(method_callOnBLEPasskeyEntry, 1, + argv, argt)) { + app_manager_printf( + "app_manager_call_java " + "method_callOnBLEPasskeyEntry fail error\n"); + goto fail; + } + } + break; + } + + case BLE_SUB_EVENT_SECURITY_LEVEL_CHANGE: + { + if (ble_dev) { + ble_dev->securityLevel = dev_info->security_level; + } + break; + } + + default: + break; } - static void - app_instance_free_ble_msg(char *msg) - { - bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg; - ble_device_info *dev_info; - - dev_info = (ble_device_info *) ble_msg->payload; - - if (dev_info->scan_response != NULL) +fail: + if (dev_info->scan_response != NULL) { APP_MGR_FREE(dev_info->scan_response); - - if (dev_info->private_data != NULL) + } + if (dev_info->private_data != NULL) { APP_MGR_FREE(dev_info->private_data); + } - if (dev_info->adv_data != NULL) + if (dev_info->adv_data != NULL) { APP_MGR_FREE(dev_info->adv_data); - - if (dev_info != NULL) + } + if (dev_info != NULL) { APP_MGR_FREE(dev_info); } +} - static void - app_instance_queue_free_callback(void *queue_msg) - { - bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; +static void +app_instance_free_ble_msg(char *msg) +{ + bh_queue_ble_sub_msg_t *ble_msg = (bh_queue_ble_sub_msg_t *)msg; + ble_device_info *dev_info; - switch (msg->message_type) { - case APPLET_REQUEST: - { - bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; - APP_MGR_FREE(req_msg); - break; - } + dev_info = (ble_device_info *)ble_msg->payload; - case TIMER_EVENT: - { - break; - } + if (dev_info->scan_response != NULL) + APP_MGR_FREE(dev_info->scan_response); - case SENSOR_EVENT: - { - if (msg->payload) { - bh_sensor_event_t *sensor_event = (bh_sensor_event_t *)msg->payload; - attr_container_t *event = sensor_event->event; + if (dev_info->private_data != NULL) + APP_MGR_FREE(dev_info->private_data); - attr_container_destroy(event); - APP_MGR_FREE(sensor_event); - } - break; - } + if (dev_info->adv_data != NULL) + APP_MGR_FREE(dev_info->adv_data); - case BLE_EVENT: - { - if (msg->payload) { - app_instance_free_ble_msg(msg->payload); - APP_MGR_FREE(msg->payload); - } - break; - } + if (dev_info != NULL) + APP_MGR_FREE(dev_info); +} - case GPIO_INTERRUPT_EVENT: - { - break; - } +static void +app_instance_queue_free_callback(void *queue_msg) +{ + bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; - default: - { - break; - } + switch (msg->message_type) { + case APPLET_REQUEST: + { + bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; + APP_MGR_FREE(req_msg); + break; } - APP_MGR_FREE(msg); + case TIMER_EVENT: + { + break; + } + + case SENSOR_EVENT: + { + if (msg->payload) { + bh_sensor_event_t *sensor_event = + (bh_sensor_event_t *)msg->payload; + attr_container_t *event = sensor_event->event; + + attr_container_destroy(event); + APP_MGR_FREE(sensor_event); + } + break; + } + + case BLE_EVENT: + { + if (msg->payload) { + app_instance_free_ble_msg(msg->payload); + APP_MGR_FREE(msg->payload); + } + break; + } + + case GPIO_INTERRUPT_EVENT: + { + break; + } + + default: + { + break; + } } - static void - app_instance_queue_callback(void *queue_msg) - { - bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; - unsigned int argv[5]; - uint8 argt[5]; + APP_MGR_FREE(msg); +} - if (app_manager_is_interrupting_module(Module_Jeff)) { - app_instance_queue_free_callback(queue_msg); - return; - } +static void +app_instance_queue_callback(void *queue_msg) +{ + bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; + unsigned int argv[5]; + uint8 argt[5]; - switch (msg->message_type) { - case APPLET_REQUEST: - { - JeffLocalObjectRef ref; - AEERequest req_obj; - bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; - attr_container_t *attr_cont = (attr_container_t *)req_msg->payload; - module_data *m_data = app_manager_get_module_data(Module_Jeff); - jeff_applet_data *applet_data = (jeff_applet_data*)m_data->internal_data; + if (app_manager_is_interrupting_module(Module_Jeff)) { + app_instance_queue_free_callback(queue_msg); + return; + } - app_manager_printf("Applet %s got request, url %s, action %d\n", - m_data->module_name, req_msg->url, req_msg->action); + switch (msg->message_type) { + case APPLET_REQUEST: + { + JeffLocalObjectRef ref; + AEERequest req_obj; + bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; + attr_container_t *attr_cont = (attr_container_t *)req_msg->payload; + module_data *m_data = app_manager_get_module_data(Module_Jeff); + jeff_applet_data *applet_data = + (jeff_applet_data *)m_data->internal_data; - /* Create Request object */ - req_obj = (AEERequest)jeff_object_new(m_data->heap, class_AEERequest); - if (!req_obj) { - app_manager_printf("Applet process request failed: create request obj failed.\n"); - goto fail1; - } + app_manager_printf("Applet %s got request, url %s, action %d\n", + m_data->module_name, req_msg->url, + req_msg->action); - jeff_runtime_push_local_object_ref(&ref); - ref.val = (JeffObjectRef)req_obj; + /* Create Request object */ + req_obj = + (AEERequest)jeff_object_new(m_data->heap, class_AEERequest); + if (!req_obj) { + app_manager_printf("Applet process request failed: create " + "request obj failed.\n"); + goto fail1; + } - req_obj->mid = req_msg->mid; - req_obj->action = req_msg->action; - req_obj->fmt = req_msg->fmt; + jeff_runtime_push_local_object_ref(&ref); + ref.val = (JeffObjectRef)req_obj; - /* Create Java url string */ - if (req_msg->url) { - req_obj->url = (jstring)jeff_runtime_create_java_string(req_msg->url); - if (!req_obj->url) { - app_manager_printf("Applet process request failed: create url string failed.\n"); - goto fail2; - } - } + req_obj->mid = req_msg->mid; + req_obj->action = req_msg->action; + req_obj->fmt = req_msg->fmt; - /* Create Java AttributeObject payload */ - if (attr_cont - && !attr_container_to_attr_obj(attr_cont, &req_obj->payload)) { - app_manager_printf("Applet process request failed: convert payload failed.\n"); + /* Create Java url string */ + if (req_msg->url) { + req_obj->url = + (jstring)jeff_runtime_create_java_string(req_msg->url); + if (!req_obj->url) { + app_manager_printf("Applet process request failed: " + "create url string failed.\n"); goto fail2; } - - /* Call AEEApplet.callOnRequest(Request request) method */ - argv[0] = (unsigned int)applet_data->applet_obj; - argv[1] = (unsigned int)req_obj; - argt[0] = argt[1] = 1; - app_manager_call_java(method_AEEApplet_callOnRequest, 2, argv, argt); - app_manager_printf("Applet process request success.\n"); - - fail2: - jeff_runtime_pop_local_object_ref(1); - fail1: - APP_MGR_FREE(req_msg); - break; } - case TIMER_EVENT: - { - if (msg->payload) { - /* Call Timer.callOnTimer() method */ - argv[0] = (unsigned int)msg->payload; - argt[0] = 1; - app_manager_call_java(method_callOnTimer, 1, argv, argt); - } - break; + /* Create Java AttributeObject payload */ + if (attr_cont + && !attr_container_to_attr_obj(attr_cont, &req_obj->payload)) { + app_manager_printf("Applet process request failed: convert " + "payload failed.\n"); + goto fail2; } - case SENSOR_EVENT: - { - if (msg->payload) { - bh_sensor_event_t *sensor_event = (bh_sensor_event_t *)msg->payload; - AEESensor sensor = sensor_event->sensor; - attr_container_t *event = sensor_event->event; - bool ret = attr_container_to_attr_obj(event, &sensor->event); + /* Call AEEApplet.callOnRequest(Request request) method */ + argv[0] = (unsigned int)applet_data->applet_obj; + argv[1] = (unsigned int)req_obj; + argt[0] = argt[1] = 1; + app_manager_call_java(method_AEEApplet_callOnRequest, 2, argv, + argt); + app_manager_printf("Applet process request success.\n"); - attr_container_destroy(event); - APP_MGR_FREE(sensor_event); - - if (ret) { - /* Call Sensor.callOnSensorEvent() method */ - argv[0] = (unsigned int)sensor; - argt[0] = 1; - app_manager_call_java(method_callOnSensorEvent, 1, argv, argt); - } - } - break; - } - - case BLE_EVENT: - { - if (msg->payload) { - app_instance_process_ble_msg(msg->payload); - APP_MGR_FREE(msg->payload); - } - break; - } - - case GPIO_INTERRUPT_EVENT: - { - AEEGPIOChannel gpio_ch = (AEEGPIOChannel)msg->payload; - - if ((gpio_ch == NULL) || (gpio_ch->callback == 0) || (gpio_ch->listener == NULL)) { - break; - } - argv[0] = (unsigned int) gpio_ch; - argt[0] = 1; - bool ret_value = app_manager_call_java(method_callOnGPIOInterrupt, 1, argv, argt); - - if (!ret_value) { - app_manager_printf("app_manager_call_java method_method_callOnGPIOInterrupt return false\n"); - } - break; - } - - default: - { - app_manager_printf("Invalid message type of applet queue message.\n"); - break; - } + fail2: + jeff_runtime_pop_local_object_ref(1); + fail1: + APP_MGR_FREE(req_msg); + break; } - APP_MGR_FREE(msg); + case TIMER_EVENT: + { + if (msg->payload) { + /* Call Timer.callOnTimer() method */ + argv[0] = (unsigned int)msg->payload; + argt[0] = 1; + app_manager_call_java(method_callOnTimer, 1, argv, argt); + } + break; + } + + case SENSOR_EVENT: + { + if (msg->payload) { + bh_sensor_event_t *sensor_event = + (bh_sensor_event_t *)msg->payload; + AEESensor sensor = sensor_event->sensor; + attr_container_t *event = sensor_event->event; + bool ret = attr_container_to_attr_obj(event, &sensor->event); + + attr_container_destroy(event); + APP_MGR_FREE(sensor_event); + + if (ret) { + /* Call Sensor.callOnSensorEvent() method */ + argv[0] = (unsigned int)sensor; + argt[0] = 1; + app_manager_call_java(method_callOnSensorEvent, 1, argv, + argt); + } + } + break; + } + + case BLE_EVENT: + { + if (msg->payload) { + app_instance_process_ble_msg(msg->payload); + APP_MGR_FREE(msg->payload); + } + break; + } + + case GPIO_INTERRUPT_EVENT: + { + AEEGPIOChannel gpio_ch = (AEEGPIOChannel)msg->payload; + + if ((gpio_ch == NULL) || (gpio_ch->callback == 0) + || (gpio_ch->listener == NULL)) { + break; + } + argv[0] = (unsigned int)gpio_ch; + argt[0] = 1; + bool ret_value = app_manager_call_java(method_callOnGPIOInterrupt, + 1, argv, argt); + + if (!ret_value) { + app_manager_printf( + "app_manager_call_java " + "method_method_callOnGPIOInterrupt return false\n"); + } + break; + } + + default: + { + app_manager_printf( + "Invalid message type of applet queue message.\n"); + break; + } } - static JeffClassHeaderLinked* - find_main_class(JeffFileHeaderLinked *main_file) - { - JeffClassHeaderLinked *c = NULL, *ci; - unsigned int i; + APP_MGR_FREE(msg); +} - for (i = 0; i < main_file->internal_class_count; i++) { - ci = main_file->class_header[i]; +static JeffClassHeaderLinked * +find_main_class(JeffFileHeaderLinked *main_file) +{ + JeffClassHeaderLinked *c = NULL, *ci; + unsigned int i; - if (jeff_is_super_class(class_AEEApplet, ci) + for (i = 0; i < main_file->internal_class_count; i++) { + ci = main_file->class_header[i]; + + if (jeff_is_super_class(class_AEEApplet, ci) && (ci->access_flag & JEFF_ACC_PUBLIC)) { - if (c) { - jeff_printe_more_than_one_main_class(); - return NULL; - } - - c = ci; + if (c) { + jeff_printe_more_than_one_main_class(); + return NULL; } - } - if (!c) + c = ci; + } + } + + if (!c) jeff_printe_no_main_class(); - return c; - } + return c; +} - /* Java applet thread main routine */ - static void* - app_instance_main(void *arg) - { - module_data *m_data = (module_data *)arg; - jeff_applet_data *applet_data = (jeff_applet_data*)m_data->internal_data; - JeffClassHeaderLinked *object_class; - JeffMethodLinked *m; - unsigned int argv[1]; - uint8 argt[1]; +/* Java applet thread main routine */ +static void * +app_instance_main(void *arg) +{ + module_data *m_data = (module_data *)arg; + jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data; + JeffClassHeaderLinked *object_class; + JeffMethodLinked *m; + unsigned int argv[1]; + uint8 argt[1]; - app_manager_printf("Java Applet '%s' started\n", m_data->module_name); + app_manager_printf("Java Applet '%s' started\n", m_data->module_name); #if BEIHAI_ENABLE_TOOL_AGENT != 0 - if (applet_data->debug_mode) + if (applet_data->debug_mode) jeff_tool_suspend_self(); #endif - applet_data->vm_instance->applet_object = applet_data->applet_obj; - object_class = jeff_object_class_pointer(applet_data->applet_obj); - m = jeff_select_method_virtual(object_class, method_AEEApplet_onInit); - bh_assert(m != NULL); - /* Initialize applet class which call */ - if (!app_manager_initialize_class(object_class)) { - app_manager_printf("Call fail\n"); - goto fail; - } - - /* Initialize applet object which call */ - if (!app_manager_initialize_object(applet_data->applet_obj)) { - app_manager_printf("Call fail\n"); - goto fail; - } - - /* Call applet's onInit() method */ - argv[0] = (unsigned int)applet_data->applet_obj; - argt[0] = 1; - if (app_manager_call_java(m, 1, argv, argt)) - /* Enter queue loop run to receive and process applet queue message */ - bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback); - - fail: - applet_data->vm_instance->applet_object = applet_data->applet_obj; - object_class = jeff_object_class_pointer(applet_data->applet_obj); - m = jeff_select_method_virtual(object_class, method_AEEApplet_onDestroy); - bh_assert(m != NULL); - /* Call User Applet or AEEApplet onDestroy() method */ - app_manager_call_java(m, 1, argv, argt); - if (m != method_AEEApplet_onDestroy) { - /*If 'm' is user onDestroy, then Call AEEApplet.onDestroy() method*/ - app_manager_call_java(method_AEEApplet_onDestroy, 1, argv, argt); - } - app_manager_printf("Applet instance main thread exit.\n"); - return NULL; + applet_data->vm_instance->applet_object = applet_data->applet_obj; + object_class = jeff_object_class_pointer(applet_data->applet_obj); + m = jeff_select_method_virtual(object_class, method_AEEApplet_onInit); + bh_assert(m != NULL); + /* Initialize applet class which call */ + if (!app_manager_initialize_class(object_class)) { + app_manager_printf("Call fail\n"); + goto fail; } - static bool - verify_signature(JeffFileHeader *file, unsigned size) - { - uint8 *sig; - unsigned sig_size; + /* Initialize applet object which call */ + if (!app_manager_initialize_object(applet_data->applet_obj)) { + app_manager_printf("Call fail\n"); + goto fail; + } + + /* Call applet's onInit() method */ + argv[0] = (unsigned int)applet_data->applet_obj; + argt[0] = 1; + if (app_manager_call_java(m, 1, argv, argt)) + /* Enter queue loop run to receive and process applet queue message + */ + bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback); + +fail: + applet_data->vm_instance->applet_object = applet_data->applet_obj; + object_class = jeff_object_class_pointer(applet_data->applet_obj); + m = jeff_select_method_virtual(object_class, method_AEEApplet_onDestroy); + bh_assert(m != NULL); + /* Call User Applet or AEEApplet onDestroy() method */ + app_manager_call_java(m, 1, argv, argt); + if (m != method_AEEApplet_onDestroy) { + /*If 'm' is user onDestroy, then Call AEEApplet.onDestroy() method*/ + app_manager_call_java(method_AEEApplet_onDestroy, 1, argv, argt); + } + app_manager_printf("Applet instance main thread exit.\n"); + return NULL; +} + +static bool +verify_signature(JeffFileHeader *file, unsigned size) +{ + uint8 *sig; + unsigned sig_size; #if BEIHAI_ENABLE_NO_SIGNATURE != 0 - /* no signature */ - if (file->file_signature == 0) + /* no signature */ + if (file->file_signature == 0) return true; #endif - if (file->file_length != size + if (file->file_length != size #if BEIHAI_ENABLE_NO_SIGNATURE == 0 || file->file_signature == 0 #endif || file->file_signature >= file->file_length) return false; - sig = (uint8 *)file + file->file_signature; - sig_size = file->file_length - file->file_signature; + sig = (uint8 *)file + file->file_signature; + sig_size = file->file_length - file->file_signature; - if (0 == app_manager_signature_verify((uint8_t *)file, file->file_signature, - sig, sig_size)) + if (0 + == app_manager_signature_verify((uint8_t *)file, file->file_signature, + sig, sig_size)) return false; - return true; + return true; +} + +/* Install Java Applet */ +static bool +jeff_module_install(bh_request_msg_t *msg) +{ + unsigned int size, bpk_file_len, main_file_len, heap_size, timeout; + uint8 *bpk_file; + JeffFileHeaderLinked *main_file; + JeffClassHeaderLinked *main_class; + module_data *m_data; + jeff_applet_data *applet_data; + char *applet_name, *applet_perm; + attr_container_t *attr_cont; + bool debug = false; + + /* Check url */ + if (!msg->url || strcmp(msg->url, "/applet") != 0) { + SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid url."); + return false; } - /* Install Java Applet */ - static bool - jeff_module_install(bh_request_msg_t *msg) - { - unsigned int size, bpk_file_len, main_file_len, heap_size, timeout; - uint8 *bpk_file; - JeffFileHeaderLinked *main_file; - JeffClassHeaderLinked *main_class; - module_data *m_data; - jeff_applet_data *applet_data; - char *applet_name, *applet_perm; - attr_container_t *attr_cont; - bool debug = false; + /* Check payload */ + attr_cont = (attr_container_t *)msg->payload; + if (!attr_cont + || !(bpk_file = (uint8 *)attr_container_get_as_bytearray( + attr_cont, "bpk", &bpk_file_len))) { + SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid bpk file."); + return false; + } - /* Check url */ - if (!msg->url - || strcmp(msg->url, "/applet") != 0) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid url."); - return false; - } + /* Check applet name */ + applet_name = attr_container_get_as_string(attr_cont, "name"); - /* Check payload */ - attr_cont = (attr_container_t *)msg->payload; - if (!attr_cont - || !(bpk_file = (uint8 *) - attr_container_get_as_bytearray(attr_cont, "bpk", &bpk_file_len))) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid bpk file."); - return false; - } + if (!applet_name || strlen(applet_name) == 0) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: invalid applet name."); + return false; + } - /* Check applet name */ - applet_name = attr_container_get_as_string(attr_cont, "name"); + if (app_manager_lookup_module_data(applet_name)) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: applet already installed."); + return false; + } - if (!applet_name || strlen(applet_name) == 0) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: invalid applet name."); - return false; - } + /* TODO: convert bpk file to Jeff file */ + main_file_len = bpk_file_len; + main_file = APP_MGR_MALLOC(main_file_len); + if (!main_file) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: allocate memory failed."); + return false; + } + bh_memcpy_s(main_file, main_file_len, bpk_file, main_file_len); - if (app_manager_lookup_module_data(applet_name)) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: applet already installed."); - return false; - } + /* Verify signature */ + if (!verify_signature((JeffFileHeader *)main_file, main_file_len)) { + SEND_ERR_RESPONSE( + msg->mid, + "Install Applet failed: verify Jeff file signature failed."); + goto fail1; + } - /* TODO: convert bpk file to Jeff file */ - main_file_len = bpk_file_len; - main_file = APP_MGR_MALLOC(main_file_len); - if (!main_file) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: allocate memory failed."); - return false; - } - bh_memcpy_s(main_file, main_file_len, bpk_file, main_file_len); + /* Load Jeff main file */ + if (!jeff_runtime_load(main_file, main_file_len, false, NULL, NULL)) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: load Jeff file failed."); + goto fail1; + } - /* Verify signature */ - if (!verify_signature((JeffFileHeader *)main_file, main_file_len)) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: verify Jeff file signature failed."); - goto fail1; - } + /* Find main class */ + main_class = find_main_class(main_file); + if (!main_class) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: find applet class failed."); + goto fail2; + } - /* Load Jeff main file */ - if (!jeff_runtime_load(main_file, main_file_len, false, NULL, NULL)) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: load Jeff file failed."); - goto fail1; - } + /* Create module data */ + size = offsetof(module_data, module_name) + strlen(applet_name) + 1; + size = align_uint(size, 4); + m_data = APP_MGR_MALLOC(size + sizeof(jeff_applet_data)); + if (!m_data) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: allocate memory failed."); + goto fail2; + } - /* Find main class */ - main_class = find_main_class(main_file); - if (!main_class) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: find applet class failed."); - goto fail2; - } + memset(m_data, 0, size + sizeof(jeff_applet_data)); + m_data->module_type = Module_Jeff; + m_data->internal_data = (uint8 *)m_data + size; + applet_data = (jeff_applet_data *)m_data->internal_data; + bh_strcpy_s(m_data->module_name, strlen(applet_name) + 1, applet_name); + applet_data->main_file = main_file; - /* Create module data */ - size = offsetof(module_data, module_name) + strlen(applet_name) + 1; - size = align_uint(size, 4); - m_data = APP_MGR_MALLOC(size + sizeof(jeff_applet_data)); - if (!m_data) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: allocate memory failed."); - goto fail2; - } - - memset(m_data, 0, size + sizeof(jeff_applet_data)); - m_data->module_type = Module_Jeff; - m_data->internal_data = (uint8*)m_data + size; - applet_data = (jeff_applet_data*)m_data->internal_data; - bh_strcpy_s(m_data->module_name, strlen(applet_name) + 1, applet_name); - applet_data->main_file = main_file; - - /* Set applet execution timeout */ - timeout = DEFAULT_APPLET_TIMEOUT; - if (attr_container_contain_key(attr_cont, "execution timeout")) + /* Set applet execution timeout */ + timeout = DEFAULT_APPLET_TIMEOUT; + if (attr_container_contain_key(attr_cont, "execution timeout")) timeout = attr_container_get_as_int(attr_cont, "execution timeout"); - m_data->timeout = timeout; + m_data->timeout = timeout; - /* Create applet permissions */ - applet_perm = attr_container_get_as_string(attr_cont, "perm"); - if (applet_perm != NULL) { - applet_data->perms = APP_MGR_MALLOC(strlen(applet_perm) + 1); - if (!applet_data->perms) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: allocate memory for applet permissions failed."); - goto fail3; - } - - bh_strcpy_s(applet_data->perms, strlen(applet_perm) + 1, applet_perm); + /* Create applet permissions */ + applet_perm = attr_container_get_as_string(attr_cont, "perm"); + if (applet_perm != NULL) { + applet_data->perms = APP_MGR_MALLOC(strlen(applet_perm) + 1); + if (!applet_data->perms) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: allocate memory for " + "applet permissions failed."); + goto fail3; } - /* Create applet queue */ - m_data->queue = bh_queue_create(); - if (!m_data->queue) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create applet queue failed."); - goto fail3_1; - } + bh_strcpy_s(applet_data->perms, strlen(applet_perm) + 1, applet_perm); + } - /* Set heap size */ - heap_size = DEFAULT_APPLET_HEAP_SIZE; - if (attr_container_contain_key(attr_cont, "heap size")) { - heap_size = attr_container_get_as_int(attr_cont, "heap size"); - if (heap_size < MIN_APPLET_HEAP_SIZE) + /* Create applet queue */ + m_data->queue = bh_queue_create(); + if (!m_data->queue) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: create applet queue failed."); + goto fail3_1; + } + + /* Set heap size */ + heap_size = DEFAULT_APPLET_HEAP_SIZE; + if (attr_container_contain_key(attr_cont, "heap size")) { + heap_size = attr_container_get_as_int(attr_cont, "heap size"); + if (heap_size < MIN_APPLET_HEAP_SIZE) heap_size = MIN_APPLET_HEAP_SIZE; - else if (heap_size > MAX_APPLET_HEAP_SIZE) + else if (heap_size > MAX_APPLET_HEAP_SIZE) heap_size = MAX_APPLET_HEAP_SIZE; - } + } - m_data->heap_size = heap_size; + m_data->heap_size = heap_size; - /* Create applet heap */ - m_data->heap = gc_init_for_instance(heap_size); - if (!m_data->heap) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create heap failed."); - goto fail4; - } + /* Create applet heap */ + m_data->heap = gc_init_for_instance(heap_size); + if (!m_data->heap) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: create heap failed."); + goto fail4; + } - /* Create applet object */ - applet_data->applet_obj = jeff_object_new(m_data->heap, main_class); - if (!applet_data->applet_obj) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create applet object failed."); - goto fail5; - } + /* Create applet object */ + applet_data->applet_obj = jeff_object_new(m_data->heap, main_class); + if (!applet_data->applet_obj) { + SEND_ERR_RESPONSE( + msg->mid, "Install Applet failed: create applet object failed."); + goto fail5; + } - /* Initialize watchdog timer */ - if (!watchdog_timer_init(m_data)) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create applet watchdog timer failed."); - goto fail5; - } + /* Initialize watchdog timer */ + if (!watchdog_timer_init(m_data)) { + SEND_ERR_RESPONSE( + msg->mid, + "Install Applet failed: create applet watchdog timer failed."); + goto fail5; + } #if BEIHAI_ENABLE_TOOL_AGENT != 0 - /* Check whether applet is debuggable */ - if (attr_container_contain_key(attr_cont, "debug")) + /* Check whether applet is debuggable */ + if (attr_container_contain_key(attr_cont, "debug")) debug = attr_container_get_as_bool(attr_cont, "debug"); - applet_data->debug_mode = debug; + applet_data->debug_mode = debug; - /* Create tool agent queue */ - if (debug && !(applet_data->tool_agent_queue = bh_queue_create())) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create tool agent queue failed."); - goto fail5_1; - } + /* Create tool agent queue */ + if (debug && !(applet_data->tool_agent_queue = bh_queue_create())) { + SEND_ERR_RESPONSE( + msg->mid, "Install Applet failed: create tool agent queue failed."); + goto fail5_1; + } #endif - /* Create applet instance */ - applet_data->vm_instance = - jeff_runtime_create_instance(main_file, m_data->heap, 16, - app_instance_main, m_data, - NULL); - if (!applet_data->vm_instance) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: create Java VM failed"); - goto fail6; - } + /* Create applet instance */ + applet_data->vm_instance = jeff_runtime_create_instance( + main_file, m_data->heap, 16, app_instance_main, m_data, NULL); + if (!applet_data->vm_instance) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: create Java VM failed"); + goto fail6; + } - /* Add applet data to applet data list */ - applet_data->vm_instance->applet_object = applet_data->applet_obj; - app_manager_add_module_data(m_data); - app_manager_post_applets_update_event(); + /* Add applet data to applet data list */ + applet_data->vm_instance->applet_object = applet_data->applet_obj; + app_manager_add_module_data(m_data); + app_manager_post_applets_update_event(); #if BEIHAI_ENABLE_TOOL_AGENT != 0 - /* Start tool agent thread */ - if (debug && !jeff_tool_start_agent(applet_data->vm_instance, applet_data->tool_agent_queue)) { - SEND_ERR_RESPONSE(msg->mid, "Install Applet failed: start tool agent failed"); - goto fail6; - } + /* Start tool agent thread */ + if (debug + && !jeff_tool_start_agent(applet_data->vm_instance, + applet_data->tool_agent_queue)) { + SEND_ERR_RESPONSE(msg->mid, + "Install Applet failed: start tool agent failed"); + goto fail6; + } #endif - app_manager_printf("Install Applet success!\n"); - app_send_response_to_host(msg->mid, CREATED_2_01, NULL); /* CREATED */ - return true; + app_manager_printf("Install Applet success!\n"); + app_send_response_to_host(msg->mid, CREATED_2_01, NULL); /* CREATED */ + return true; - fail6: +fail6: #if BEIHAI_ENABLE_TOOL_AGENT != 0 - if (debug) + if (debug) bh_queue_destroy(applet_data->tool_agent_queue); #endif - fail5_1: - watchdog_timer_destroy(&m_data->wd_timer); +fail5_1: + watchdog_timer_destroy(&m_data->wd_timer); - fail5: - gc_destroy_for_instance(m_data->heap); +fail5: + gc_destroy_for_instance(m_data->heap); - fail4: - bh_queue_destroy(m_data->queue, NULL); +fail4: + bh_queue_destroy(m_data->queue, NULL); - fail3_1: - APP_MGR_FREE(applet_data->perms); +fail3_1: + APP_MGR_FREE(applet_data->perms); - fail3: - APP_MGR_FREE(applet_data); +fail3: + APP_MGR_FREE(applet_data); - fail2: - jeff_runtime_unload(main_file); +fail2: + jeff_runtime_unload(main_file); - fail1: - APP_MGR_FREE(main_file); +fail1: + APP_MGR_FREE(main_file); + return false; +} + +static void +cleanup_applet_resource(module_data *m_data) +{ + jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data; + + /* Unload Jeff main file and free it */ + jeff_runtime_unload(applet_data->main_file); + APP_MGR_FREE(applet_data->main_file); + + /* Destroy queue */ + bh_queue_destroy(m_data->queue, app_instance_queue_free_callback); + + /* Destroy heap */ + gc_destroy_for_instance(m_data->heap); + + /* Destroy watchdog timer */ + watchdog_timer_destroy(&m_data->wd_timer); + + /* Remove module data from module data list and free it */ + app_manager_del_module_data(m_data); + APP_MGR_FREE(applet_data->perms); + APP_MGR_FREE(m_data); +} + +/* Uninstall Java Applet */ +static bool +jeff_module_uninstall(bh_request_msg_t *msg) +{ + module_data *m_data; + jeff_applet_data *applet_data; + attr_container_t *attr_cont; + char *applet_name; + bool do_not_reply = false; + + /* Check payload and applet name*/ + attr_cont = (attr_container_t *)msg->payload; + + /* Check whether need to reply this request */ + if (attr_container_contain_key(attr_cont, "do not reply me")) + do_not_reply = attr_container_get_as_bool(attr_cont, "do not reply me"); + + /* Check url */ + if (!msg->url || strcmp(msg->url, "/applet") != 0) { + if (!do_not_reply) + SEND_ERR_RESPONSE(msg->mid, + "Uninstall Applet failed: invalid url."); + else + app_manager_printf("Uninstall Applet failed: invalid url."); return false; } - static void - cleanup_applet_resource(module_data *m_data) - { - jeff_applet_data *applet_data = (jeff_applet_data*)m_data->internal_data; - - /* Unload Jeff main file and free it */ - jeff_runtime_unload(applet_data->main_file); - APP_MGR_FREE(applet_data->main_file); - - /* Destroy queue */ - bh_queue_destroy(m_data->queue, app_instance_queue_free_callback); - - /* Destroy heap */ - gc_destroy_for_instance(m_data->heap); - - /* Destroy watchdog timer */ - watchdog_timer_destroy(&m_data->wd_timer); - - /* Remove module data from module data list and free it */ - app_manager_del_module_data(m_data); - APP_MGR_FREE(applet_data->perms); - APP_MGR_FREE(m_data); - } - - /* Uninstall Java Applet */ - static bool - jeff_module_uninstall(bh_request_msg_t *msg) - { - module_data *m_data; - jeff_applet_data *applet_data; - attr_container_t *attr_cont; - char *applet_name; - bool do_not_reply = false; - - /* Check payload and applet name*/ - attr_cont = (attr_container_t *)msg->payload; - - /* Check whether need to reply this request */ - if (attr_container_contain_key(attr_cont, "do not reply me")) - do_not_reply = attr_container_get_as_bool(attr_cont, "do not reply me"); - - /* Check url */ - if (!msg->url - || strcmp(msg->url, "/applet") != 0) { - if (!do_not_reply) - SEND_ERR_RESPONSE(msg->mid, "Uninstall Applet failed: invalid url."); - else - app_manager_printf("Uninstall Applet failed: invalid url."); - return false; - } - - if (!attr_cont + if (!attr_cont || !(applet_name = attr_container_get_as_string(attr_cont, "name")) || strlen(applet_name) == 0) { - if (!do_not_reply) - SEND_ERR_RESPONSE(msg->mid, "Uninstall Applet failed: invalid applet name."); - else + if (!do_not_reply) + SEND_ERR_RESPONSE(msg->mid, + "Uninstall Applet failed: invalid applet name."); + else app_manager_printf("Uninstall Applet failed: invalid applet name."); - return false; - } + return false; + } - m_data = app_manager_lookup_module_data(applet_name); - if (!m_data) { - if (!do_not_reply) - SEND_ERR_RESPONSE(msg->mid, "Uninstall Applet failed: no applet found."); - else + m_data = app_manager_lookup_module_data(applet_name); + if (!m_data) { + if (!do_not_reply) + SEND_ERR_RESPONSE(msg->mid, + "Uninstall Applet failed: no applet found."); + else app_manager_printf("Uninstall Applet failed: no applet found."); - return false; - } + return false; + } - if (m_data->module_type != Module_Jeff) { - if (!do_not_reply) - SEND_ERR_RESPONSE(msg->mid, "Uninstall Applet failed: invlaid module type."); - else + if (m_data->module_type != Module_Jeff) { + if (!do_not_reply) + SEND_ERR_RESPONSE(msg->mid, + "Uninstall Applet failed: invlaid module type."); + else app_manager_printf("Uninstall Applet failed: invalid module type."); - return false; - } + return false; + } - if (m_data->wd_timer.is_interrupting) { - if (!do_not_reply) - SEND_ERR_RESPONSE(msg->mid, "Uninstall Applet failed: applet is being interrupted by watchdog."); - else - app_manager_printf("Uninstall Applet failed: applet is being interrupted by watchdog."); - return false; - } + if (m_data->wd_timer.is_interrupting) { + if (!do_not_reply) + SEND_ERR_RESPONSE(msg->mid, + "Uninstall Applet failed: applet is being " + "interrupted by watchdog."); + else + app_manager_printf("Uninstall Applet failed: applet is being " + "interrupted by watchdog."); + return false; + } - /* Exit applet queue loop run */ - bh_queue_exit_loop_run(m_data->queue); + /* Exit applet queue loop run */ + bh_queue_exit_loop_run(m_data->queue); - applet_data = (jeff_applet_data*)m_data->internal_data; + applet_data = (jeff_applet_data *)m_data->internal_data; #if BEIHAI_ENABLE_TOOL_AGENT != 0 - /* Exit tool agent queue loop run */ - if (is_tool_agent_running(m_data)) { - bh_queue_exit_loop_run(applet_data->tool_agent_queue); - } + /* Exit tool agent queue loop run */ + if (is_tool_agent_running(m_data)) { + bh_queue_exit_loop_run(applet_data->tool_agent_queue); + } #endif - /* Wait the end of the applet instance and then destroy it */ - if (applet_data->vm_instance->main_file) + /* Wait the end of the applet instance and then destroy it */ + if (applet_data->vm_instance->main_file) jeff_runtime_wait_for_instance(applet_data->vm_instance, -1); - jeff_runtime_destroy_instance(applet_data->vm_instance); + jeff_runtime_destroy_instance(applet_data->vm_instance); - cleanup_applet_resource(m_data); - app_manager_post_applets_update_event(); + cleanup_applet_resource(m_data); + app_manager_post_applets_update_event(); - app_manager_printf("Uninstall Applet success!\n"); + app_manager_printf("Uninstall Applet success!\n"); - if (!do_not_reply) + if (!do_not_reply) app_send_response_to_host(msg->mid, DELETED_2_02, NULL); /* DELETED */ - return true; - } + return true; +} #define PERM_PREFIX "AEE.permission." - static bool - check_permission_format(const char *perm) - { - const char *prefix = PERM_PREFIX; - const char *p; +static bool +check_permission_format(const char *perm) +{ + const char *prefix = PERM_PREFIX; + const char *p; - if (perm == NULL || strncmp(perm, prefix, strlen(prefix)) != 0 + if (perm == NULL || strncmp(perm, prefix, strlen(prefix)) != 0 || *(p = perm + strlen(prefix)) == '\0') return false; - do { - if (!(*p == '.' || ('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z'))) + do { + if (!(*p == '.' || ('A' <= *p && *p <= 'Z') + || ('a' <= *p && *p <= 'z'))) return false; - }while (*++p != '\0'); + } while (*++p != '\0'); - return true; - } + return true; +} - static bool - match(const char *haystack, const char *needle, char delim) - { - const char *p = needle; +static bool +match(const char *haystack, const char *needle, char delim) +{ + const char *p = needle; - if (haystack == NULL || *haystack == '\0' - || needle == NULL || *needle == '\0') + if (haystack == NULL || *haystack == '\0' || needle == NULL + || *needle == '\0') return false; + while (true) { while (true) { - while (true) { - if ((*haystack == '\0' || *haystack == delim) && *p == '\0') { - return true; - } else if (*p == *haystack) { - ++p; - ++haystack; - } else { - break; - } + if ((*haystack == '\0' || *haystack == delim) && *p == '\0') { + return true; } - while (*haystack != '\0' && *haystack != delim) { + else if (*p == *haystack) { + ++p; ++haystack; } - if (*haystack == '\0') { - return false; - } else { - ++haystack; - p = needle; + else { + break; } } + while (*haystack != '\0' && *haystack != delim) { + ++haystack; + } + if (*haystack == '\0') { + return false; + } + else { + ++haystack; + p = needle; + } + } +} + +bool +bh_applet_check_permission(const char *perm) +{ + return check_permission_format(perm) + && match(app_manager_get_jeff_applet_data()->perms, + perm + strlen(PERM_PREFIX), ' '); +} + +static bool +jeff_module_init() +{ + JeffDescriptorFull d[] = { { JEFF_TYPE_VOID, 0, NULL } }; + JeffDescriptorFull d1[] = { { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, NULL }, + { JEFF_TYPE_VOID, 0, NULL } }; + + /* Resolve class com.intel.aee.AEEApplet */ + class_AEEApplet = + jeff_runtime_resolve_class_full_name("com.intel.aee.AEEApplet"); + if (!class_AEEApplet) { + app_manager_printf( + "App Manager start failed: resolve class AEEApplet failed.\n"); + return false; } - bool - bh_applet_check_permission(const char *perm) - { - return check_permission_format(perm) - && match(app_manager_get_jeff_applet_data()->perms, - perm + strlen(PERM_PREFIX), ' '); + /* Resolve class com.intel.aee.Request */ + class_AEERequest = + jeff_runtime_resolve_class_full_name("com.intel.aee.Request"); + if (!class_AEERequest) { + app_manager_printf( + "App Manager start failed: resolve class Request failed.\n"); + return false; } - static bool - jeff_module_init() - { - JeffDescriptorFull d[] = { {JEFF_TYPE_VOID, 0, NULL}}; - JeffDescriptorFull d1[] = { - { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, NULL}, - { JEFF_TYPE_VOID, 0, NULL} - }; + /* Resolve class com.intel.aee.Timer */ + class_Timer = jeff_runtime_resolve_class_full_name("com.intel.aee.Timer"); + if (!class_Timer) { + app_manager_printf( + "App Manager start failed: resolve class Timer failed.\n"); + return false; + } - /* Resolve class com.intel.aee.AEEApplet */ - class_AEEApplet = jeff_runtime_resolve_class_full_name("com.intel.aee.AEEApplet"); - if (!class_AEEApplet) { - app_manager_printf("App Manager start failed: resolve class AEEApplet failed.\n"); - return false; - } + /* Resolve class com.intel.aee.Sensor */ + class_Sensor = jeff_runtime_resolve_class_full_name("com.intel.aee.Sensor"); + if (!class_Sensor) { + app_manager_printf( + "App Manager start failed: resolve class Sensor failed.\n"); + return false; + } - /* Resolve class com.intel.aee.Request */ - class_AEERequest = jeff_runtime_resolve_class_full_name("com.intel.aee.Request"); - if (!class_AEERequest) { - app_manager_printf("App Manager start failed: resolve class Request failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.Timer */ - class_Timer = jeff_runtime_resolve_class_full_name("com.intel.aee.Timer"); - if (!class_Timer) { - app_manager_printf("App Manager start failed: resolve class Timer failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.Sensor */ - class_Sensor = jeff_runtime_resolve_class_full_name("com.intel.aee.Sensor"); - if (!class_Sensor) { - app_manager_printf("App Manager start failed: resolve class Sensor failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.ble.BLEManager */ - class_BLEManager = jeff_runtime_resolve_class_full_name( - "com.intel.aee.ble.BLEManager"); - if (!class_BLEManager) { - app_manager_printf( + /* Resolve class com.intel.aee.ble.BLEManager */ + class_BLEManager = + jeff_runtime_resolve_class_full_name("com.intel.aee.ble.BLEManager"); + if (!class_BLEManager) { + app_manager_printf( "App Manager start failed: resolve class BLEManager failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.ble.BLEDevice */ - class_BLEDevice = jeff_runtime_resolve_class_full_name( - "com.intel.aee.ble.BLEDevice"); - if (!class_BLEDevice) { - app_manager_printf( - "App Manager start failed: resolve class BLEDevice failed.\n"); - return false; - } - /* Resolve class com.intel.aee.ble.BLEDevice */ - class_BLEGattService = jeff_runtime_resolve_class_full_name( - "com.intel.aee.ble.BLEGattService"); - if (!class_BLEGattService) { - app_manager_printf( - "App Manager start failed: resolve class BLEGattService failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.ble.BLEDevice */ - class_BLEGattCharacteristic = jeff_runtime_resolve_class_full_name( - "com.intel.aee.ble.BLEGattCharacteristic"); - if (!class_BLEGattCharacteristic) { - app_manager_printf( - "App Manager start failed: resolve class BLEGattCharacteristic failed.\n"); - return false; - } - - /* Resolve class com.intel.aee.ble.BLEDevice */ - class_BLEGattDescriptor = jeff_runtime_resolve_class_full_name( - "com.intel.aee.ble.BLEGattDescriptor"); - if (!class_BLEGattDescriptor) { - app_manager_printf( - "App Manager start failed: resolve class BLEGattDescriptor failed.\n"); - return false; - } - /* Resolve class com.intel.aee.gpio.GPIOChannel */ - class_GPIOChannel = jeff_runtime_resolve_class_full_name( - "com.intel.aee.gpio.GPIOChannel"); - if (!class_GPIOChannel) { - app_manager_printf( - "App Manager start failed: resolve class GPIOChannel failed.\n"); - return false; - } - - /* Resolve method com.intel.aee.AEEApplet.onInit() */ - method_AEEApplet_onInit = jeff_lookup_method(class_AEEApplet, "onInit", 0, d); - if (!method_AEEApplet_onInit) { - app_manager_printf("App Manager start failed: resolve method Applet.onInit() failed.\n"); - return false; - } - - /* Resolve method com.intel.aee.AEEApplet.onDestroy() */ - method_AEEApplet_onDestroy = jeff_lookup_method(class_AEEApplet, "onDestroy", 0, d); - if (!method_AEEApplet_onDestroy) { - app_manager_printf("App Manager start failed: resolve method AEEApplet.onDestroy() failed.\n"); - return false; - } - - /* Resolve method com.intel.aee.AEEApplet.callOnRequest(Request) */ - d1[0].class_header = class_AEERequest; - method_AEEApplet_callOnRequest = jeff_lookup_method(class_AEEApplet, "callOnRequest", 1, d1); - if (!method_AEEApplet_callOnRequest) { - app_manager_printf("App Manager start failed: resolve method AEEApplet.callOnRequest() failed.\n"); - return false; - } - - /* Resolve method com.intel.aee.Timer.callOnTimer() */ - method_callOnTimer = jeff_lookup_method(class_Timer, "callOnTimer", 0, d); - if (!method_callOnTimer) { - app_manager_printf("App Manager start failed: resolve method Timer.callOnTimer() failed.\n"); - return false; - } - - /* Resolve method com.intel.aee.Sensor.callOnSensorEvent() */ - method_callOnSensorEvent = jeff_lookup_method(class_Sensor, "callOnSensorEvent", 0, d); - if (!method_callOnSensorEvent) { - app_manager_printf("App Manager start failed: resolve method Sensor.callOnSensorEvent() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.callOnBLEStartDiscovery(BLEDevice) */ - d1[0].class_header = class_BLEDevice; - method_callOnBLEStartDiscovery = jeff_lookup_method(class_BLEManager, "callOnBLEStartDiscovery", 1, d1); - if (!method_callOnBLEStartDiscovery) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLEStartDiscovery() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ - JeffDescriptorFull d2_1[] = { {JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice}, - { JEFF_TYPE_INT, 0, NULL}, - { JEFF_TYPE_VOID, 0, NULL}}; - method_callOnBLEConnected = jeff_lookup_method(class_BLEManager, "callOnBLEConnected", 2, d2_1); - if (!method_callOnBLEConnected) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLEConnected() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.method_callOnBLENotification(BLEDevice,byte[]) */ - JeffDescriptorFull d2_2[] = { {JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice}, - { JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL}, - { JEFF_TYPE_INT, 0, NULL}, - { JEFF_TYPE_INT, 0, NULL}, - { JEFF_TYPE_VOID, 0, NULL}}; - method_callOnBLENotification = jeff_lookup_method(class_BLEManager, "callOnBLENotification", 4, d2_2); - if (!method_callOnBLENotification) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLENotification() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice,byte[]) */ - method_callOnBLEIndication = jeff_lookup_method(class_BLEManager, "callOnBLEIndication", 4, d2_2); - if (!method_callOnBLEIndication) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLEIndication() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ - d1[0].class_header = class_BLEDevice; - method_callOnBLEDisconnected = jeff_lookup_method(class_BLEManager, "callOnBLEDisconnected", 1, d1); - if (!method_callOnBLEDisconnected) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLEDisconnected() failed.\n"); - return false; - } - - /* Resovle method com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ - method_callOnBLEPasskeyEntry = jeff_lookup_method(class_BLEManager, "callOnBLEPasskeyEntry", 1, d1); - if (!method_callOnBLEPasskeyEntry) { - app_manager_printf("App Manager start failed: resolve method BLEManager.callOnBLEPasskeyEntry() failed.\n"); - return false; - } - /* Resovle method void com.intel.aee.gpio.GPIOChannel.callOnGPIOInterrupt() */ - method_callOnGPIOInterrupt = jeff_lookup_method(class_GPIOChannel, "callOnGPIOInterrupt", 0, d); - if (!method_callOnGPIOInterrupt) { - app_manager_printf("App Manager start failed: resolve method GPIOChannel.callOnGPIOInterrupt() failed.\n"); - return false; - } - - JeffDescriptorFull d2[] = { {JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL}, - { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice}}; - /* Resovle method com.intel.aee.ble.BLEManager.getBLEDevice(byte []) */ - method_callOnBLEManagerGetBLEDevice = jeff_lookup_method(class_BLEManager, - "getBLEDevice", 1, d2); - if (!method_callOnBLEManagerGetBLEDevice) { - app_manager_printf( - "App Manager start failed: resolve method BLEManager.getBLEDevice() failed.\n"); - return false; - } - - return true; + return false; } - static void - jeff_module_watchdog_kill(module_data *m_data) - { - jeff_applet_data *applet_data = (jeff_applet_data*)m_data->internal_data; + /* Resolve class com.intel.aee.ble.BLEDevice */ + class_BLEDevice = + jeff_runtime_resolve_class_full_name("com.intel.aee.ble.BLEDevice"); + if (!class_BLEDevice) { + app_manager_printf( + "App Manager start failed: resolve class BLEDevice failed.\n"); + return false; + } + /* Resolve class com.intel.aee.ble.BLEDevice */ + class_BLEGattService = jeff_runtime_resolve_class_full_name( + "com.intel.aee.ble.BLEGattService"); + if (!class_BLEGattService) { + app_manager_printf("App Manager start failed: resolve class " + "BLEGattService failed.\n"); + return false; + } - app_manager_printf("Watchdog interrupt the applet %s\n", m_data->module_name); + /* Resolve class com.intel.aee.ble.BLEDevice */ + class_BLEGattCharacteristic = jeff_runtime_resolve_class_full_name( + "com.intel.aee.ble.BLEGattCharacteristic"); + if (!class_BLEGattCharacteristic) { + app_manager_printf("App Manager start failed: resolve class " + "BLEGattCharacteristic failed.\n"); + return false; + } - jeff_runtime_interrupt_instance(applet_data->vm_instance, true); + /* Resolve class com.intel.aee.ble.BLEDevice */ + class_BLEGattDescriptor = jeff_runtime_resolve_class_full_name( + "com.intel.aee.ble.BLEGattDescriptor"); + if (!class_BLEGattDescriptor) { + app_manager_printf("App Manager start failed: resolve class " + "BLEGattDescriptor failed.\n"); + return false; + } + /* Resolve class com.intel.aee.gpio.GPIOChannel */ + class_GPIOChannel = + jeff_runtime_resolve_class_full_name("com.intel.aee.gpio.GPIOChannel"); + if (!class_GPIOChannel) { + app_manager_printf("App Manager start failed: resolve class " + "GPIOChannel failed.\n"); + return false; + } - /* Exit applet queue loop run */ - bh_queue_exit_loop_run(m_data->queue); + /* Resolve method com.intel.aee.AEEApplet.onInit() */ + method_AEEApplet_onInit = + jeff_lookup_method(class_AEEApplet, "onInit", 0, d); + if (!method_AEEApplet_onInit) { + app_manager_printf("App Manager start failed: resolve method " + "Applet.onInit() failed.\n"); + return false; + } - /* Wait the end of the applet instance. If timeout, it means applet - * is busy executing native code, then try to cancle the main thread. */ - if (applet_data->vm_instance->main_file) + /* Resolve method com.intel.aee.AEEApplet.onDestroy() */ + method_AEEApplet_onDestroy = + jeff_lookup_method(class_AEEApplet, "onDestroy", 0, d); + if (!method_AEEApplet_onDestroy) { + app_manager_printf("App Manager start failed: resolve method " + "AEEApplet.onDestroy() failed.\n"); + return false; + } + + /* Resolve method com.intel.aee.AEEApplet.callOnRequest(Request) */ + d1[0].class_header = class_AEERequest; + method_AEEApplet_callOnRequest = + jeff_lookup_method(class_AEEApplet, "callOnRequest", 1, d1); + if (!method_AEEApplet_callOnRequest) { + app_manager_printf("App Manager start failed: resolve method " + "AEEApplet.callOnRequest() failed.\n"); + return false; + } + + /* Resolve method com.intel.aee.Timer.callOnTimer() */ + method_callOnTimer = jeff_lookup_method(class_Timer, "callOnTimer", 0, d); + if (!method_callOnTimer) { + app_manager_printf("App Manager start failed: resolve method " + "Timer.callOnTimer() failed.\n"); + return false; + } + + /* Resolve method com.intel.aee.Sensor.callOnSensorEvent() */ + method_callOnSensorEvent = + jeff_lookup_method(class_Sensor, "callOnSensorEvent", 0, d); + if (!method_callOnSensorEvent) { + app_manager_printf("App Manager start failed: resolve method " + "Sensor.callOnSensorEvent() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.callOnBLEStartDiscovery(BLEDevice) */ + d1[0].class_header = class_BLEDevice; + method_callOnBLEStartDiscovery = + jeff_lookup_method(class_BLEManager, "callOnBLEStartDiscovery", 1, d1); + if (!method_callOnBLEStartDiscovery) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLEStartDiscovery() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ + JeffDescriptorFull d2_1[] = { { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, + class_BLEDevice }, + { JEFF_TYPE_INT, 0, NULL }, + { JEFF_TYPE_VOID, 0, NULL } }; + method_callOnBLEConnected = + jeff_lookup_method(class_BLEManager, "callOnBLEConnected", 2, d2_1); + if (!method_callOnBLEConnected) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLEConnected() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.method_callOnBLENotification(BLEDevice,byte[]) + */ + JeffDescriptorFull d2_2[] = { + { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice }, + { JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL }, + { JEFF_TYPE_INT, 0, NULL }, + { JEFF_TYPE_INT, 0, NULL }, + { JEFF_TYPE_VOID, 0, NULL } + }; + method_callOnBLENotification = + jeff_lookup_method(class_BLEManager, "callOnBLENotification", 4, d2_2); + if (!method_callOnBLENotification) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLENotification() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice,byte[]) */ + method_callOnBLEIndication = + jeff_lookup_method(class_BLEManager, "callOnBLEIndication", 4, d2_2); + if (!method_callOnBLEIndication) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLEIndication() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ + d1[0].class_header = class_BLEDevice; + method_callOnBLEDisconnected = + jeff_lookup_method(class_BLEManager, "callOnBLEDisconnected", 1, d1); + if (!method_callOnBLEDisconnected) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLEDisconnected() failed.\n"); + return false; + } + + /* Resovle method + * com.intel.aee.ble.BLEManager.callOnBLEConnected(BLEDevice) */ + method_callOnBLEPasskeyEntry = + jeff_lookup_method(class_BLEManager, "callOnBLEPasskeyEntry", 1, d1); + if (!method_callOnBLEPasskeyEntry) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.callOnBLEPasskeyEntry() failed.\n"); + return false; + } + /* Resovle method void + * com.intel.aee.gpio.GPIOChannel.callOnGPIOInterrupt() */ + method_callOnGPIOInterrupt = + jeff_lookup_method(class_GPIOChannel, "callOnGPIOInterrupt", 0, d); + if (!method_callOnGPIOInterrupt) { + app_manager_printf("App Manager start failed: resolve method " + "GPIOChannel.callOnGPIOInterrupt() failed.\n"); + return false; + } + + JeffDescriptorFull d2[] = { + { JEFF_TYPE_BYTE | JEFF_TYPE_REF | JEFF_TYPE_MONO, 1, NULL }, + { JEFF_TYPE_OBJECT | JEFF_TYPE_REF, 0, class_BLEDevice } + }; + /* Resovle method com.intel.aee.ble.BLEManager.getBLEDevice(byte []) */ + method_callOnBLEManagerGetBLEDevice = + jeff_lookup_method(class_BLEManager, "getBLEDevice", 1, d2); + if (!method_callOnBLEManagerGetBLEDevice) { + app_manager_printf("App Manager start failed: resolve method " + "BLEManager.getBLEDevice() failed.\n"); + return false; + } + + return true; +} + +static void +jeff_module_watchdog_kill(module_data *m_data) +{ + jeff_applet_data *applet_data = (jeff_applet_data *)m_data->internal_data; + + app_manager_printf("Watchdog interrupt the applet %s\n", + m_data->module_name); + + jeff_runtime_interrupt_instance(applet_data->vm_instance, true); + + /* Exit applet queue loop run */ + bh_queue_exit_loop_run(m_data->queue); + + /* Wait the end of the applet instance. If timeout, it means applet + * is busy executing native code, then try to cancle the main thread. */ + if (applet_data->vm_instance->main_file) jeff_runtime_wait_for_instance(applet_data->vm_instance, 3000); - if (applet_data->vm_instance->main_file) { - app_manager_printf("Watchdog cancel applet main thread.\n"); - os_thread_cancel(applet_data->vm_instance->main_tlr.handle); - /* k_thread_abort(applet_data->vm_instance->main_tlr.handle); */ - } - - send_exception_event_to_host(m_data->module_name, "java.lang.InterruptedException"); - cleanup_applet_resource(m_data); - app_manager_printf("Watchdog interrupt Jeff applet done.\n"); + if (applet_data->vm_instance->main_file) { + app_manager_printf("Watchdog cancel applet main thread.\n"); + os_thread_cancel(applet_data->vm_instance->main_tlr.handle); + /* k_thread_abort(applet_data->vm_instance->main_tlr.handle); */ } - static bool - jeff_module_handle_host_url(void *queue_msg) - { + send_exception_event_to_host(m_data->module_name, + "java.lang.InterruptedException"); + cleanup_applet_resource(m_data); + app_manager_printf("Watchdog interrupt Jeff applet done.\n"); +} + +static bool +jeff_module_handle_host_url(void *queue_msg) +{ #if BEIHAI_ENABLE_TOOL_AGENT != 0 - bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; + bh_queue_msg_t *msg = (bh_queue_msg_t *)queue_msg; - if (msg->message_type == COAP_PARSED) { - coap_packet_t *packet = (coap_packet_t *)msg->payload; - attr_container_t *attr_cont = (attr_container_t *)packet->payload; - const char *url = NULL; - int url_len = 0, mid; + if (msg->message_type == COAP_PARSED) { + coap_packet_t *packet = (coap_packet_t *)msg->payload; + attr_container_t *attr_cont = (attr_container_t *)packet->payload; + const char *url = NULL; + int url_len = 0, mid; - bh_memcpy_s(&mid, sizeof(uint32), packet->token, sizeof(uint32)); - url_len = coap_get_header_uri_path(packet, &url); + bh_memcpy_s(&mid, sizeof(uint32), packet->token, sizeof(uint32)); + url_len = coap_get_header_uri_path(packet, &url); - /* Send request to tool agent */ - if (url_len >= 12 && memcmp(url, "/tool_agent/", 12) == 0) { - module_data *m_data; - jeff_applet_data *applet_data; - unsigned attr_cont_len = 0, req_msg_len; - bh_queue_msg_t *tool_agent_msg; - bh_request_msg_t *req_msg; - char url_buf[256] = {0}, *p = url_buf; - char applet_name[128] = {0}; + /* Send request to tool agent */ + if (url_len >= 12 && memcmp(url, "/tool_agent/", 12) == 0) { + module_data *m_data; + jeff_applet_data *applet_data; + unsigned attr_cont_len = 0, req_msg_len; + bh_queue_msg_t *tool_agent_msg; + bh_request_msg_t *req_msg; + char url_buf[256] = { 0 }, *p = url_buf; + char applet_name[128] = { 0 }; - /* Resolve applet name */ - bh_memcpy_s(url_buf, sizeof(url_buf), url + 12, url_len - 12); - while (*p != '/' && *p != '\0') + /* Resolve applet name */ + bh_memcpy_s(url_buf, sizeof(url_buf), url + 12, url_len - 12); + while (*p != '/' && *p != '\0') p++; - bh_memcpy_s(applet_name, sizeof(applet_name), url_buf, p - url_buf); - app_manager_printf("Send request to tool agent of applet: %s\n", applet_name); + bh_memcpy_s(applet_name, sizeof(applet_name), url_buf, p - url_buf); + app_manager_printf("Send request to tool agent of applet: %s\n", + applet_name); - /* Check applet name */ - if (!(m_data = app_manager_lookup_module_data(applet_name))) { - SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: invalid applet name"); + /* Check applet name */ + if (!(m_data = app_manager_lookup_module_data(applet_name))) { + SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: " + "invalid applet name"); + return false; + } + + applet_data = (jeff_applet_data *)m_data->internal_data; + /* Attach debug: start the tool agent firstly */ + if (packet->code == COAP_PUT) { + if (is_tool_agent_running(m_data)) { + SEND_ERR_RESPONSE(mid, "Attach debug failed: tool " + "agent is already exist."); return false; } - applet_data = (jeff_applet_data*)m_data->internal_data; - /* Attach debug: start the tool agent firstly */ - if (packet->code == COAP_PUT) { - if (is_tool_agent_running(m_data)) { - SEND_ERR_RESPONSE(mid, "Attach debug failed: tool agent is already exist."); - return false; - } + applet_data->debug_mode = true; - applet_data->debug_mode = true; - - /* Create tool agent queue */ - if (!(applet_data->tool_agent_queue = bh_queue_create())) { - SEND_ERR_RESPONSE(mid, "Attach debug failed: create tool agent queue failed."); - return false; - } - - /* Start tool agent thread */ - if (!jeff_tool_start_agent(applet_data->vm_instance, applet_data->tool_agent_queue)) { - bh_queue_destroy(applet_data->tool_agent_queue, NULL); - SEND_ERR_RESPONSE(mid, "Attach debug failed: start tool agent failed"); - return false; - } - - app_manager_printf("Attach debug: start tool agent of applet %s success.\n", applet_name); - app_send_response_to_host(mid, CREATED_2_01, NULL); /* OK */ - } else { - /* Check tool agent running */ - if (!is_tool_agent_running(m_data)) { - SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: tool agent is not running"); - return false; - } - - /* Create queue message for tool agent */ - if (!(tool_agent_msg = APP_MGR_MALLOC(sizeof(bh_queue_msg_t)))) { - SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: allocate memory failed"); - return false; - } - - if (attr_cont) - attr_cont_len = attr_container_get_serialize_length(attr_cont); - - req_msg_len = sizeof(bh_request_msg_t) + strlen(p) + 1 + attr_cont_len; - - /* Create request message */ - if (!(req_msg = APP_MGR_MALLOC(req_msg_len))) { - SEND_ERR_RESPONSE(mid, "Send request to applet failed: allocate memory failed"); - APP_MGR_FREE(tool_agent_msg); - return false; - } - - /* Set request message */ - memset(req_msg, 0, req_msg_len); - req_msg->mid = mid; - req_msg->url = (char*)req_msg + sizeof(bh_request_msg_t); - bh_strcpy_s(req_msg->url, strlen(p)+1, p); /* Actual url sent to tool agent */ - req_msg->action = packet->code; - req_msg->fmt = 0; - if (attr_cont) { - req_msg->payload = (char*)req_msg + sizeof(bh_request_msg_t) + - strlen(p) + 1; - attr_container_serialize(req_msg->payload, attr_cont); - } - - /* Set queue message and send to tool agent's queue */ - tool_agent_msg->message_type = JDWP_REQUEST; - tool_agent_msg->payload_size = req_msg_len; - tool_agent_msg->payload = (char*)req_msg; - if (!bh_queue_send_message(applet_data->tool_agent_queue, tool_agent_msg)) { - APP_MGR_FREE(req_msg); - APP_MGR_FREE(tool_agent_msg); - SEND_ERR_RESPONSE - (mid, "Send request to tool agent failed: send queue msg failed."); - return false; - } - - /* app_manager_printf("Send request to tool agent of applet %s success.\n", applet_name); */ + /* Create tool agent queue */ + if (!(applet_data->tool_agent_queue = bh_queue_create())) { + SEND_ERR_RESPONSE(mid, "Attach debug failed: create " + "tool agent queue failed."); + return false; } - return true; - } - } -#endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */ - return false; - } + /* Start tool agent thread */ + if (!jeff_tool_start_agent(applet_data->vm_instance, + applet_data->tool_agent_queue)) { + bh_queue_destroy(applet_data->tool_agent_queue, NULL); + SEND_ERR_RESPONSE( + mid, "Attach debug failed: start tool agent failed"); + return false; + } - static module_data* - jeff_module_get_module_data(void) - { - JeffThreadLocalRoot *self = jeff_runtime_get_tlr(); - return (module_data *)self->il_root->start_routine_arg; + app_manager_printf("Attach debug: start tool agent of " + "applet %s success.\n", + applet_name); + app_send_response_to_host(mid, CREATED_2_01, NULL); /* OK */ + } + else { + /* Check tool agent running */ + if (!is_tool_agent_running(m_data)) { + SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: " + "tool agent is not running"); + return false; + } + + /* Create queue message for tool agent */ + if (!(tool_agent_msg = + APP_MGR_MALLOC(sizeof(bh_queue_msg_t)))) { + SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: " + "allocate memory failed"); + return false; + } + + if (attr_cont) + attr_cont_len = + attr_container_get_serialize_length(attr_cont); + + req_msg_len = + sizeof(bh_request_msg_t) + strlen(p) + 1 + attr_cont_len; + + /* Create request message */ + if (!(req_msg = APP_MGR_MALLOC(req_msg_len))) { + SEND_ERR_RESPONSE(mid, "Send request to applet failed: " + "allocate memory failed"); + APP_MGR_FREE(tool_agent_msg); + return false; + } + + /* Set request message */ + memset(req_msg, 0, req_msg_len); + req_msg->mid = mid; + req_msg->url = (char *)req_msg + sizeof(bh_request_msg_t); + bh_strcpy_s(req_msg->url, strlen(p) + 1, + p); /* Actual url sent to tool agent */ + req_msg->action = packet->code; + req_msg->fmt = 0; + if (attr_cont) { + req_msg->payload = (char *)req_msg + + sizeof(bh_request_msg_t) + strlen(p) + + 1; + attr_container_serialize(req_msg->payload, attr_cont); + } + + /* Set queue message and send to tool agent's queue */ + tool_agent_msg->message_type = JDWP_REQUEST; + tool_agent_msg->payload_size = req_msg_len; + tool_agent_msg->payload = (char *)req_msg; + if (!bh_queue_send_message(applet_data->tool_agent_queue, + tool_agent_msg)) { + APP_MGR_FREE(req_msg); + APP_MGR_FREE(tool_agent_msg); + SEND_ERR_RESPONSE(mid, "Send request to tool agent failed: " + "send queue msg failed."); + return false; + } + + /* app_manager_printf("Send request to tool agent of applet + * %s success.\n", applet_name); */ + } + + return true; + } } +#endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */ + return false; +} + +static module_data * +jeff_module_get_module_data(void) +{ + JeffThreadLocalRoot *self = jeff_runtime_get_tlr(); + return (module_data *)self->il_root->start_routine_arg; +} #if BEIHAI_ENABLE_TOOL_AGENT != 0 -#define JDWP_HANDSHAKE_MAGIC "JDWP-Handshake" -#define JDWP_HANDSHAKE_LEN (sizeof (JDWP_HANDSHAKE_MAGIC) - 1) +#define JDWP_HANDSHAKE_MAGIC "JDWP-Handshake" +#define JDWP_HANDSHAKE_LEN (sizeof(JDWP_HANDSHAKE_MAGIC) - 1) -#define JDWP_PAYLOAD_KEY "jdwp" +#define JDWP_PAYLOAD_KEY "jdwp" - static bool debug = true; +static bool debug = true; - static bool - send_msg_to_host (int mid, const char *url, int code, const uint8 *msg, unsigned size) - { - bool ret; - int payload_len = 0; - attr_container_t *payload = NULL; +static bool +send_msg_to_host(int mid, const char *url, int code, const uint8 *msg, + unsigned size) +{ + bool ret; + int payload_len = 0; + attr_container_t *payload = NULL; - if (msg) { - if ((payload = attr_container_create(""))) { - attr_container_set_bytearray(&payload, JDWP_PAYLOAD_KEY, (const int8_t *)msg, size); - payload_len = attr_container_get_serialize_length(payload); - } + if (msg) { + if ((payload = attr_container_create(""))) { + attr_container_set_bytearray(&payload, JDWP_PAYLOAD_KEY, + (const int8_t *)msg, size); + payload_len = attr_container_get_serialize_length(payload); } - ret = app_send_msg_to_host(mid, url, code, (char*)payload, payload_len); + } + ret = app_send_msg_to_host(mid, url, code, (char *)payload, payload_len); - if (payload) + if (payload) attr_container_destroy(payload); - return ret; - } + return ret; +} - static bool - send_response(int mid, int code, const uint8 *msg, unsigned size) - { - return send_msg_to_host(mid, NULL, code, msg, size); - } +static bool +send_response(int mid, int code, const uint8 *msg, unsigned size) +{ + return send_msg_to_host(mid, NULL, code, msg, size); +} - static bool - send_packet_response(int mid, int code, JeffBuffer *packet) - { - int size; +static bool +send_packet_response(int mid, int code, JeffBuffer *packet) +{ + int size; - if ((size = jeff_buffer_size(packet)) == 0) + if ((size = jeff_buffer_size(packet)) == 0) /* No data need to be written, succeed. */ return true; - return send_msg_to_host(mid, NULL, code, jeff_buffer_at(packet, 0), size); - } + return send_msg_to_host(mid, NULL, code, jeff_buffer_at(packet, 0), size); +} - void - jeff_tool_event_publish(uint8 *evtbuf, unsigned size) - { - char *prefix = "/jdwp/", *url = NULL; - int url_len; +void +jeff_tool_event_publish(uint8 *evtbuf, unsigned size) +{ + char *prefix = "/jdwp/", *url = NULL; + int url_len; - url_len = strlen(prefix) + strlen(app_manager_get_module_name(Module_Jeff)); - if (NULL == (url = jeff_runtime_malloc(url_len + 1))) + url_len = strlen(prefix) + strlen(app_manager_get_module_name(Module_Jeff)); + if (NULL == (url = jeff_runtime_malloc(url_len + 1))) return; - bh_strcpy_s(url,url_len + 1, prefix); - bh_strcat_s(url,url_len + 1, app_manager_get_module_name(Module_Jeff)); + bh_strcpy_s(url, url_len + 1, prefix); + bh_strcat_s(url, url_len + 1, app_manager_get_module_name(Module_Jeff)); - /* Event is sent as request so we set code as COAP_PUT */ - if (event_is_registered(url)) + /* Event is sent as request so we set code as COAP_PUT */ + if (event_is_registered(url)) send_msg_to_host(0, url, COAP_PUT, evtbuf, size); - jeff_runtime_free(url); - } + jeff_runtime_free(url); +} -#define SEND_ERROR_RESPONSE(err_msg) do { \ - app_manager_printf("%s\n", err_msg); \ - send_response(req_msg->mid, INTERNAL_SERVER_ERROR_5_00,\ - (uint8 *)err_msg, strlen(err_msg) + 1); \ - } while (0) +#define SEND_ERROR_RESPONSE(err_msg) \ + do { \ + app_manager_printf("%s\n", err_msg); \ + send_response(req_msg->mid, INTERNAL_SERVER_ERROR_5_00, \ + (uint8 *)err_msg, strlen(err_msg) + 1); \ + } while (0) - /* Queue callback of tool agent */ - void - tool_agent_queue_callback(void *arg) - { - bh_queue_msg_t *msg = (bh_queue_msg_t*)arg; +/* Queue callback of tool agent */ +void +tool_agent_queue_callback(void *arg) +{ + bh_queue_msg_t *msg = (bh_queue_msg_t *)arg; - if (msg->message_type == JDWP_REQUEST) { - bh_request_msg_t *req_msg = (bh_request_msg_t*)msg->payload; - attr_container_t *attr_cont = (attr_container_t*)req_msg->payload; - JeffThreadLocalRoot *self = jeff_runtime_get_tlr(); - JeffInstanceLocalRoot *cur_instance = self->il_root; - JeffToolAgent *agent = cur_instance->tool_agent; - bh_queue *queue = (bh_queue *)self->start_routine_arg; + if (msg->message_type == JDWP_REQUEST) { + bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; + attr_container_t *attr_cont = (attr_container_t *)req_msg->payload; + JeffThreadLocalRoot *self = jeff_runtime_get_tlr(); + JeffInstanceLocalRoot *cur_instance = self->il_root; + JeffToolAgent *agent = cur_instance->tool_agent; + bh_queue *queue = (bh_queue *)self->start_routine_arg; - if (debug) - app_manager_printf("Tool Agent of applet %s got request, url %s, action %d\n", - app_manager_get_module_name(Module_Jeff), req_msg->url, req_msg->action); + if (debug) + app_manager_printf( + "Tool Agent of applet %s got request, url %s, action %d\n", + app_manager_get_module_name(Module_Jeff), req_msg->url, + req_msg->action); - /* Handshake or Process Request */ - if (req_msg->action == COAP_GET) { - uint8 *buf; - unsigned buf_len; + /* Handshake or Process Request */ + if (req_msg->action == COAP_GET) { + uint8 *buf; + unsigned buf_len; - if (!attr_cont - || !(buf = (uint8*) - attr_container_get_as_bytearray(attr_cont, JDWP_PAYLOAD_KEY, &buf_len))) { - SEND_ERROR_RESPONSE("Tool Agent fail: invalid JDWP payload."); - goto fail; - } - - if (!agent->connected) { - if (buf_len != JDWP_HANDSHAKE_LEN - || memcmp (buf, JDWP_HANDSHAKE_MAGIC, JDWP_HANDSHAKE_LEN)) { - SEND_ERROR_RESPONSE("Tool Agent fail: handshake fail."); - goto fail; - } - - /* Handshake success and response */ - agent->connected = true; - send_response(req_msg->mid, CONTENT_2_05, buf, buf_len); - } else { - /* TODO: tool-agent thread should reuse the request/reply buffer to avoid allocating memory repeatedly */ - JeffBuffer request, reply; - - /* Initialize the package buffers. */ - jeff_buffer_init(&request); - jeff_buffer_init(&reply); - - if (!jeff_buffer_resize(&request, buf_len)) { - SEND_ERROR_RESPONSE("Tool Agent fail: resize buffer fail."); - jeff_buffer_destroy(&request); - jeff_buffer_destroy(&reply); - goto fail; - } - - /* Copy data from request to jeff buffer */ - bh_memcpy_s(jeff_buffer_at(&request, 0), jeff_buffer_size(&request), buf, buf_len); - - /* Handle JDWP request */ - if (!jeff_tool_handle_packet(agent, &request, &reply)) { - SEND_ERROR_RESPONSE("Tool agent fail: handle request fail."); - jeff_buffer_destroy(&request); - jeff_buffer_destroy(&reply); - goto fail; - } - - /* Response JDWP reply */ - send_packet_response(req_msg->mid, CONTENT_2_05, &reply); - - /* Destroy the package buffers. */ - jeff_buffer_destroy(&request); - jeff_buffer_destroy(&reply); - } - } - /* Debugger disconnect */ - else if (req_msg->action == COAP_DELETE) { - send_response(req_msg->mid, DELETED_2_02, NULL, 0); - bh_queue_exit_loop_run(queue); - } - else { - SEND_ERROR_RESPONSE("Tool agent fail: invalid request."); + if (!attr_cont + || !(buf = (uint8 *)attr_container_get_as_bytearray( + attr_cont, JDWP_PAYLOAD_KEY, &buf_len))) { + SEND_ERROR_RESPONSE("Tool Agent fail: invalid JDWP payload."); goto fail; } - APP_MGR_FREE(req_msg); - APP_MGR_FREE(msg); - return; + if (!agent->connected) { + if (buf_len != JDWP_HANDSHAKE_LEN + || memcmp(buf, JDWP_HANDSHAKE_MAGIC, JDWP_HANDSHAKE_LEN)) { + SEND_ERROR_RESPONSE("Tool Agent fail: handshake fail."); + goto fail; + } - fail: + /* Handshake success and response */ + agent->connected = true; + send_response(req_msg->mid, CONTENT_2_05, buf, buf_len); + } + else { + /* TODO: tool-agent thread should reuse the request/reply + * buffer to avoid allocating memory repeatedly */ + JeffBuffer request, reply; + + /* Initialize the package buffers. */ + jeff_buffer_init(&request); + jeff_buffer_init(&reply); + + if (!jeff_buffer_resize(&request, buf_len)) { + SEND_ERROR_RESPONSE("Tool Agent fail: resize buffer fail."); + jeff_buffer_destroy(&request); + jeff_buffer_destroy(&reply); + goto fail; + } + + /* Copy data from request to jeff buffer */ + bh_memcpy_s(jeff_buffer_at(&request, 0), + jeff_buffer_size(&request), buf, buf_len); + + /* Handle JDWP request */ + if (!jeff_tool_handle_packet(agent, &request, &reply)) { + SEND_ERROR_RESPONSE( + "Tool agent fail: handle request fail."); + jeff_buffer_destroy(&request); + jeff_buffer_destroy(&reply); + goto fail; + } + + /* Response JDWP reply */ + send_packet_response(req_msg->mid, CONTENT_2_05, &reply); + + /* Destroy the package buffers. */ + jeff_buffer_destroy(&request); + jeff_buffer_destroy(&reply); + } + } + /* Debugger disconnect */ + else if (req_msg->action == COAP_DELETE) { + send_response(req_msg->mid, DELETED_2_02, NULL, 0); bh_queue_exit_loop_run(queue); - APP_MGR_FREE(req_msg); + } + else { + SEND_ERROR_RESPONSE("Tool agent fail: invalid request."); + goto fail; } + APP_MGR_FREE(req_msg); APP_MGR_FREE(msg); + return; + + fail: + bh_queue_exit_loop_run(queue); + APP_MGR_FREE(req_msg); } - void - tool_agent_queue_free_callback(void *message) - { - bh_queue_msg_t *msg = (bh_queue_msg_t*)message; + APP_MGR_FREE(msg); +} - if (msg->message_type == JDWP_REQUEST) { - bh_request_msg_t *req_msg = (bh_request_msg_t*)msg->payload; - APP_MGR_FREE(req_msg); - } +void +tool_agent_queue_free_callback(void *message) +{ + bh_queue_msg_t *msg = (bh_queue_msg_t *)message; - APP_MGR_FREE(msg); + if (msg->message_type == JDWP_REQUEST) { + bh_request_msg_t *req_msg = (bh_request_msg_t *)msg->payload; + APP_MGR_FREE(req_msg); } -#endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */ + APP_MGR_FREE(msg); +} - module_interface jeff_module_interface = { - jeff_module_init, - jeff_module_install, - jeff_module_uninstall, - jeff_module_watchdog_kill, - jeff_module_handle_host_url, - jeff_module_get_module_data, - NULL - }; +#endif /* BEIHAI_ENABLE_TOOL_AGENT != 0 */ + +/* clang-format off */ +module_interface jeff_module_interface = { + jeff_module_init, + jeff_module_install, + jeff_module_uninstall, + jeff_module_watchdog_kill, + jeff_module_handle_host_url, + jeff_module_get_module_data, + NULL +}; +/* clang-format on */ #endif diff --git a/core/app-mgr/app-manager/module_utils.c b/core/app-mgr/app-manager/module_utils.c index 561ef24fe..b4b25e4a9 100644 --- a/core/app-mgr/app-manager/module_utils.c +++ b/core/app-mgr/app-manager/module_utils.c @@ -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; } - diff --git a/core/app-mgr/app-manager/module_wasm_app.c b/core/app-mgr/app-manager/module_wasm_app.c index f7752bd5b..dd86524db 100644 --- a/core/app-mgr/app-manager/module_wasm_app.c +++ b/core/app-mgr/app-manager/module_wasm_app.c @@ -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,174 +207,180 @@ 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: { - request_t *request = (request_t *)bh_message_payload(queue_msg); - int size; - char *buffer; - int32 buffer_offset; + 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, - request->action); + app_manager_printf("App %s got request, url %s, action %d\n", + m_data->module_name, request->url, + request->action); - func_onRequest = app_manager_lookup_function(inst, - "_on_request", - "(i32i32)"); - if (!func_onRequest) { - app_manager_printf("Cannot find function onRequest\n"); - break; - } + func_onRequest = app_manager_lookup_function( + inst, "_on_request", "(i32i32)"); + if (!func_onRequest) { + app_manager_printf("Cannot find function onRequest\n"); + break; + } - buffer = pack_request(request, &size); - if (buffer == NULL) - break; + buffer = pack_request(request, &size); + if (buffer == NULL) + break; - 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); - wasm_runtime_clear_exception(inst); - } - free_req_resp_packet(buffer); - break; - } + 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); + wasm_runtime_clear_exception(inst); + } + free_req_resp_packet(buffer); + break; + } - free_req_resp_packet(buffer); + 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)) { - const char *exception = wasm_runtime_get_exception(inst); - bh_assert(exception); - app_manager_printf("Got exception running wasm code: %s\n", - exception); - wasm_runtime_clear_exception(inst); - wasm_runtime_module_free(inst, buffer_offset); - break; - } + 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", + exception); + wasm_runtime_clear_exception(inst); + wasm_runtime_module_free(inst, buffer_offset); + break; + } - wasm_runtime_module_free(inst, buffer_offset); - app_manager_printf("Wasm app process request success.\n"); - break; + wasm_runtime_module_free(inst, buffer_offset); + app_manager_printf("Wasm app process request success.\n"); + break; } - case RESTFUL_RESPONSE: { - wasm_function_inst_t func_onResponse; - response_t *response = (response_t *) bh_message_payload(queue_msg); - int size; - char *buffer; - int32 buffer_offset; + case RESTFUL_RESPONSE: + { + wasm_function_inst_t func_onResponse; + response_t *response = + (response_t *)bh_message_payload(queue_msg); + int size; + char *buffer; + int32 buffer_offset; - app_manager_printf("App %s got response_t,status %d\n", - m_data->module_name, response->status); + 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)"); - if (!func_onResponse) { - app_manager_printf("Cannot find function on_response\n"); - break; - } + func_onResponse = app_manager_lookup_function( + inst, "_on_response", "(i32i32)"); + if (!func_onResponse) { + app_manager_printf("Cannot find function on_response\n"); + break; + } - buffer = pack_response(response, &size); - if (buffer == NULL) - break; + buffer = pack_response(response, &size); + if (buffer == NULL) + break; - 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); - wasm_runtime_clear_exception(inst); - } - free_req_resp_packet(buffer); - break; - } + 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); + wasm_runtime_clear_exception(inst); + } + free_req_resp_packet(buffer); + break; + } - free_req_resp_packet(buffer); + 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)) { - const char *exception = wasm_runtime_get_exception(inst); - bh_assert(exception); - app_manager_printf("Got exception running wasm code: %s\n", - exception); - wasm_runtime_clear_exception(inst); - wasm_runtime_module_free(inst, buffer_offset); - break; - } + 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", + exception); + wasm_runtime_clear_exception(inst); + wasm_runtime_module_free(inst, buffer_offset); + break; + } - wasm_runtime_module_free(inst, buffer_offset); - app_manager_printf("Wasm app process response success.\n"); - break; + wasm_runtime_module_free(inst, buffer_offset); + app_manager_printf("Wasm app process response success.\n"); + break; } - 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"); - break; - + 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"); + break; } } } else { switch (message_type) { - 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)"); + 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)"); - if (!func_onTimer) { - 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); - bh_assert(exception); - app_manager_printf("Got exception running wasm code: %s\n", - exception); - wasm_runtime_clear_exception(inst); - } - } - break; + if (!func_onTimer) { + 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); + bh_assert(exception); + app_manager_printf( + "Got exception running wasm code: %s\n", exception); + wasm_runtime_clear_exception(inst); + } + } + break; } - 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"); - break; + 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"); + 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,12 +449,13 @@ 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", - exception); + 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, - "Install WASM app failed: prepare wasi env failed."); + 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, - "Install WASM app failed: prepare wasi env failed."); + 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,15 +774,15 @@ 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, - "Install WASM app failed: invalid wasm package type."); + 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; @@ -802,8 +813,9 @@ 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."); + wasm_runtime_create_exec_env(inst, DEFAULT_WASM_STACK_SIZE))) { + 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,8 +863,9 @@ wasm_app_module_install(request_t * msg) /* Initialize watchdog timer */ if (!watchdog_timer_init(m_data)) { - SEND_ERR_RESPONSE(msg->mid, - "Install WASM app failed: create app watchdog timer failed."); + 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,13 +960,15 @@ 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, - "Uninstall WASM app failed: app is being interrupted by watchdog."); + 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; @@ -1001,7 +1019,7 @@ wasm_app_module_watchdog_kill(module_data *m_data) bool wasm_register_msg_callback(int message_type, - message_type_handler_t message_handler) + message_type_handler_t message_handler) { int i; int freeslot = -1; @@ -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,18 +1059,19 @@ wasm_register_cleanup_callback(resource_cleanup_handler_t handler) return false; } -#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)) { \ - if (sizeof(value) == 4) \ - value = ntohl(value); \ - else if (sizeof(value) == 2) \ - value = ntohs(value); \ - recv_ctx.phase = next_phase; \ - recv_ctx.size_in_phase = 0; \ - } \ - } while(0) +#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)) { \ + if (sizeof(value) == 4) \ + value = ntohl(value); \ + else if (sizeof(value) == 2) \ + value = ntohs(value); \ + recv_ctx.phase = next_phase; \ + recv_ctx.size_in_phase = 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,19 +1157,18 @@ 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) { #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0 case Wasm_Module_Bytecode: recv_ctx.message.app_file.u.bytecode.magic = - recv_ctx.message.app_file_magic; + recv_ctx.message.app_file_magic; recv_ctx.phase = Phase_Wasm_Version; recv_ctx.size_in_phase = 0; break; @@ -1158,7 +1176,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch, #if WASM_ENABLE_AOT != 0 case Wasm_Module_AoT: recv_ctx.message.app_file.u.aot.magic = - recv_ctx.message.app_file_magic; + recv_ctx.message.app_file_magic; recv_ctx.phase = Phase_AOT_Version; recv_ctx.size_in_phase = 0; break; @@ -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,10 +1283,12 @@ 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, - "Install WASM app failed: allocate memory failed"); + SEND_ERR_RESPONSE( + recv_ctx.message.request_mid, + "Install WASM app failed: allocate memory failed"); goto fail; } recv_ctx.phase = Phase_Wasm_Section_Content; @@ -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*)§ion->section_body_size; + p = (uint8 *)§ion->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"); @@ -1441,7 +1467,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch, } else { if (!(section->section_body = - APP_MGR_MALLOC(section->section_body_size))) { + APP_MGR_MALLOC(section->section_body_size))) { app_manager_printf("Allocate memory failed!\n"); SEND_ERR_RESPONSE(recv_ctx.message.request_mid, "Install WASM app failed: " @@ -1466,12 +1492,12 @@ 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, - 0, total_size - section->section_body_size); + memset(section->section_body + section->section_body_size, + 0, total_size - section->section_body_size); section->section_body_size = total_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 - diff --git a/core/app-mgr/app-manager/module_wasm_app.h b/core/app-mgr/app-manager/module_wasm_app.h index 8965a975f..8a7ae4e54 100644 --- a/core/app-mgr/app-manager/module_wasm_app.h +++ b/core/app-mgr/app-manager/module_wasm_app.h @@ -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, - message_type_handler_t message_handler); +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 diff --git a/core/app-mgr/app-manager/module_wasm_lib.c b/core/app-mgr/app-manager/module_wasm_lib.c index bdf3215c8..0b5c07ea7 100644 --- a/core/app-mgr/app-manager/module_wasm_lib.c +++ b/core/app-mgr/app-manager/module_wasm_lib.c @@ -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, - wasm_lib_module_get_module_data, - NULL }; - +/* 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 +}; +/* clang-format on */ diff --git a/core/app-mgr/app-manager/platform/linux/app_mgr_linux.c b/core/app-mgr/app-manager/platform/linux/app_mgr_linux.c index 45957bc18..5e51788bc 100644 --- a/core/app-mgr/app-manager/platform/linux/app_mgr_linux.c +++ b/core/app-mgr/app-manager/platform/linux/app_mgr_linux.c @@ -5,25 +5,28 @@ #include "app_manager.h" -void* -app_manager_timer_create(void (*timer_callback)(void*), - watchdog_timer *wd_timer) +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, - const uint8_t *signature, unsigned int sig_size) +int +app_manager_signature_verify(const uint8_t *file, unsigned int file_len, + const uint8_t *signature, unsigned int sig_size) { return 1; } - diff --git a/core/app-mgr/app-manager/platform/zephyr/app_mgr_zephyr.c b/core/app-mgr/app-manager/platform/zephyr/app_mgr_zephyr.c index cca66549c..650e536f1 100644 --- a/core/app-mgr/app-manager/platform/zephyr/app_mgr_zephyr.c +++ b/core/app-mgr/app-manager/platform/zephyr/app_mgr_zephyr.c @@ -16,32 +16,36 @@ typedef struct k_timer_watchdog { watchdog_timer *wd_timer; } k_timer_watchdog; -void* -app_manager_timer_create(void (*timer_callback)(void*), - watchdog_timer *wd_timer) +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, - NULL); + k_timer_init(&timer->timer, (void (*)(struct k_timer *))timer_callback, + NULL); timer->wd_timer = wd_timer; } 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, diff --git a/core/app-mgr/app-manager/resource_reg.c b/core/app-mgr/app-manager/resource_reg.c index fbeb803ae..4e930890e 100644 --- a/core/app-mgr/app-manager/resource_reg.c +++ b/core/app-mgr/app-manager/resource_reg.c @@ -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; @@ -54,10 +54,11 @@ void module_request_handler(request_t *request, void *user_data) } app_manager_printf("Send request to app %s success.\n", - m_data->module_name); + m_data->module_name); } -void targeted_app_request_handler(request_t *request, void *unused) +void +targeted_app_request_handler(request_t *request, void *unused) { char applet_name[128] = { 0 }; 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); @@ -84,16 +86,17 @@ void targeted_app_request_handler(request_t *request, void *unused) m_data = module_data_list_lookup(applet_name); if (!m_data) { SEND_ERR_RESPONSE(request->mid, - "Send request to applet failed: invalid applet name"); + "Send request to applet failed: invalid applet name"); goto end; } 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,15 +104,15 @@ 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; } bh_message_t msg = bh_new_msg(RESTFUL_RESPONSE, resp_for_send, - sizeof(*resp_for_send), response_cleaner); + sizeof(*resp_for_send), response_cleaner); if (!msg) { response_cleaner(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, - void (*request_handler)(request_t *, void *), - uint32 register_id) +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; diff --git a/core/app-mgr/app-manager/watchdog.c b/core/app-mgr/app-manager/watchdog.c index 6ed88bbdb..ba5bb05f5 100644 --- a/core/app-mgr/app-manager/watchdog.c +++ b/core/app-mgr/app-manager/watchdog.c @@ -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); @@ -26,14 +27,15 @@ static void watchdog_timer_callback(void *timer) wd_timer->is_interrupting = true; bh_post_msg(watchdog_queue, WD_TIMEOUT, wd_timer->module_data, - sizeof(module_data)); + sizeof(module_data)); } os_mutex_unlock(&wd_timer->lock); } #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; @@ -42,7 +44,7 @@ bool watchdog_timer_init(module_data *m_data) return false; if (!(wd_timer->timer_handle = - app_manager_timer_create(watchdog_timer_callback, wd_timer))) { + app_manager_timer_create(watchdog_timer_callback, wd_timer))) { os_mutex_destroy(&wd_timer->lock); return false; } @@ -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,32 +65,35 @@ 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); wd_timer->is_interrupting = false; wd_timer->is_stopped = false; app_manager_timer_start(wd_timer->timer_handle, - wd_timer->module_data->timeout); + wd_timer->module_data->timeout); 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) { g_module_interfaces[m_data->module_type]->module_watchdog_kill( - m_data); + m_data); app_manager_post_applets_update_event(); } } @@ -95,22 +101,23 @@ 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( - "App Manager start failed: create watchdog queue failed.\n"); + "App Manager start failed: create watchdog queue failed.\n"); return false; } #if 0 @@ -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); diff --git a/core/app-mgr/app-manager/watchdog.h b/core/app-mgr/app-manager/watchdog.h index c1478d40e..d960df03b 100644 --- a/core/app-mgr/app-manager/watchdog.h +++ b/core/app-mgr/app-manager/watchdog.h @@ -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 diff --git a/core/app-mgr/app-mgr-shared/app_manager_export.h b/core/app-mgr/app-mgr-shared/app_manager_export.h index 4d8ced8d5..d9a8c0445 100644 --- a/core/app-mgr/app-mgr-shared/app_manager_export.h +++ b/core/app-mgr/app-mgr-shared/app_manager_export.h @@ -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,12 +114,13 @@ 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) ( - uint8 ch, int total_size, int *received_total_size); +typedef bool (*module_on_install_request_byte_arrive_func)( + uint8 ch, int total_size, int *received_total_size); /* Interfaces of each module */ typedef struct module_interface { @@ -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 - diff --git a/core/iwasm/aot/arch/aot_reloc_x86_32.c b/core/iwasm/aot/arch/aot_reloc_x86_32.c index 40f8ec0cc..5a8a944b4 100644 --- a/core/iwasm/aot/arch/aot_reloc_x86_32.c +++ b/core/iwasm/aot/arch/aot_reloc_x86_32.c @@ -5,9 +5,9 @@ #include "aot_reloc.h" -#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_32 1 /* Direct 32 bit */ +#define R_386_PC32 2 /* PC relative 32 bit */ +#define R_386_PLT32 4 /* 32-bit address ProcedureLinkageTable */ #if !defined(_WIN32) && !defined(_WIN32_) /* clang-format off */ diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 871f0708a..881da8b6c 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -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 /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 diff --git a/test-tools/binarydump-tool/binarydump.c b/test-tools/binarydump-tool/binarydump.c index cd19eecad..050de6dfa 100644 --- a/test-tools/binarydump-tool/binarydump.c +++ b/test-tools/binarydump-tool/binarydump.c @@ -8,30 +8,30 @@ #include #include -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,20 +41,19 @@ read_file_to_buffer (const char *filename, int *ret_size) } static int -print_help () +print_help() { - printf ("Usage: binarydump -o -n input_file\n"); - printf ("Options:\n"); - printf (" -o Place the output into \n"); - printf (" -n The name of array \n"); + printf("Usage: binarydump -o -n input_file\n"); + printf("Options:\n"); + printf(" -o Place the output into \n"); + printf(" -n The name of array \n"); return -1; } static bool -bin_file_dump (const unsigned char *file, int size, - const char *bin_file_output, - const char *array_name) +bin_file_dump(const unsigned char *file, int size, const char *bin_file_output, + const char *array_name) { unsigned i = 0; const unsigned char *p = file, *p_end = file + size; @@ -63,7 +62,8 @@ bin_file_dump (const unsigned char *file, int size, if (!file_output) 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; } diff --git a/test-tools/component_test/README.md b/test-tools/component-test/README.md similarity index 100% rename from test-tools/component_test/README.md rename to test-tools/component-test/README.md diff --git a/test-tools/component_test/__init__.py b/test-tools/component-test/__init__.py similarity index 100% rename from test-tools/component_test/__init__.py rename to test-tools/component-test/__init__.py diff --git a/test-tools/component_test/framework/__init__.py b/test-tools/component-test/framework/__init__.py similarity index 100% rename from test-tools/component_test/framework/__init__.py rename to test-tools/component-test/framework/__init__.py diff --git a/test-tools/component_test/framework/case_base.py b/test-tools/component-test/framework/case_base.py similarity index 100% rename from test-tools/component_test/framework/case_base.py rename to test-tools/component-test/framework/case_base.py diff --git a/test-tools/component_test/framework/engine.py b/test-tools/component-test/framework/engine.py similarity index 100% rename from test-tools/component_test/framework/engine.py rename to test-tools/component-test/framework/engine.py diff --git a/test-tools/component_test/framework/framework.py b/test-tools/component-test/framework/framework.py similarity index 100% rename from test-tools/component_test/framework/framework.py rename to test-tools/component-test/framework/framework.py diff --git a/test-tools/component_test/framework/suite.py b/test-tools/component-test/framework/suite.py similarity index 100% rename from test-tools/component_test/framework/suite.py rename to test-tools/component-test/framework/suite.py diff --git a/test-tools/component_test/framework/test_api.py b/test-tools/component-test/framework/test_api.py similarity index 100% rename from test-tools/component_test/framework/test_api.py rename to test-tools/component-test/framework/test_api.py diff --git a/test-tools/component_test/framework/test_utils.py b/test-tools/component-test/framework/test_utils.py similarity index 100% rename from test-tools/component_test/framework/test_utils.py rename to test-tools/component-test/framework/test_utils.py diff --git a/test-tools/component_test/harness/__init__.py b/test-tools/component-test/harness/__init__.py similarity index 100% rename from test-tools/component_test/harness/__init__.py rename to test-tools/component-test/harness/__init__.py diff --git a/test-tools/component_test/harness/harness_api.py b/test-tools/component-test/harness/harness_api.py similarity index 100% rename from test-tools/component_test/harness/harness_api.py rename to test-tools/component-test/harness/harness_api.py diff --git a/test-tools/component_test/host-clients/src/host_app_sample.c b/test-tools/component-test/host-clients/src/host_app_sample.c similarity index 81% rename from test-tools/component_test/host-clients/src/host_app_sample.c rename to test-tools/component-test/host-clients/src/host_app_sample.c index 1fb4ba121..c4010de6a 100644 --- a/test-tools/component_test/host-clients/src/host_app_sample.c +++ b/test-tools/component-test/host-clients/src/host_app_sample.c @@ -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); + 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]; @@ -94,15 +100,15 @@ static void install_applet(int index) printf("Installing TestApplet%d...\n", index); snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index); snprintf(applet_file_name, sizeof(applet_file_name), "./TestApplet%d.wasm", - index); + index); buf = read_file_to_buffer(applet_file_name, &size); if (!buf) { printf("Install Applet failed: read file %s error.\n", - applet_file_name); + applet_file_name); 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,51 +125,56 @@ 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, - 10000); + attr_container_get_serialize_length(attr_obj), + PAYLOAD_FORMAT_ATTRIBUTE_OBJECT); + ret = aee_request_send(&req, f_aee_response_handler, (void *)user_context, + 10000); if (ret) { printf("send request to TestApplet1 success\n"); } } -static void register_event(const char *event_path) +static void +register_event(const char *event_path) { hostclient_register_event(event_path, f_aee_event_listener); } -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 diff --git a/test-tools/component_test/host-clients/src/makefile b/test-tools/component-test/host-clients/src/makefile similarity index 100% rename from test-tools/component_test/host-clients/src/makefile rename to test-tools/component-test/host-clients/src/makefile diff --git a/test-tools/component_test/set_dev_env.sh b/test-tools/component-test/set_dev_env.sh similarity index 100% rename from test-tools/component_test/set_dev_env.sh rename to test-tools/component-test/set_dev_env.sh diff --git a/test-tools/component_test/start.py b/test-tools/component-test/start.py similarity index 100% rename from test-tools/component_test/start.py rename to test-tools/component-test/start.py diff --git a/test-tools/component_test/suites/01-life-cycle/__init__.py b/test-tools/component-test/suites/01-life-cycle/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/__init__.py rename to test-tools/component-test/suites/01-life-cycle/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/01-install/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/01-install/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/01-install/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/01-install/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/01-install/case.py b/test-tools/component-test/suites/01-life-cycle/cases/01-install/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/01-install/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/01-install/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/02-request/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/02-request/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/02-request/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/02-request/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/02-request/case.py b/test-tools/component-test/suites/01-life-cycle/cases/02-request/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/02-request/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/02-request/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/03-event/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/03-event/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/03-event/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/03-event/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/03-event/case.py b/test-tools/component-test/suites/01-life-cycle/cases/03-event/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/03-event/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/03-event/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/04-request-internal/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/04-request-internal/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/04-request-internal/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/04-request-internal/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/04-request-internal/case.py b/test-tools/component-test/suites/01-life-cycle/cases/04-request-internal/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/04-request-internal/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/04-request-internal/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/05-event-internal/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/05-event-internal/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/05-event-internal/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/05-event-internal/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/05-event-internal/case.py b/test-tools/component-test/suites/01-life-cycle/cases/05-event-internal/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/05-event-internal/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/05-event-internal/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/06-timer/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/06-timer/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/06-timer/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/06-timer/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/06-timer/case.py b/test-tools/component-test/suites/01-life-cycle/cases/06-timer/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/06-timer/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/06-timer/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/07-sensor/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/07-sensor/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/07-sensor/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/07-sensor/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/07-sensor/case.py b/test-tools/component-test/suites/01-life-cycle/cases/07-sensor/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/07-sensor/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/07-sensor/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/08-on-destroy/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/08-on-destroy/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/08-on-destroy/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/08-on-destroy/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/08-on-destroy/case.py b/test-tools/component-test/suites/01-life-cycle/cases/08-on-destroy/case.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/08-on-destroy/case.py rename to test-tools/component-test/suites/01-life-cycle/cases/08-on-destroy/case.py diff --git a/test-tools/component_test/suites/01-life-cycle/cases/__init__.py b/test-tools/component-test/suites/01-life-cycle/cases/__init__.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/cases/__init__.py rename to test-tools/component-test/suites/01-life-cycle/cases/__init__.py diff --git a/test-tools/component_test/suites/01-life-cycle/suite_setup.py b/test-tools/component-test/suites/01-life-cycle/suite_setup.py similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/suite_setup.py rename to test-tools/component-test/suites/01-life-cycle/suite_setup.py diff --git a/test-tools/component-test/suites/01-life-cycle/test-app/01_install.c b/test-tools/component-test/suites/01-life-cycle/test-app/01_install.c new file mode 100644 index 000000000..5c7153588 --- /dev/null +++ b/test-tools/component-test/suites/01-life-cycle/test-app/01_install.c @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" + +void +on_init() +{ + printf("Hello, I was installed.\n"); +} + +void +on_destroy() +{ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ +} diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/02_request.c b/test-tools/component-test/suites/01-life-cycle/test-app/02_request.c similarity index 66% rename from test-tools/component_test/suites/01-life-cycle/test-app/02_request.c rename to test-tools/component-test/suites/01-life-cycle/test-app/02_request.c index ee7ecafee..251de6ff4 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/02_request.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/02_request.c @@ -1,12 +1,13 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { response_t response[1]; attr_container_t *payload; @@ -18,9 +19,9 @@ void res1_handler(request_t *request) printf("url: %s\n", request->url); printf("action: %d\n", request->action); printf("payload:\n"); - if (request->payload - != NULL&& request->payload_len > 0 && request->fmt == FMT_ATTR_CONTAINER) - attr_container_dump((attr_container_t *) request->payload); + if (request->payload != NULL && request->payload_len > 0 + && request->fmt == FMT_ATTR_CONTAINER) + attr_container_dump((attr_container_t *)request->payload); printf("#### dump request end ###\n"); payload = attr_container_create("wasm app response payload"); @@ -31,15 +32,17 @@ void res1_handler(request_t *request) attr_container_set_string(&payload, "key2", "value2"); make_response_for_request(request, response); - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } -void res2_handler(request_t *request) +void +res2_handler(request_t *request) { response_t response[1]; make_response_for_request(request, response); @@ -49,14 +52,17 @@ void res2_handler(request_t *request) printf("### user resource 2 handler called\n"); } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/res1", res1_handler); api_register_resource_handler("/res2", res2_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/03_event.c b/test-tools/component-test/suites/01-life-cycle/test-app/03_event.c similarity index 67% rename from test-tools/component_test/suites/01-life-cycle/test-app/03_event.c rename to test-tools/component-test/suites/01-life-cycle/test-app/03_event.c index 712110a9b..59cfd0097 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/03_event.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/03_event.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/timer_wasm_app.h" @@ -9,7 +9,8 @@ int num = 0; -void publish_overheat_event() +void +publish_overheat_event() { attr_container_t *event; @@ -19,7 +20,7 @@ void publish_overheat_event() printf("###app publish event begin ###\n"); api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event, - attr_container_get_serialize_length(event)); + attr_container_get_serialize_length(event)); printf("###app publish event end ###\n"); @@ -27,13 +28,15 @@ void publish_overheat_event() } /* Timer callback */ -void timer1_update(user_timer_t timer) +void +timer1_update(user_timer_t timer) { printf("Timer update %d\n", num++); publish_overheat_event(); } -void start_timer() +void +start_timer() { user_timer_t timer; @@ -42,12 +45,15 @@ void start_timer() api_timer_restart(timer, 1000); } -void on_init() +void +on_init() { start_timer(); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_req.c b/test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_req.c similarity index 67% rename from test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_req.c rename to test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_req.c index 3e3ad0e60..99bab9704 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_req.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_req.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" @@ -9,7 +9,8 @@ uint32 mid; unsigned long sender; -void my_response_handler(response_t *response, void *user_data) +void +my_response_handler(response_t *response, void *user_data) { attr_container_t *payload; printf("### user resource 1 handler called\n"); @@ -23,15 +24,17 @@ void my_response_handler(response_t *response, void *user_data) response->mid = mid; response->reciever = sender; - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } -static void test_send_request(const char *url, const char *tag) +static void +test_send_request(const char *url, const char *tag) { request_t request[1]; @@ -39,28 +42,33 @@ static void test_send_request(const char *url, const char *tag) api_send_request(request, my_response_handler, (void *)tag); } -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { mid = request->mid; sender = request->sender; test_send_request("url1", "a general request"); } -void res2_handler(request_t *request) +void +res2_handler(request_t *request) { mid = request->mid; sender = request->sender; test_send_request("/app/App1/url1", "a general request"); } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/res1", res1_handler); api_register_resource_handler("/res2", res2_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_resp.c b/test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_resp.c similarity index 67% rename from test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_resp.c rename to test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_resp.c index 45b25cbd2..13aecb43a 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/04_request_internal_resp.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/04_request_internal_resp.c @@ -1,12 +1,13 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { response_t response[1]; attr_container_t *payload; @@ -19,7 +20,7 @@ void res1_handler(request_t *request) printf("[resp] action: %d\n", request->action); printf("[resp] payload:\n"); if (request->payload != NULL && request->fmt == FMT_ATTR_CONTAINER) - attr_container_dump((attr_container_t *) request->payload); + attr_container_dump((attr_container_t *)request->payload); printf("[resp] #### dump request end ###\n"); payload = attr_container_create("wasm app response payload"); @@ -30,23 +31,27 @@ void res1_handler(request_t *request) attr_container_set_string(&payload, "key2", "value2"); make_response_for_request(request, response); - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("[resp] response payload len %d\n", - attr_container_get_serialize_length(payload)); + attr_container_get_serialize_length(payload)); printf("[resp] reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/url1", res1_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_provider.c b/test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_provider.c similarity index 67% rename from test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_provider.c rename to test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_provider.c index 712110a9b..59cfd0097 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_provider.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_provider.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/timer_wasm_app.h" @@ -9,7 +9,8 @@ int num = 0; -void publish_overheat_event() +void +publish_overheat_event() { attr_container_t *event; @@ -19,7 +20,7 @@ void publish_overheat_event() printf("###app publish event begin ###\n"); api_publish_event("alert/overheat", FMT_ATTR_CONTAINER, event, - attr_container_get_serialize_length(event)); + attr_container_get_serialize_length(event)); printf("###app publish event end ###\n"); @@ -27,13 +28,15 @@ void publish_overheat_event() } /* Timer callback */ -void timer1_update(user_timer_t timer) +void +timer1_update(user_timer_t timer) { printf("Timer update %d\n", num++); publish_overheat_event(); } -void start_timer() +void +start_timer() { user_timer_t timer; @@ -42,12 +45,15 @@ void start_timer() api_timer_restart(timer, 1000); } -void on_init() +void +on_init() { start_timer(); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c b/test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c similarity index 64% rename from test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c rename to test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c index dd031f80a..00e451369 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/05_event_internal_subscriber.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" @@ -9,7 +9,8 @@ uint32 mid; unsigned long sender; -void over_heat_event_handler(request_t *request) +void +over_heat_event_handler(request_t *request) { response_t response[1]; attr_container_t *payload; @@ -23,28 +24,33 @@ void over_heat_event_handler(request_t *request) response->mid = mid; response->reciever = sender; - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { mid = request->mid; sender = request->sender; api_subscribe_event("alert/overheat", over_heat_event_handler); } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/res1", res1_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/06_timer.c b/test-tools/component-test/suites/01-life-cycle/test-app/06_timer.c similarity index 68% rename from test-tools/component_test/suites/01-life-cycle/test-app/06_timer.c rename to test-tools/component-test/suites/01-life-cycle/test-app/06_timer.c index c20365717..6aa107d5c 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/06_timer.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/06_timer.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" @@ -11,13 +11,15 @@ int num = 0; /* Timer callback */ -void timer1_update(user_timer_t timer) +void +timer1_update(user_timer_t timer) { if (num < 2) num++; } -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { user_timer_t timer; @@ -29,13 +31,13 @@ void res1_handler(request_t *request) make_response_for_request(request, response); - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, NULL, 0); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, NULL, 0); api_response_send(response); } -void res2_handler(request_t *request) +void +res2_handler(request_t *request) { response_t response[1]; attr_container_t *payload; @@ -52,25 +54,27 @@ void res2_handler(request_t *request) make_response_for_request(request, response); - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, - attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } - } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/res1", res1_handler); api_register_resource_handler("/check_timer", res2_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/07_sensor.c b/test-tools/component-test/suites/01-life-cycle/test-app/07_sensor.c similarity index 71% rename from test-tools/component_test/suites/01-life-cycle/test-app/07_sensor.c rename to test-tools/component-test/suites/01-life-cycle/test-app/07_sensor.c index 24af34c91..a6c24a8bc 100644 --- a/test-tools/component_test/suites/01-life-cycle/test-app/07_sensor.c +++ b/test-tools/component-test/suites/01-life-cycle/test-app/07_sensor.c @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ #include "wasm_app.h" #include "wa-inc/request.h" @@ -11,8 +11,8 @@ uint32 mid; unsigned long sender; /* Sensor event callback*/ -void sensor_event_handler(sensor_t sensor, attr_container_t *event, - void *user_data) +void +sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data) { printf("### app get sensor event\n"); @@ -28,15 +28,17 @@ void sensor_event_handler(sensor_t sensor, attr_container_t *event, response->mid = mid; response->reciever = sender; - set_response(response, CONTENT_2_05, - FMT_ATTR_CONTAINER, (const char *)payload, attr_container_get_serialize_length(payload)); + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); api_response_send(response); attr_container_destroy(payload); } -void res1_handler(request_t *request) +void +res1_handler(request_t *request) { mid = request->mid; sender = request->sender; @@ -57,13 +59,16 @@ void res1_handler(request_t *request) printf("### app on_init 4\n"); } -void on_init() +void +on_init() { /* register resource uri */ api_register_resource_handler("/res1", res1_handler); } -void on_destroy() +void +on_destroy() { - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ + /* real destroy work including killing timer and closing sensor is + * accomplished in wasm app library version of on_destroy() */ } diff --git a/test-tools/component-test/suites/01-life-cycle/test-app/08_on_destroy.c b/test-tools/component-test/suites/01-life-cycle/test-app/08_on_destroy.c new file mode 100644 index 000000000..ac05a77da --- /dev/null +++ b/test-tools/component-test/suites/01-life-cycle/test-app/08_on_destroy.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "wasm_app.h" +#include "wa-inc/request.h" +#include "wa-inc/sensor.h" + +uint32 mid; +unsigned long sender; +sensor_t sensor; + +/* Sensor event callback*/ +void +sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data) +{ + printf("### app get sensor event\n"); + + response_t response[1]; + attr_container_t *payload; + + payload = attr_container_create("wasm app response payload"); + if (payload == NULL) + return; + + attr_container_set_string(&payload, "key1", "value1"); + + response->mid = mid; + response->reciever = sender; + set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, + (const char *)payload, + attr_container_get_serialize_length(payload)); + printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); + api_response_send(response); + + attr_container_destroy(payload); +} + +void +res1_handler(request_t *request) +{ + mid = request->mid; + sender = request->sender; + + char *user_data; + attr_container_t *config; + + printf("### app on_init 1\n"); + /* open a sensor */ + user_data = malloc(100); + printf("### app on_init 2\n"); + sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data); + printf("### app on_init 3\n"); +} + +void +on_init() +{ + /* register resource uri */ + api_register_resource_handler("/res1", res1_handler); +} + +void +on_destroy() +{ + if (NULL != sensor) { + sensor_close(sensor); + } +} diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/build.sh b/test-tools/component-test/suites/01-life-cycle/test-app/build.sh similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/test-app/build.sh rename to test-tools/component-test/suites/01-life-cycle/test-app/build.sh diff --git a/test-tools/component_test/suites/01-life-cycle/tools/product/start.sh b/test-tools/component-test/suites/01-life-cycle/tools/product/start.sh similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/tools/product/start.sh rename to test-tools/component-test/suites/01-life-cycle/tools/product/start.sh diff --git a/test-tools/component_test/suites/01-life-cycle/tools/product/stop.sh b/test-tools/component-test/suites/01-life-cycle/tools/product/stop.sh similarity index 100% rename from test-tools/component_test/suites/01-life-cycle/tools/product/stop.sh rename to test-tools/component-test/suites/01-life-cycle/tools/product/stop.sh diff --git a/test-tools/component_test/suites/__init__.py b/test-tools/component-test/suites/__init__.py similarity index 100% rename from test-tools/component_test/suites/__init__.py rename to test-tools/component-test/suites/__init__.py diff --git a/test-tools/component_test/suites/readme.txt b/test-tools/component-test/suites/readme.txt similarity index 100% rename from test-tools/component_test/suites/readme.txt rename to test-tools/component-test/suites/readme.txt diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/01_install.c b/test-tools/component_test/suites/01-life-cycle/test-app/01_install.c deleted file mode 100644 index f8ec39373..000000000 --- a/test-tools/component_test/suites/01-life-cycle/test-app/01_install.c +++ /dev/null @@ -1,16 +0,0 @@ -/* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ - -#include "wasm_app.h" - -void on_init() -{ - printf("Hello, I was installed.\n"); -} - -void on_destroy() -{ - /* real destroy work including killing timer and closing sensor is accomplished in wasm app library version of on_destroy() */ -} diff --git a/test-tools/component_test/suites/01-life-cycle/test-app/08_on_destroy.c b/test-tools/component_test/suites/01-life-cycle/test-app/08_on_destroy.c deleted file mode 100644 index 637e0f7a6..000000000 --- a/test-tools/component_test/suites/01-life-cycle/test-app/08_on_destroy.c +++ /dev/null @@ -1,67 +0,0 @@ -/* -* Copyright (C) 2019 Intel Corporation. All rights reserved. -* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -*/ - -#include "wasm_app.h" -#include "wa-inc/request.h" -#include "wa-inc/sensor.h" - -uint32 mid; -unsigned long sender; -sensor_t sensor; - -/* Sensor event callback*/ -void sensor_event_handler(sensor_t sensor, attr_container_t *event, void *user_data) { - printf("### app get sensor event\n"); - - response_t response[1]; - attr_container_t *payload; - - payload = attr_container_create("wasm app response payload"); - if (payload == NULL) - return; - - attr_container_set_string(&payload, "key1", "value1"); - - response->mid = mid; - response->reciever = sender; - set_response(response, - CONTENT_2_05, - FMT_ATTR_CONTAINER, - (const char *)payload, - attr_container_get_serialize_length(payload)); - printf("reciver: %lu, mid:%d\n", response->reciever, response->mid); - api_response_send(response); - - attr_container_destroy(payload); -} - -void res1_handler(request_t *request) -{ - mid = request->mid; - sender = request->sender; - - char *user_data; - attr_container_t *config; - - printf("### app on_init 1\n"); - /* open a sensor */ - user_data = malloc(100); - printf("### app on_init 2\n"); - sensor = sensor_open("sensor_test", 0, sensor_event_handler, user_data); - printf("### app on_init 3\n"); -} - -void on_init() -{ - /* register resource uri */ - api_register_resource_handler("/res1", res1_handler); -} - -void on_destroy() -{ - if(NULL != sensor){ - sensor_close(sensor); - } -} diff --git a/test-tools/host-tool/external/cJSON/cJSON.c b/test-tools/host-tool/external/cJSON/cJSON.c index b948d6d75..5bec53a7a 100644 --- a/test-tools/host-tool/external/cJSON/cJSON.c +++ b/test-tools/host-tool/external/cJSON/cJSON.c @@ -32,9 +32,9 @@ #pragma GCC visibility push(default) #endif #if defined(_MSC_VER) -#pragma warning (push) +#pragma warning(push) /* disable warning about single line comments in system headers */ -#pragma warning (disable : 4001) +#pragma warning(disable : 4001) #endif #include @@ -49,7 +49,7 @@ #endif #if defined(_MSC_VER) -#pragma warning (pop) +#pragma warning(pop) #endif #ifdef __GNUC__ #pragma GCC visibility pop @@ -69,10 +69,11 @@ static error global_error = { NULL, 0 }; CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) { - return (const char*) (global_error.json + global_error.position); + return (const char *)(global_error.json + global_error.position); } -CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) { +CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) +{ if (!cJSON_IsString(item)) { return NULL; } @@ -80,23 +81,27 @@ CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) { return item->valuestring; } -/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ -#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 10) +/* This is a safeguard to prevent copy-pasters from using incompatible C and + * header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) \ + || (CJSON_VERSION_PATCH != 10) #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif -CJSON_PUBLIC(const char*) cJSON_Version(void) +CJSON_PUBLIC(const char *) cJSON_Version(void) { static char version[15]; - snprintf(version, sizeof(version), "%i.%i.%i", - CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + snprintf(version, sizeof(version), "%i.%i.%i", CJSON_VERSION_MAJOR, + CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); return version; } -/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ -static int case_insensitive_strcmp(const unsigned char *string1, - const unsigned char *string2) +/* Case insensitive string comparison, doesn't consider two NULL pointers equal + * though */ +static int +case_insensitive_strcmp(const unsigned char *string1, + const unsigned char *string2) { if ((string1 == NULL) || (string2 == NULL)) { return 1; @@ -106,8 +111,7 @@ static int case_insensitive_strcmp(const unsigned char *string1, return 0; } - for (; tolower(*string1) == tolower(*string2); - (void) string1++, string2++) { + for (; tolower(*string1) == tolower(*string2); (void)string1++, string2++) { if (*string1 == '\0') { return 0; } @@ -118,21 +122,25 @@ static int case_insensitive_strcmp(const unsigned char *string1, typedef struct internal_hooks { void *(CJSON_CDECL *allocate)(size_t size); - void (CJSON_CDECL *deallocate)(void *pointer); + void(CJSON_CDECL *deallocate)(void *pointer); void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); } internal_hooks; #if defined(_MSC_VER) -/* work around MSVC error C2322: '...' address of dillimport '...' is not static */ -static void * CJSON_CDECL internal_malloc(size_t size) +/* work around MSVC error C2322: '...' address of dillimport '...' + is not static */ +static void *CJSON_CDECL +internal_malloc(size_t size) { return malloc(size); } -static void CJSON_CDECL internal_free(void *pointer) +static void CJSON_CDECL +internal_free(void *pointer) { free(pointer); } -static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +static void *CJSON_CDECL +internal_realloc(void *pointer, size_t size) { return realloc(pointer, size); } @@ -142,11 +150,16 @@ static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) #define internal_realloc realloc #endif -static internal_hooks global_hooks = { internal_malloc, internal_free, -internal_realloc }; +/* clang-format off */ +static internal_hooks global_hooks = { + internal_malloc, + internal_free, + internal_realloc +}; +/* clang-format on */ -static unsigned char* cJSON_strdup(const unsigned char* string, - const internal_hooks * const hooks) +static unsigned char * +cJSON_strdup(const unsigned char *string, const internal_hooks *const hooks) { size_t length = 0; unsigned char *copy = NULL; @@ -155,8 +168,8 @@ static unsigned char* cJSON_strdup(const unsigned char* string, return NULL; } - length = strlen((const char*) string) + sizeof(""); - copy = (unsigned char*) hooks->allocate(length); + length = strlen((const char *)string) + sizeof(""); + copy = (unsigned char *)hooks->allocate(length); if (copy == NULL) { return NULL; } @@ -165,7 +178,7 @@ static unsigned char* cJSON_strdup(const unsigned char* string, return copy; } -CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks) { if (hooks == NULL) { /* Reset hooks */ @@ -188,15 +201,16 @@ CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) /* use realloc only if both free and malloc are used */ global_hooks.reallocate = NULL; if ((global_hooks.allocate == malloc) - && (global_hooks.deallocate == free)) { + && (global_hooks.deallocate == free)) { global_hooks.reallocate = realloc; } } /* Internal constructor. */ -static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +static cJSON * +cJSON_New_Item(const internal_hooks *const hooks) { - cJSON* node = (cJSON*) hooks->allocate(sizeof(cJSON)); + cJSON *node = (cJSON *)hooks->allocate(sizeof(cJSON)); if (node) { memset(node, '\0', sizeof(cJSON)); } @@ -225,11 +239,12 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) } /* get the decimal point character of the current locale */ -static unsigned char get_decimal_point(void) +static unsigned char +get_decimal_point(void) { #ifdef ENABLE_LOCALES struct lconv *lconv = localeconv(); - return (unsigned char) lconv->decimal_point[0]; + return (unsigned char)lconv->decimal_point[0]; #else return '.'; #endif @@ -239,21 +254,27 @@ typedef struct { const unsigned char *content; size_t length; size_t offset; - size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + size_t depth; /* How deeply nested (in arrays/objects) is the input at the + current offset. */ internal_hooks hooks; } parse_buffer; -/* check if the given size is left to read in a given parse buffer (starting with 1) */ -#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the given size is left to read in a given parse buffer (starting + * with 1) */ +#define can_read(buffer, size) \ + ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) /* check if the buffer can be accessed at the given index (starting with 0) */ -#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) -#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +#define can_access_at_index(buffer, index) \ + ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) \ + (!can_access_at_index(buffer, index)) /* get a pointer to the buffer at the position */ #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) -/* Parse the input text to generate a number, and populate the result into item. */ -static cJSON_bool parse_number(cJSON * const item, - parse_buffer * const input_buffer) +/* Parse the input text to generate a number, and populate the result + into item. */ +static cJSON_bool +parse_number(cJSON *const item, parse_buffer *const input_buffer) { double number = 0; unsigned char *after_end = NULL; @@ -265,41 +286,43 @@ static cJSON_bool parse_number(cJSON * const item, return false; } - /* copy the number into a temporary buffer and replace '.' with the decimal point - * of the current locale (for strtod) - * This also takes care of '\0' not necessarily being available for marking the end of the input */ - for (i = 0; - (i < (sizeof(number_c_string) - 1)) - && can_access_at_index(input_buffer, i); i++) { + /* copy the number into a temporary buffer and replace '.' with the decimal + * point of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking + * the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) + && can_access_at_index(input_buffer, i); + i++) { switch (buffer_at_offset(input_buffer)[i]) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '+': - case '-': - case 'e': - case 'E': - number_c_string[i] = buffer_at_offset(input_buffer)[i]; - break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; - case '.': - number_c_string[i] = decimal_point; - break; + case '.': + number_c_string[i] = decimal_point; + break; - default: - goto loop_end; + default: + goto loop_end; } } - loop_end: number_c_string[i] = '\0'; +loop_end: + number_c_string[i] = '\0'; - number = strtod((const char*) number_c_string, (char**) &after_end); + number = strtod((const char *)number_c_string, (char **)&after_end); if (number_c_string == after_end) { return false; /* parse_error */ } @@ -309,10 +332,12 @@ static cJSON_bool parse_number(cJSON * const item, /* use saturation in case of overflow */ if (number >= INT_MAX) { item->valueint = INT_MAX; - } else if (number <= (double) INT_MIN) { + } + else if (number <= (double)INT_MIN) { item->valueint = INT_MIN; - } else { - item->valueint = (int) number; + } + else { + item->valueint = (int)number; } item->type = cJSON_Number; @@ -321,15 +346,18 @@ static cJSON_bool parse_number(cJSON * const item, return true; } -/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or + * double */ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) { if (number >= INT_MAX) { object->valueint = INT_MAX; - } else if (number <= (double) INT_MIN) { + } + else if (number <= (double)INT_MIN) { object->valueint = INT_MIN; - } else { - object->valueint = (int) number; + } + else { + object->valueint = (int)number; } return object->valuedouble = number; @@ -346,7 +374,8 @@ typedef struct { } printbuffer; /* realloc printbuffer if necessary to have at least "needed" bytes more */ -static unsigned char* ensure(printbuffer * const p, size_t needed) +static unsigned char * +ensure(printbuffer *const p, size_t needed) { unsigned char *newbuffer = NULL; size_t newsize = 0; @@ -379,16 +408,18 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) /* overflow of int, use INT_MAX if possible */ if (needed <= INT_MAX) { newsize = INT_MAX; - } else { + } + else { return NULL; } - } else { + } + else { newsize = needed * 2; } if (p->hooks.reallocate != NULL) { /* reallocate with realloc if available */ - newbuffer = (unsigned char*) p->hooks.reallocate(p->buffer, newsize); + newbuffer = (unsigned char *)p->hooks.reallocate(p->buffer, newsize); if (newbuffer == NULL) { p->hooks.deallocate(p->buffer); p->length = 0; @@ -396,9 +427,10 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) return NULL; } - } else { + } + else { /* otherwise reallocate manually */ - newbuffer = (unsigned char*) p->hooks.allocate(newsize); + newbuffer = (unsigned char *)p->hooks.allocate(newsize); if (!newbuffer) { p->hooks.deallocate(p->buffer); p->length = 0; @@ -417,8 +449,10 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) return newbuffer + p->offset; } -/* calculate the new length of the string in a printbuffer and update the offset */ -static void update_offset(printbuffer * const buffer) +/* calculate the new length of the string in a printbuffer and update the offset + */ +static void +update_offset(printbuffer *const buffer) { const unsigned char *buffer_pointer = NULL; if ((buffer == NULL) || (buffer->buffer == NULL)) { @@ -426,18 +460,19 @@ static void update_offset(printbuffer * const buffer) } buffer_pointer = buffer->buffer + buffer->offset; - buffer->offset += strlen((const char*) buffer_pointer); + buffer->offset += strlen((const char *)buffer_pointer); } /* Render the number nicely from the given item into a string. */ -static cJSON_bool print_number(const cJSON * const item, - printbuffer * const output_buffer) +static cJSON_bool +print_number(const cJSON *const item, printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; double d = item->valuedouble; int length = 0; size_t i = 0; - unsigned char number_buffer[26]; /* temporary buffer to print the number into */ + unsigned char + number_buffer[26]; /* temporary buffer to print the number into */ unsigned char decimal_point = get_decimal_point(); double test; @@ -447,33 +482,37 @@ static cJSON_bool print_number(const cJSON * const item, /* This checks for NaN and Infinity */ if ((d * 0) != 0) { - length = snprintf((char*) number_buffer, sizeof(number_buffer), "null"); - } else { - /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ - length = snprintf((char*) number_buffer, sizeof(number_buffer), "%1.15g", d); + length = snprintf((char *)number_buffer, sizeof(number_buffer), "null"); + } + else { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero + * digits */ + length = + snprintf((char *)number_buffer, sizeof(number_buffer), "%1.15g", d); /* Check whether the original double can be recovered */ - if ((sscanf((char*) number_buffer, "%lg", &test) != 1) - || ((double) test != d)) { + if ((sscanf((char *)number_buffer, "%lg", &test) != 1) + || ((double)test != d)) { /* If not, print with 17 decimal places of precision */ - length = snprintf((char*) number_buffer, sizeof(number_buffer), "%1.17g", d); + length = snprintf((char *)number_buffer, sizeof(number_buffer), + "%1.17g", d); } } /* snprintf failed or buffer overrun occured */ - if ((length < 0) || (length > (int) (sizeof(number_buffer) - 1))) { + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) { return false; } /* reserve appropriate space in the output */ - output_pointer = ensure(output_buffer, (size_t) length + sizeof("")); + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); if (output_pointer == NULL) { return false; } /* copy the printed number to the output and replace locale * dependent decimal point with '.' */ - for (i = 0; i < ((size_t) length); i++) { + for (i = 0; i < ((size_t)length); i++) { if (number_buffer[i] == decimal_point) { output_pointer[i] = '.'; continue; @@ -483,13 +522,14 @@ static cJSON_bool print_number(const cJSON * const item, } output_pointer[i] = '\0'; - output_buffer->offset += (size_t) length; + output_buffer->offset += (size_t)length; return true; } /* parse 4 digit hexadecimal number */ -static unsigned parse_hex4(const unsigned char * const input) +static unsigned +parse_hex4(const unsigned char *const input) { unsigned int h = 0; size_t i = 0; @@ -497,12 +537,15 @@ static unsigned parse_hex4(const unsigned char * const input) for (i = 0; i < 4; i++) { /* parse digit */ if ((input[i] >= '0') && (input[i] <= '9')) { - h += (unsigned int) input[i] - '0'; - } else if ((input[i] >= 'A') && (input[i] <= 'F')) { - h += (unsigned int) 10 + input[i] - 'A'; - } else if ((input[i] >= 'a') && (input[i] <= 'f')) { - h += (unsigned int) 10 + input[i] - 'a'; - } else /* invalid */ + h += (unsigned int)input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) { + h += (unsigned int)10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) { + h += (unsigned int)10 + input[i] - 'a'; + } + else /* invalid */ { return 0; } @@ -518,9 +561,10 @@ static unsigned parse_hex4(const unsigned char * const input) /* converts a UTF-16 literal to UTF-8 * A literal can be one or two sequences of the form \uXXXX */ -static unsigned char utf16_literal_to_utf8( - const unsigned char * const input_pointer, - const unsigned char * const input_end, unsigned char **output_pointer) +static unsigned char +utf16_literal_to_utf8(const unsigned char *const input_pointer, + const unsigned char *const input_end, + unsigned char **output_pointer) { long unsigned int codepoint = 0; unsigned int first_code = 0; @@ -568,9 +612,10 @@ static unsigned char utf16_literal_to_utf8( } /* calculate the unicode codepoint from the surrogate pair */ - codepoint = 0x10000 - + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); - } else { + codepoint = + 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else { sequence_length = 6; /* \uXXXX */ codepoint = first_code; } @@ -581,49 +626,55 @@ static unsigned char utf16_literal_to_utf8( if (codepoint < 0x80) { /* normal ascii, encoding 0xxxxxxx */ utf8_length = 1; - } else if (codepoint < 0x800) { + } + else if (codepoint < 0x800) { /* two bytes, encoding 110xxxxx 10xxxxxx */ utf8_length = 2; first_byte_mark = 0xC0; /* 11000000 */ - } else if (codepoint < 0x10000) { + } + else if (codepoint < 0x10000) { /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ utf8_length = 3; first_byte_mark = 0xE0; /* 11100000 */ - } else if (codepoint <= 0x10FFFF) { + } + else if (codepoint <= 0x10FFFF) { /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ utf8_length = 4; first_byte_mark = 0xF0; /* 11110000 */ - } else { + } + else { /* invalid unicode codepoint */ goto fail; } /* encode as utf8 */ - for (utf8_position = (unsigned char) (utf8_length - 1); utf8_position > 0; - utf8_position--) { + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; + utf8_position--) { /* 10xxxxxx */ - (*output_pointer)[utf8_position] = (unsigned char) ((codepoint | 0x80) - & 0xBF); + (*output_pointer)[utf8_position] = + (unsigned char)((codepoint | 0x80) & 0xBF); codepoint >>= 6; } /* encode first byte */ if (utf8_length > 1) { - (*output_pointer)[0] = (unsigned char) ((codepoint | first_byte_mark) - & 0xFF); - } else { - (*output_pointer)[0] = (unsigned char) (codepoint & 0x7F); + (*output_pointer)[0] = + (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); } *output_pointer += utf8_length; return sequence_length; - fail: return 0; +fail: + return 0; } /* Parse the input text into an unescaped cinput, and populate item. */ -static cJSON_bool parse_string(cJSON * const item, - parse_buffer * const input_buffer) +static cJSON_bool +parse_string(cJSON *const item, parse_buffer *const input_buffer) { const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; @@ -639,13 +690,15 @@ static cJSON_bool parse_string(cJSON * const item, /* calculate approximate size of the output (overestimate) */ size_t allocation_length = 0; size_t skipped_bytes = 0; - while (((size_t)(input_end - input_buffer->content) - < input_buffer->length) && (*input_end != '\"')) { + while ( + ((size_t)(input_end - input_buffer->content) < input_buffer->length) + && (*input_end != '\"')) { /* is escape sequence */ if (input_end[0] == '\\') { if ((size_t)(input_end + 1 - input_buffer->content) - >= input_buffer->length) { - /* prevent buffer overflow when last input character is a backslash */ + >= input_buffer->length) { + /* prevent buffer overflow when last input character is a + * backslash */ goto fail; } skipped_bytes++; @@ -653,17 +706,18 @@ static cJSON_bool parse_string(cJSON * const item, } input_end++; } - if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) - || (*input_end != '\"')) { + if (((size_t)(input_end - input_buffer->content) + >= input_buffer->length) + || (*input_end != '\"')) { goto fail; /* string ended unexpectedly */ } /* This is at most how much we need for the output */ allocation_length = (size_t)(input_end - buffer_at_offset(input_buffer)) - - skipped_bytes; - output = (unsigned char*) input_buffer->hooks.allocate( - allocation_length + sizeof("")); + - skipped_bytes; + output = (unsigned char *)input_buffer->hooks.allocate(allocation_length + + sizeof("")); if (output == NULL) { goto fail; /* allocation failure */ @@ -684,39 +738,39 @@ static cJSON_bool parse_string(cJSON * const item, } switch (input_pointer[1]) { - case 'b': - *output_pointer++ = '\b'; - break; - case 'f': - *output_pointer++ = '\f'; - break; - case 'n': - *output_pointer++ = '\n'; - break; - case 'r': - *output_pointer++ = '\r'; - break; - case 't': - *output_pointer++ = '\t'; - break; - case '\"': - case '\\': - case '/': - *output_pointer++ = input_pointer[1]; - break; + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; - /* UTF-16 literal */ - case 'u': - sequence_length = utf16_literal_to_utf8(input_pointer, - input_end, &output_pointer); - if (sequence_length == 0) { - /* failed to convert UTF16-literal to UTF-8 */ + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8( + input_pointer, input_end, &output_pointer); + if (sequence_length == 0) { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: goto fail; - } - break; - - default: - goto fail; } input_pointer += sequence_length; } @@ -726,14 +780,15 @@ static cJSON_bool parse_string(cJSON * const item, *output_pointer = '\0'; item->type = cJSON_String; - item->valuestring = (char*) output; + item->valuestring = (char *)output; input_buffer->offset = (size_t)(input_end - input_buffer->content); input_buffer->offset++; return true; - fail: if (output != NULL) { +fail: + if (output != NULL) { input_buffer->hooks.deallocate(output); } @@ -745,8 +800,9 @@ static cJSON_bool parse_string(cJSON * const item, } /* Render the cstring provided to an escaped version that can be printed. */ -static cJSON_bool print_string_ptr(const unsigned char * const input, - printbuffer * const output_buffer) +static cJSON_bool +print_string_ptr(const unsigned char *const input, + printbuffer *const output_buffer) { const unsigned char *input_pointer = NULL; unsigned char *output = NULL, *output_end; @@ -765,7 +821,7 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, if (output == NULL) { return false; } - strcpy((char*) output, "\"\""); + strcpy((char *)output, "\"\""); return true; } @@ -773,22 +829,22 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, /* set "flag" to 1 if something needs to be escaped */ for (input_pointer = input; *input_pointer; input_pointer++) { switch (*input_pointer) { - case '\"': - case '\\': - case '\b': - case '\f': - case '\n': - case '\r': - case '\t': - /* one character escape sequence */ - escape_characters++; - break; - default: - if (*input_pointer < 32) { - /* UTF-16 escape sequence uXXXX */ - escape_characters += 5; - } - break; + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; } } output_length = (size_t)(input_pointer - input) + escape_characters; @@ -813,42 +869,44 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, output_pointer = output + 1; /* copy the string */ for (input_pointer = input; *input_pointer != '\0'; - (void) input_pointer++, output_pointer++) { + (void)input_pointer++, output_pointer++) { if ((*input_pointer > 31) && (*input_pointer != '\"') - && (*input_pointer != '\\')) { + && (*input_pointer != '\\')) { /* normal character, copy */ *output_pointer = *input_pointer; - } else { + } + else { /* character needs to be escaped */ *output_pointer++ = '\\'; switch (*input_pointer) { - case '\\': - *output_pointer = '\\'; - break; - case '\"': - *output_pointer = '\"'; - break; - case '\b': - *output_pointer = 'b'; - break; - case '\f': - *output_pointer = 'f'; - break; - case '\n': - *output_pointer = 'n'; - break; - case '\r': - *output_pointer = 'r'; - break; - case '\t': - *output_pointer = 't'; - break; - default: - /* escape and print as unicode codepoint */ - snprintf((char*) output_pointer, output_end - output_pointer, - "u%04x", *input_pointer); - output_pointer += 4; - break; + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + snprintf((char *)output_pointer, + output_end - output_pointer, "u%04x", + *input_pointer); + output_pointer += 4; + break; } } } @@ -859,33 +917,36 @@ static cJSON_bool print_string_ptr(const unsigned char * const input, } /* Invoke print_string_ptr (which is useful) on an item. */ -static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +static cJSON_bool +print_string(const cJSON *const item, printbuffer *const p) { - return print_string_ptr((unsigned char*) item->valuestring, p); + return print_string_ptr((unsigned char *)item->valuestring, p); } /* Predeclare these prototypes. */ -static cJSON_bool parse_value(cJSON * const item, - parse_buffer * const input_buffer); -static cJSON_bool print_value(const cJSON * const item, - printbuffer * const output_buffer); -static cJSON_bool parse_array(cJSON * const item, - parse_buffer * const input_buffer); -static cJSON_bool print_array(const cJSON * const item, - printbuffer * const output_buffer); -static cJSON_bool parse_object(cJSON * const item, - parse_buffer * const input_buffer); -static cJSON_bool print_object(const cJSON * const item, - printbuffer * const output_buffer); +static cJSON_bool +parse_value(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool +print_value(const cJSON *const item, printbuffer *const output_buffer); +static cJSON_bool +parse_array(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool +print_array(const cJSON *const item, printbuffer *const output_buffer); +static cJSON_bool +parse_object(cJSON *const item, parse_buffer *const input_buffer); +static cJSON_bool +print_object(const cJSON *const item, printbuffer *const output_buffer); /* Utility to jump whitespace and cr/lf */ -static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +static parse_buffer * +buffer_skip_whitespace(parse_buffer *const buffer) { if ((buffer == NULL) || (buffer->content == NULL)) { return NULL; } - while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) { + while (can_access_at_index(buffer, 0) + && (buffer_at_offset(buffer)[0] <= 32)) { buffer->offset++; } @@ -897,16 +958,17 @@ static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) } /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ -static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +static parse_buffer * +skip_utf8_bom(parse_buffer *const buffer) { if ((buffer == NULL) || (buffer->content == NULL) - || (buffer->offset != 0)) { + || (buffer->offset != 0)) { return NULL; } if (can_access_at_index(buffer, 4) - && (strncmp((const char*) buffer_at_offset(buffer), "\xEF\xBB\xBF", - 3) == 0)) { + && (strncmp((const char *)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) + == 0)) { buffer->offset += 3; } @@ -914,22 +976,23 @@ static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) } /* Parse an object - create a new root, and populate. */ -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithOpts(const char *value, const char **return_parse_end, + cJSON_bool require_null_terminated) { - parse_buffer buffer = {0, 0, 0, 0, {0, 0, 0}}; + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; cJSON *item = NULL; /* reset error position */ global_error.json = NULL; global_error.position = 0; - if (value == NULL) - { + if (value == NULL) { goto fail; } - buffer.content = (const unsigned char*)value; - buffer.length = strlen((const char*)value) + sizeof(""); + buffer.content = (const unsigned char *)value; + buffer.length = strlen((const char *)value) + sizeof(""); buffer.offset = 0; buffer.hooks = global_hooks; @@ -939,52 +1002,46 @@ CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return goto fail; } - if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) - { + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) { /* parse failure. ep is set. */ goto fail; } - /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ - if (require_null_terminated) - { + /* if we require null-terminated JSON without appended garbage, skip and + * then check for a null terminator */ + if (require_null_terminated) { buffer_skip_whitespace(&buffer); - if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') - { + if ((buffer.offset >= buffer.length) + || buffer_at_offset(&buffer)[0] != '\0') { goto fail; } } - if (return_parse_end) - { - *return_parse_end = (const char*)buffer_at_offset(&buffer); + if (return_parse_end) { + *return_parse_end = (const char *)buffer_at_offset(&buffer); } return item; - fail: - if (item != NULL) - { +fail: + if (item != NULL) { cJSON_Delete(item); } - if (value != NULL) - { + if (value != NULL) { error local_error; - local_error.json = (const unsigned char*)value; + local_error.json = (const unsigned char *)value; local_error.position = 0; - if (buffer.offset < buffer.length) - { + if (buffer.offset < buffer.length) { local_error.position = buffer.offset; } - else if (buffer.length > 0) - { + else if (buffer.length > 0) { local_error.position = buffer.length - 1; } - if (return_parse_end != NULL) - { - *return_parse_end = (const char*)local_error.json + local_error.position; + if (return_parse_end != NULL) { + *return_parse_end = + (const char *)local_error.json + local_error.position; } global_error = local_error; @@ -1001,8 +1058,9 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) #define cjson_min(a, b) ((a < b) ? a : b) -static unsigned char *print(const cJSON * const item, cJSON_bool format, - const internal_hooks * const hooks) +static unsigned char * +print(const cJSON *const item, cJSON_bool format, + const internal_hooks *const hooks) { static const size_t default_buffer_size = 256; printbuffer buffer[1]; @@ -1011,7 +1069,7 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, memset(buffer, 0, sizeof(buffer)); /* create buffer */ - buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->buffer = (unsigned char *)hooks->allocate(default_buffer_size); buffer->length = default_buffer_size; buffer->format = format; buffer->hooks = *hooks; @@ -1027,20 +1085,21 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, /* check if reallocate is available */ if (hooks->reallocate != NULL) { - printed = (unsigned char*) hooks->reallocate(buffer->buffer, - buffer->offset + 1); + printed = (unsigned char *)hooks->reallocate(buffer->buffer, + buffer->offset + 1); if (printed == NULL) { goto fail; } buffer->buffer = NULL; - } else /* otherwise copy the JSON over to a new buffer */ + } + else /* otherwise copy the JSON over to a new buffer */ { - printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + printed = (unsigned char *)hooks->allocate(buffer->offset + 1); if (printed == NULL) { goto fail; } memcpy(printed, buffer->buffer, - cjson_min(buffer->length, buffer->offset + 1)); + cjson_min(buffer->length, buffer->offset + 1)); printed[buffer->offset] = '\0'; /* just to be sure */ /* free the buffer */ @@ -1049,7 +1108,8 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, return printed; - fail: if (buffer->buffer != NULL) { +fail: + if (buffer->buffer != NULL) { hooks->deallocate(buffer->buffer); } @@ -1063,26 +1123,25 @@ static unsigned char *print(const cJSON * const item, cJSON_bool format, /* Render a cJSON item/entity/structure to text. */ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) { - return (char*)print(item, true, &global_hooks); + return (char *)print(item, true, &global_hooks); } CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) { - return (char*)print(item, false, &global_hooks); + return (char *)print(item, false, &global_hooks); } -CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +CJSON_PUBLIC(char *) +cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) { - printbuffer p = {0, 0, 0, 0, 0, 0, {0, 0, 0}}; + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; - if (prebuffer < 0) - { + if (prebuffer < 0) { return NULL; } - p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); - if (!p.buffer) - { + p.buffer = (unsigned char *)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) { return NULL; } @@ -1092,17 +1151,17 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON p.format = fmt; p.hooks = global_hooks; - if (!print_value(item, &p)) - { + if (!print_value(item, &p)) { global_hooks.deallocate(p.buffer); return NULL; } - return (char*)p.buffer; + return (char *)p.buffer; } -CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, - const int len, const cJSON_bool fmt) +CJSON_PUBLIC(cJSON_bool) +cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, + const cJSON_bool fmt) { printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; @@ -1110,8 +1169,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, return false; } - p.buffer = (unsigned char*) buf; - p.length = (size_t) len; + p.buffer = (unsigned char *)buf; + p.length = (size_t)len; p.offset = 0; p.noalloc = true; p.format = fmt; @@ -1121,8 +1180,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, } /* Parser core - when encountering text, process appropriately. */ -static cJSON_bool parse_value(cJSON * const item, - parse_buffer * const input_buffer) +static cJSON_bool +parse_value(cJSON *const item, parse_buffer *const input_buffer) { if ((input_buffer == NULL) || (input_buffer->content == NULL)) { return false; /* no input */ @@ -1131,24 +1190,24 @@ static cJSON_bool parse_value(cJSON * const item, /* parse the different types of values */ /* null */ if (can_read(input_buffer, 4) - && (strncmp((const char*) buffer_at_offset(input_buffer), "null", 4) - == 0)) { + && (strncmp((const char *)buffer_at_offset(input_buffer), "null", 4) + == 0)) { item->type = cJSON_NULL; input_buffer->offset += 4; return true; } /* false */ if (can_read(input_buffer, 5) - && (strncmp((const char*) buffer_at_offset(input_buffer), "false", - 5) == 0)) { + && (strncmp((const char *)buffer_at_offset(input_buffer), "false", 5) + == 0)) { item->type = cJSON_False; input_buffer->offset += 5; return true; } /* true */ if (can_read(input_buffer, 4) - && (strncmp((const char*) buffer_at_offset(input_buffer), "true", 4) - == 0)) { + && (strncmp((const char *)buffer_at_offset(input_buffer), "true", 4) + == 0)) { item->type = cJSON_True; item->valueint = 1; input_buffer->offset += 4; @@ -1156,24 +1215,24 @@ static cJSON_bool parse_value(cJSON * const item, } /* string */ if (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == '\"')) { + && (buffer_at_offset(input_buffer)[0] == '\"')) { return parse_string(item, input_buffer); } /* number */ if (can_access_at_index(input_buffer, 0) - && ((buffer_at_offset(input_buffer)[0] == '-') - || ((buffer_at_offset(input_buffer)[0] >= '0') - && (buffer_at_offset(input_buffer)[0] <= '9')))) { + && ((buffer_at_offset(input_buffer)[0] == '-') + || ((buffer_at_offset(input_buffer)[0] >= '0') + && (buffer_at_offset(input_buffer)[0] <= '9')))) { return parse_number(item, input_buffer); } /* array */ if (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == '[')) { + && (buffer_at_offset(input_buffer)[0] == '[')) { return parse_array(item, input_buffer); } /* object */ if (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == '{')) { + && (buffer_at_offset(input_buffer)[0] == '{')) { return parse_object(item, input_buffer); } @@ -1181,8 +1240,8 @@ static cJSON_bool parse_value(cJSON * const item, } /* Render a value to text. */ -static cJSON_bool print_value(const cJSON * const item, - printbuffer * const output_buffer) +static cJSON_bool +print_value(const cJSON *const item, printbuffer *const output_buffer) { unsigned char *output = NULL; @@ -1191,65 +1250,66 @@ static cJSON_bool print_value(const cJSON * const item, } switch ((item->type) & 0xFF) { - case cJSON_NULL: - output = ensure(output_buffer, 5); - if (output == NULL) { - return false; - } - strcpy((char*) output, "null"); - return true; + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) { + return false; + } + strcpy((char *)output, "null"); + return true; - case cJSON_False: - output = ensure(output_buffer, 6); - if (output == NULL) { - return false; - } - strcpy((char*) output, "false"); - return true; + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) { + return false; + } + strcpy((char *)output, "false"); + return true; - case cJSON_True: - output = ensure(output_buffer, 5); - if (output == NULL) { - return false; - } - strcpy((char*) output, "true"); - return true; + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) { + return false; + } + strcpy((char *)output, "true"); + return true; - case cJSON_Number: - return print_number(item, output_buffer); + case cJSON_Number: + return print_number(item, output_buffer); - case cJSON_Raw: { - size_t raw_length = 0; - if (item->valuestring == NULL) { - return false; + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; } - raw_length = strlen(item->valuestring) + sizeof(""); - output = ensure(output_buffer, raw_length); - if (output == NULL) { + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: return false; - } - memcpy(output, item->valuestring, raw_length); - return true; - } - - case cJSON_String: - return print_string(item, output_buffer); - - case cJSON_Array: - return print_array(item, output_buffer); - - case cJSON_Object: - return print_object(item, output_buffer); - - default: - return false; } } /* Build an array from input text. */ -static cJSON_bool parse_array(cJSON * const item, - parse_buffer * const input_buffer) +static cJSON_bool +parse_array(cJSON *const item, parse_buffer *const input_buffer) { cJSON *head = NULL; /* head of the linked list */ cJSON *current_item = NULL; @@ -1267,7 +1327,7 @@ static cJSON_bool parse_array(cJSON * const item, input_buffer->offset++; buffer_skip_whitespace(input_buffer); if (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == ']')) { + && (buffer_at_offset(input_buffer)[0] == ']')) { /* empty array */ goto success; } @@ -1293,7 +1353,8 @@ static cJSON_bool parse_array(cJSON * const item, if (head == NULL) { /* start the linked list */ current_item = head = new_item; - } else { + } + else { /* add to the end and advance */ current_item->next = new_item; new_item->prev = current_item; @@ -1309,15 +1370,16 @@ static cJSON_bool parse_array(cJSON * const item, } buffer_skip_whitespace(input_buffer); } while (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == ',')); + && (buffer_at_offset(input_buffer)[0] == ',')); if (cannot_access_at_index(input_buffer, 0) - || buffer_at_offset(input_buffer)[0] != ']') { + || buffer_at_offset(input_buffer)[0] != ']') { goto fail; /* expected end of array */ } - success: input_buffer->depth--; +success: + input_buffer->depth--; item->type = cJSON_Array; item->child = head; @@ -1326,7 +1388,8 @@ static cJSON_bool parse_array(cJSON * const item, return true; - fail: if (head != NULL) { +fail: + if (head != NULL) { cJSON_Delete(head); } @@ -1334,8 +1397,8 @@ static cJSON_bool parse_array(cJSON * const item, } /* Render an array to text */ -static cJSON_bool print_array(const cJSON * const item, - printbuffer * const output_buffer) +static cJSON_bool +print_array(const cJSON *const item, printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1389,8 +1452,8 @@ static cJSON_bool print_array(const cJSON * const item, } /* Build an object from the text. */ -static cJSON_bool parse_object(cJSON * const item, - parse_buffer * const input_buffer) +static cJSON_bool +parse_object(cJSON *const item, parse_buffer *const input_buffer) { cJSON *head = NULL; /* linked list head */ cJSON *current_item = NULL; @@ -1401,7 +1464,7 @@ static cJSON_bool parse_object(cJSON * const item, input_buffer->depth++; if (cannot_access_at_index(input_buffer, 0) - || (buffer_at_offset(input_buffer)[0] != '{')) { + || (buffer_at_offset(input_buffer)[0] != '{')) { goto fail; /* not an object */ } @@ -1409,7 +1472,7 @@ static cJSON_bool parse_object(cJSON * const item, input_buffer->offset++; buffer_skip_whitespace(input_buffer); if (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == '}')) { + && (buffer_at_offset(input_buffer)[0] == '}')) { goto success; /* empty object */ } @@ -1435,7 +1498,8 @@ static cJSON_bool parse_object(cJSON * const item, if (head == NULL) { /* start the linked list */ current_item = head = new_item; - } else { + } + else { /* add to the end and advance */ current_item->next = new_item; new_item->prev = current_item; @@ -1456,7 +1520,7 @@ static cJSON_bool parse_object(cJSON * const item, current_item->valuestring = NULL; if (cannot_access_at_index(input_buffer, 0) - || (buffer_at_offset(input_buffer)[0] != ':')) { + || (buffer_at_offset(input_buffer)[0] != ':')) { goto fail; /* invalid object */ } @@ -1470,15 +1534,16 @@ static cJSON_bool parse_object(cJSON * const item, } buffer_skip_whitespace(input_buffer); } while (can_access_at_index(input_buffer, 0) - && (buffer_at_offset(input_buffer)[0] == ',')); + && (buffer_at_offset(input_buffer)[0] == ',')); if (cannot_access_at_index(input_buffer, 0) - || (buffer_at_offset(input_buffer)[0] != '}')) { + || (buffer_at_offset(input_buffer)[0] != '}')) { goto fail; /* expected end of object */ } - success: input_buffer->depth--; +success: + input_buffer->depth--; item->type = cJSON_Object; item->child = head; @@ -1486,7 +1551,8 @@ static cJSON_bool parse_object(cJSON * const item, input_buffer->offset++; return true; - fail: if (head != NULL) { +fail: + if (head != NULL) { cJSON_Delete(head); } @@ -1494,8 +1560,8 @@ static cJSON_bool parse_object(cJSON * const item, } /* Render an object to text. */ -static cJSON_bool print_object(const cJSON * const item, - printbuffer * const output_buffer) +static cJSON_bool +print_object(const cJSON *const item, printbuffer *const output_buffer) { unsigned char *output_pointer = NULL; size_t length = 0; @@ -1533,8 +1599,8 @@ static cJSON_bool print_object(const cJSON * const item, } /* print key */ - if (!print_string_ptr((unsigned char*) current_item->string, - output_buffer)) { + if (!print_string_ptr((unsigned char *)current_item->string, + output_buffer)) { return false; } update_offset(output_buffer); @@ -1558,7 +1624,7 @@ static cJSON_bool print_object(const cJSON * const item, /* print comma if not last */ length = ((size_t)(output_buffer->format ? 1 : 0) - + (size_t)(current_item->next ? 1 : 0)); + + (size_t)(current_item->next ? 1 : 0)); output_pointer = ensure(output_buffer, length + 1); if (output_pointer == NULL) { return false; @@ -1576,8 +1642,8 @@ static cJSON_bool print_object(const cJSON * const item, current_item = current_item->next; } - output_pointer = ensure(output_buffer, - output_buffer->format ? (output_buffer->depth + 1) : 2); + output_pointer = ensure( + output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); if (output_pointer == NULL) { return false; } @@ -1613,10 +1679,11 @@ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ - return (int) size; + return (int)size; } -static cJSON* get_array_item(const cJSON *array, size_t index) +static cJSON * +get_array_item(const cJSON *array, size_t index) { cJSON *current_child = NULL; @@ -1635,16 +1702,16 @@ static cJSON* get_array_item(const cJSON *array, size_t index) CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) { - if (index < 0) - { + if (index < 0) { return NULL; } return get_array_item(array, (size_t)index); } -static cJSON *get_object_item(const cJSON * const object, - const char * const name, const cJSON_bool case_sensitive) +static cJSON * +get_object_item(const cJSON *const object, const char *const name, + const cJSON_bool case_sensitive) { cJSON *current_element = NULL; @@ -1655,13 +1722,16 @@ static cJSON *get_object_item(const cJSON * const object, current_element = object->child; if (case_sensitive) { while ((current_element != NULL) && (current_element->string != NULL) - && (strcmp(name, current_element->string) != 0)) { + && (strcmp(name, current_element->string) != 0)) { current_element = current_element->next; } - } else { + } + else { while ((current_element != NULL) - && (case_insensitive_strcmp((const unsigned char*) name, - (const unsigned char*) (current_element->string)) != 0)) { + && (case_insensitive_strcmp( + (const unsigned char *)name, + (const unsigned char *)(current_element->string)) + != 0)) { current_element = current_element->next; } } @@ -1673,32 +1743,36 @@ static cJSON *get_object_item(const cJSON * const object, return current_element; } -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItem(const cJSON *const object, const char *const string) { return get_object_item(object, string, false); } -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItemCaseSensitive(const cJSON *const object, + const char *const string) { return get_object_item(object, string, true); } -CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, - const char *string) +CJSON_PUBLIC(cJSON_bool) +cJSON_HasObjectItem(const cJSON *object, const char *string) { return cJSON_GetObjectItem(object, string) ? 1 : 0; } /* Utility for array list handling. */ -static void suffix_object(cJSON *prev, cJSON *item) +static void +suffix_object(cJSON *prev, cJSON *item) { prev->next = item; item->prev = prev; } /* Utility for handling references. */ -static cJSON *create_reference(const cJSON *item, - const internal_hooks * const hooks) +static cJSON * +create_reference(const cJSON *item, const internal_hooks *const hooks) { cJSON *reference = NULL; if (item == NULL) { @@ -1717,7 +1791,8 @@ static cJSON *create_reference(const cJSON *item, return reference; } -static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +static cJSON_bool +add_item_to_array(cJSON *array, cJSON *item) { cJSON *child = NULL; @@ -1730,7 +1805,8 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) if (child == NULL) { /* list is empty, start new one */ array->child = item; - } else { + } + else { /* append to the end */ while (child->next) { child = child->next; @@ -1747,24 +1823,30 @@ CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item) add_item_to_array(array, item); } -#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#if defined(__clang__) \ + || (defined(__GNUC__) \ + && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic push #endif #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wcast-qual" #endif /* helper function to cast away const */ -static void* cast_away_const(const void* string) +static void * +cast_away_const(const void *string) { - return (void*) string; + return (void *)string; } -#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#if defined(__clang__) \ + || (defined(__GNUC__) \ + && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic pop #endif -static cJSON_bool add_item_to_object(cJSON * const object, - const char * const string, cJSON * const item, - const internal_hooks * const hooks, const cJSON_bool constant_key) +static cJSON_bool +add_item_to_object(cJSON *const object, const char *const string, + cJSON *const item, const internal_hooks *const hooks, + const cJSON_bool constant_key) { char *new_key = NULL; int new_type = cJSON_Invalid; @@ -1774,10 +1856,11 @@ static cJSON_bool add_item_to_object(cJSON * const object, } if (constant_key) { - new_key = (char*) cast_away_const(string); + new_key = (char *)cast_away_const(string); new_type = item->type | cJSON_StringIsConst; - } else { - new_key = (char*) cJSON_strdup((const unsigned char*) string, hooks); + } + else { + new_key = (char *)cJSON_strdup((const unsigned char *)string, hooks); if (new_key == NULL) { return false; } @@ -1795,15 +1878,15 @@ static cJSON_bool add_item_to_object(cJSON * const object, return add_item_to_array(object, item); } -CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, - cJSON *item) +CJSON_PUBLIC(void) +cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { add_item_to_object(object, string, item, &global_hooks, false); } /* Add an item to an object with constant string as key */ -CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, - cJSON *item) +CJSON_PUBLIC(void) +cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { add_item_to_object(object, string, item, &global_hooks, true); } @@ -1817,22 +1900,22 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) add_item_to_array(array, create_reference(item, &global_hooks)); } -CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, - const char *string, cJSON *item) +CJSON_PUBLIC(void) +cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) { if ((object == NULL) || (string == NULL)) { return; } add_item_to_object(object, string, create_reference(item, &global_hooks), - &global_hooks, false); + &global_hooks, false); } -CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +CJSON_PUBLIC(cJSON *) +cJSON_AddNullToObject(cJSON *const object, const char *const name) { cJSON *null = cJSON_CreateNull(); - if (add_item_to_object(object, name, null, &global_hooks, false)) - { + if (add_item_to_object(object, name, null, &global_hooks, false)) { return null; } @@ -1840,11 +1923,11 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * co return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +CJSON_PUBLIC(cJSON *) +cJSON_AddTrueToObject(cJSON *const object, const char *const name) { cJSON *true_item = cJSON_CreateTrue(); - if (add_item_to_object(object, name, true_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, true_item, &global_hooks, false)) { return true_item; } @@ -1852,11 +1935,11 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +CJSON_PUBLIC(cJSON *) +cJSON_AddFalseToObject(cJSON *const object, const char *const name) { cJSON *false_item = cJSON_CreateFalse(); - if (add_item_to_object(object, name, false_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, false_item, &global_hooks, false)) { return false_item; } @@ -1864,11 +1947,12 @@ CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * c return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +CJSON_PUBLIC(cJSON *) +cJSON_AddBoolToObject(cJSON *const object, const char *const name, + const cJSON_bool boolean) { cJSON *bool_item = cJSON_CreateBool(boolean); - if (add_item_to_object(object, name, bool_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) { return bool_item; } @@ -1876,11 +1960,12 @@ CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * co return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +CJSON_PUBLIC(cJSON *) +cJSON_AddNumberToObject(cJSON *const object, const char *const name, + const double number) { cJSON *number_item = cJSON_CreateNumber(number); - if (add_item_to_object(object, name, number_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, number_item, &global_hooks, false)) { return number_item; } @@ -1888,11 +1973,12 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +CJSON_PUBLIC(cJSON *) +cJSON_AddStringToObject(cJSON *const object, const char *const name, + const char *const string) { cJSON *string_item = cJSON_CreateString(string); - if (add_item_to_object(object, name, string_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, string_item, &global_hooks, false)) { return string_item; } @@ -1900,11 +1986,12 @@ CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +CJSON_PUBLIC(cJSON *) +cJSON_AddRawToObject(cJSON *const object, const char *const name, + const char *const raw) { cJSON *raw_item = cJSON_CreateRaw(raw); - if (add_item_to_object(object, name, raw_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) { return raw_item; } @@ -1912,11 +1999,11 @@ CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * con return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +CJSON_PUBLIC(cJSON *) +cJSON_AddObjectToObject(cJSON *const object, const char *const name) { cJSON *object_item = cJSON_CreateObject(); - if (add_item_to_object(object, name, object_item, &global_hooks, false)) - { + if (add_item_to_object(object, name, object_item, &global_hooks, false)) { return object_item; } @@ -1924,11 +2011,11 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * return NULL; } -CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +CJSON_PUBLIC(cJSON *) +cJSON_AddArrayToObject(cJSON *const object, const char *const name) { cJSON *array = cJSON_CreateArray(); - if (add_item_to_object(object, name, array, &global_hooks, false)) - { + if (add_item_to_object(object, name, array, &global_hooks, false)) { return array; } @@ -1936,26 +2023,23 @@ CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * c return NULL; } -CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item) { - if ((parent == NULL) || (item == NULL)) - { + if ((parent == NULL) || (item == NULL)) { return NULL; } - if (item->prev != NULL) - { + if (item->prev != NULL) { /* not the first element */ item->prev->next = item->next; } - if (item->next != NULL) - { + if (item->next != NULL) { /* not the last element */ item->next->prev = item->prev; } - if (item == parent->child) - { + if (item == parent->child) { /* first element */ parent->child = item->next; } @@ -1968,12 +2052,12 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) { - if (which < 0) - { + if (which < 0) { return NULL; } - return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); + return cJSON_DetachItemViaPointer(array, + get_array_item(array, (size_t)which)); } CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) @@ -1981,14 +2065,16 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) cJSON_Delete(cJSON_DetachItemFromArray(array, which)); } -CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemFromObject(cJSON *object, const char *string) { cJSON *to_detach = cJSON_GetObjectItem(object, string); return cJSON_DetachItemViaPointer(object, to_detach); } -CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) { cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); @@ -2000,15 +2086,15 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) cJSON_Delete(cJSON_DetachItemFromObject(object, string)); } -CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, - const char *string) +CJSON_PUBLIC(void) +cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) { cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); } /* Replace array/object items with new ones. */ -CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, - cJSON *newitem) +CJSON_PUBLIC(void) +cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { cJSON *after_inserted = NULL; @@ -2016,7 +2102,7 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, return; } - after_inserted = get_array_item(array, (size_t) which); + after_inserted = get_array_item(array, (size_t)which); if (after_inserted == NULL) { add_item_to_array(array, newitem); return; @@ -2027,13 +2113,15 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, after_inserted->prev = newitem; if (after_inserted == array->child) { array->child = newitem; - } else { + } + else { newitem->prev->next = newitem; } } -CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, - cJSON * const item, cJSON * replacement) +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item, + cJSON *replacement) { if ((parent == NULL) || (replacement == NULL) || (item == NULL)) { return false; @@ -2063,19 +2151,20 @@ CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, return true; } -CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, - cJSON *newitem) +CJSON_PUBLIC(void) +cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { if (which < 0) { return; } - cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t) which), - newitem); + cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), + newitem); } -static cJSON_bool replace_item_in_object(cJSON *object, const char *string, - cJSON *replacement, cJSON_bool case_sensitive) +static cJSON_bool +replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, + cJSON_bool case_sensitive) { if ((replacement == NULL) || (string == NULL)) { return false; @@ -2083,27 +2172,28 @@ static cJSON_bool replace_item_in_object(cJSON *object, const char *string, /* replace the name in the replacement */ if (!(replacement->type & cJSON_StringIsConst) - && (replacement->string != NULL)) { + && (replacement->string != NULL)) { cJSON_free(replacement->string); } - replacement->string = (char*) cJSON_strdup((const unsigned char*) string, - &global_hooks); + replacement->string = + (char *)cJSON_strdup((const unsigned char *)string, &global_hooks); replacement->type &= ~cJSON_StringIsConst; - cJSON_ReplaceItemViaPointer(object, - get_object_item(object, string, case_sensitive), replacement); + cJSON_ReplaceItemViaPointer( + object, get_object_item(object, string, case_sensitive), replacement); return true; } -CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, - cJSON *newitem) +CJSON_PUBLIC(void) +cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { replace_item_in_object(object, string, newitem, false); } -CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, - const char *string, cJSON *newitem) +CJSON_PUBLIC(void) +cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, + cJSON *newitem) { replace_item_in_object(object, string, newitem, true); } @@ -2112,8 +2202,7 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_NULL; } @@ -2123,8 +2212,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_True; } @@ -2134,8 +2222,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_False; } @@ -2145,8 +2232,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = b ? cJSON_True : cJSON_False; } @@ -2156,22 +2242,18 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b) CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_Number; item->valuedouble = num; /* use saturation in case of overflow */ - if (num >= INT_MAX) - { + if (num >= INT_MAX) { item->valueint = INT_MAX; } - else if (num <= (double)INT_MIN) - { + else if (num <= (double)INT_MIN) { item->valueint = INT_MIN; } - else - { + else { item->valueint = (int)num; } } @@ -2182,12 +2264,11 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_String; - item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); - if(!item->valuestring) - { + item->valuestring = + (char *)cJSON_strdup((const unsigned char *)string, &global_hooks); + if (!item->valuestring) { cJSON_Delete(item); return NULL; } @@ -2199,10 +2280,9 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) { cJSON *item = cJSON_New_Item(&global_hooks); - if (item != NULL) - { + if (item != NULL) { item->type = cJSON_String | cJSON_IsReference; - item->valuestring = (char*)cast_away_const(string); + item->valuestring = (char *)cast_away_const(string); } return item; @@ -2213,17 +2293,18 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) cJSON *item = cJSON_New_Item(&global_hooks); if (item != NULL) { item->type = cJSON_Object | cJSON_IsReference; - item->child = (cJSON*)cast_away_const(child); + item->child = (cJSON *)cast_away_const(child); } return item; } -CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) +{ cJSON *item = cJSON_New_Item(&global_hooks); if (item != NULL) { item->type = cJSON_Array | cJSON_IsReference; - item->child = (cJSON*)cast_away_const(child); + item->child = (cJSON *)cast_away_const(child); } return item; @@ -2232,12 +2313,11 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { + if (item) { item->type = cJSON_Raw; - item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); - if(!item->valuestring) - { + item->valuestring = + (char *)cJSON_strdup((const unsigned char *)raw, &global_hooks); + if (!item->valuestring) { cJSON_Delete(item); return NULL; } @@ -2249,9 +2329,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type=cJSON_Array; + if (item) { + item->type = cJSON_Array; } return item; @@ -2260,8 +2339,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { cJSON *item = cJSON_New_Item(&global_hooks); - if (item) - { + if (item) { item->type = cJSON_Object; } @@ -2276,26 +2354,21 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) cJSON *p = NULL; cJSON *a = NULL; - if ((count < 0) || (numbers == NULL)) - { + if ((count < 0) || (numbers == NULL)) { return NULL; } a = cJSON_CreateArray(); - for(i = 0; a && (i < (size_t)count); i++) - { + for (i = 0; a && (i < (size_t)count); i++) { n = cJSON_CreateNumber(numbers[i]); - if (!n) - { + if (!n) { cJSON_Delete(a); return NULL; } - if(!i) - { + if (!i) { a->child = n; } - else - { + else { suffix_object(p, n); } p = n; @@ -2311,27 +2384,22 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) cJSON *p = NULL; cJSON *a = NULL; - if ((count < 0) || (numbers == NULL)) - { + if ((count < 0) || (numbers == NULL)) { return NULL; } a = cJSON_CreateArray(); - for(i = 0; a && (i < (size_t)count); i++) - { + for (i = 0; a && (i < (size_t)count); i++) { n = cJSON_CreateNumber((double)numbers[i]); - if(!n) - { + if (!n) { cJSON_Delete(a); return NULL; } - if(!i) - { + if (!i) { a->child = n; } - else - { + else { suffix_object(p, n); } p = n; @@ -2347,27 +2415,22 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) cJSON *p = NULL; cJSON *a = NULL; - if ((count < 0) || (numbers == NULL)) - { + if ((count < 0) || (numbers == NULL)) { return NULL; } a = cJSON_CreateArray(); - for(i = 0;a && (i < (size_t)count); i++) - { + for (i = 0; a && (i < (size_t)count); i++) { n = cJSON_CreateNumber(numbers[i]); - if(!n) - { + if (!n) { cJSON_Delete(a); return NULL; } - if(!i) - { + if (!i) { a->child = n; } - else - { + else { suffix_object(p, n); } p = n; @@ -2383,28 +2446,23 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count) cJSON *p = NULL; cJSON *a = NULL; - if ((count < 0) || (strings == NULL)) - { + if ((count < 0) || (strings == NULL)) { return NULL; } a = cJSON_CreateArray(); - for (i = 0; a && (i < (size_t)count); i++) - { + for (i = 0; a && (i < (size_t)count); i++) { n = cJSON_CreateString(strings[i]); - if(!n) - { + if (!n) { cJSON_Delete(a); return NULL; } - if(!i) - { + if (!i) { a->child = n; } - else - { - suffix_object(p,n); + else { + suffix_object(p, n); } p = n; } @@ -2421,59 +2479,55 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) cJSON *newchild = NULL; /* Bail on bad ptr */ - if (!item) - { + if (!item) { goto fail; } /* Create new item */ newitem = cJSON_New_Item(&global_hooks); - if (!newitem) - { + if (!newitem) { goto fail; } /* Copy over all vars */ newitem->type = item->type & (~cJSON_IsReference); newitem->valueint = item->valueint; newitem->valuedouble = item->valuedouble; - if (item->valuestring) - { - newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); - if (!newitem->valuestring) - { + if (item->valuestring) { + newitem->valuestring = (char *)cJSON_strdup( + (unsigned char *)item->valuestring, &global_hooks); + if (!newitem->valuestring) { goto fail; } } - if (item->string) - { - newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); - if (!newitem->string) - { + if (item->string) { + newitem->string = (item->type & cJSON_StringIsConst) + ? item->string + : (char *)cJSON_strdup( + (unsigned char *)item->string, &global_hooks); + if (!newitem->string) { goto fail; } } /* If non-recursive, then we're done! */ - if (!recurse) - { + if (!recurse) { return newitem; } /* Walk the ->next chain for the child. */ child = item->child; - while (child != NULL) - { - newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ - if (!newchild) - { + while (child != NULL) { + newchild = cJSON_Duplicate( + child, + true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) { goto fail; } - if (next != NULL) - { - /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + if (next != NULL) { + /* If newitem->child already set, then crosswire ->prev and ->next + * and move on */ next->next = newchild; newchild->prev = next; next = newchild; } - else - { + else { /* Set newitem->child and move to it */ newitem->child = newchild; next = newchild; @@ -2483,9 +2537,8 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) return newitem; - fail: - if (newitem != NULL) - { +fail: + if (newitem != NULL) { cJSON_Delete(newitem); } @@ -2494,7 +2547,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) CJSON_PUBLIC(void) cJSON_Minify(char *json) { - unsigned char *into = (unsigned char*) json; + unsigned char *into = (unsigned char *)json; if (json == NULL) { return; @@ -2503,37 +2556,44 @@ CJSON_PUBLIC(void) cJSON_Minify(char *json) while (*json) { if (*json == ' ') { json++; - } else if (*json == '\t') { + } + else if (*json == '\t') { /* Whitespace characters. */ json++; - } else if (*json == '\r') { + } + else if (*json == '\r') { json++; - } else if (*json == '\n') { + } + else if (*json == '\n') { json++; - } else if ((*json == '/') && (json[1] == '/')) { + } + else if ((*json == '/') && (json[1] == '/')) { /* double-slash comments, to end of line. */ while (*json && (*json != '\n')) { json++; } - } else if ((*json == '/') && (json[1] == '*')) { + } + else if ((*json == '/') && (json[1] == '*')) { /* multiline comments. */ while (*json && !((*json == '*') && (json[1] == '/'))) { json++; } json += 2; - } else if (*json == '\"') { + } + else if (*json == '\"') { /* string literals, which are \" sensitive. */ - *into++ = (unsigned char) *json++; + *into++ = (unsigned char)*json++; while (*json && (*json != '\"')) { if (*json == '\\') { - *into++ = (unsigned char) *json++; + *into++ = (unsigned char)*json++; } - *into++ = (unsigned char) *json++; + *into++ = (unsigned char)*json++; } - *into++ = (unsigned char) *json++; - } else { + *into++ = (unsigned char)*json++; + } + else { /* All other characters. */ - *into++ = (unsigned char) *json++; + *into++ = (unsigned char)*json++; } } @@ -2541,7 +2601,7 @@ CJSON_PUBLIC(void) cJSON_Minify(char *json) *into = '\0'; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item) { if (item == NULL) { return false; @@ -2550,7 +2610,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) return (item->type & 0xFF) == cJSON_Invalid; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *const item) { if (item == NULL) { return false; @@ -2559,7 +2619,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) return (item->type & 0xFF) == cJSON_False; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *const item) { if (item == NULL) { return false; @@ -2568,7 +2628,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) return (item->type & 0xff) == cJSON_True; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *const item) { if (item == NULL) { return false; @@ -2576,7 +2636,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) return (item->type & (cJSON_True | cJSON_False)) != 0; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item) { if (item == NULL) { return false; @@ -2585,7 +2645,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) return (item->type & 0xFF) == cJSON_NULL; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item) { if (item == NULL) { return false; @@ -2594,7 +2654,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) return (item->type & 0xFF) == cJSON_Number; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item) { if (item == NULL) { return false; @@ -2603,7 +2663,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) return (item->type & 0xFF) == cJSON_String; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item) { if (item == NULL) { return false; @@ -2612,7 +2672,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) return (item->type & 0xFF) == cJSON_Array; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item) { if (item == NULL) { return false; @@ -2621,7 +2681,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) return (item->type & 0xFF) == cJSON_Object; } -CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item) { if (item == NULL) { return false; @@ -2630,28 +2690,29 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) return (item->type & 0xFF) == cJSON_Raw; } -CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, - const cJSON * const b, const cJSON_bool case_sensitive) +CJSON_PUBLIC(cJSON_bool) +cJSON_Compare(const cJSON *const a, const cJSON *const b, + const cJSON_bool case_sensitive) { if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) - || cJSON_IsInvalid(a)) { + || cJSON_IsInvalid(a)) { return false; } /* check if type is valid */ switch (a->type & 0xFF) { - case cJSON_False: - case cJSON_True: - case cJSON_NULL: - case cJSON_Number: - case cJSON_String: - case cJSON_Raw: - case cJSON_Array: - case cJSON_Object: - break; + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; - default: - return false; + default: + return false; } /* identical objects are equal */ @@ -2660,85 +2721,90 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, } switch (a->type & 0xFF) { - /* in these cases and equal type is enough */ - case cJSON_False: - case cJSON_True: - case cJSON_NULL: - return true; - - case cJSON_Number: - if (a->valuedouble == b->valuedouble) { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: return true; - } - return false; - case cJSON_String: - case cJSON_Raw: - if ((a->valuestring == NULL) || (b->valuestring == NULL)) { + case cJSON_Number: + if (a->valuedouble == b->valuedouble) { + return true; + } return false; - } - if (strcmp(a->valuestring, b->valuestring) == 0) { + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + return true; } - return false; + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = + get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) { + return false; + } - case cJSON_Array: { - cJSON *a_element = a->child; - cJSON *b_element = b->child; - - for (; (a_element != NULL) && (b_element != NULL);) { - if (!cJSON_Compare(a_element, b_element, case_sensitive)) { - return false; + if (!cJSON_Compare(a_element, b_element, case_sensitive)) { + return false; + } } - a_element = a_element->next; - b_element = b_element->next; + /* doing this twice, once on a and b to prevent true comparison if a + * subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = + get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) { + return false; + } + } + + return true; } - /* one of the arrays is longer than the other */ - if (a_element != b_element) { + default: return false; - } - - return true; - } - - case cJSON_Object: { - cJSON *a_element = NULL; - cJSON *b_element = NULL; - cJSON_ArrayForEach(a_element, a) - { - /* TODO This has O(n^2) runtime, which is horrible! */ - b_element = get_object_item(b, a_element->string, case_sensitive); - if (b_element == NULL) { - return false; - } - - if (!cJSON_Compare(a_element, b_element, case_sensitive)) { - return false; - } - } - - /* doing this twice, once on a and b to prevent true comparison if a subset of b - * TODO: Do this the proper way, this is just a fix for now */ - cJSON_ArrayForEach(b_element, b) - { - a_element = get_object_item(a, b_element->string, case_sensitive); - if (a_element == NULL) { - return false; - } - - if (!cJSON_Compare(b_element, a_element, case_sensitive)) { - return false; - } - } - - return true; - } - - default: - return false; } } diff --git a/test-tools/host-tool/external/cJSON/cJSON.h b/test-tools/host-tool/external/cJSON/cJSON.h index 82e05af6e..fc26d64ca 100644 --- a/test-tools/host-tool/external/cJSON/cJSON.h +++ b/test-tools/host-tool/external/cJSON/cJSON.h @@ -27,51 +27,57 @@ extern "C" { #endif -#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#if !defined(__WINDOWS__) \ + && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) \ + || defined(_WIN32)) #define __WINDOWS__ #endif #ifdef __WINDOWS__ -/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: - - CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols - CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) - CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol - - For *nix builds that support visibility attribute, you can define similar behavior by - - setting default visibility to hidden by adding - -fvisibility=hidden (for gcc) - or - -xldscope=hidden (for sun cc) - to CFLAGS - - then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does - +/** + * When compiling for windows, we specify a specific calling convention to avoid + * issues where we are being called from a project with a different default + * calling convention. For windows you have 3 define options: + * CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever + * dllexport symbols + * CJSON_EXPORT_SYMBOLS - Define this on library build when you want to + * dllexport symbols (default) + * CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + * + * For *nix builds that support visibility attribute, you can define similar + * behavior by setting default visibility to hidden by adding + * -fvisibility=hidden (for gcc) + * or + * -xldscope=hidden (for sun cc) + * to CFLAGS, then using the CJSON_API_VISIBILITY flag to "export" the same + * symbols the way CJSON_EXPORT_SYMBOLS does */ #define CJSON_CDECL __cdecl #define CJSON_STDCALL __stdcall -/* export symbols by default, this is necessary for copy pasting the C and header file */ -#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +/* export symbols by default, this is necessary for copy pasting the C and + header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) \ + && !defined(CJSON_EXPORT_SYMBOLS) #define CJSON_EXPORT_SYMBOLS #endif #if defined(CJSON_HIDE_SYMBOLS) -#define CJSON_PUBLIC(type) type CJSON_STDCALL +#define CJSON_PUBLIC(type) type CJSON_STDCALL #elif defined(CJSON_EXPORT_SYMBOLS) -#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL #elif defined(CJSON_IMPORT_SYMBOLS) -#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL #endif #else /* !__WINDOWS__ */ #define CJSON_CDECL #define CJSON_STDCALL -#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) -#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) \ + && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type #else #define CJSON_PUBLIC(type) type #endif @@ -86,24 +92,26 @@ extern "C" { /* cJSON Types: */ #define cJSON_Invalid (0) -#define cJSON_False (1 << 0) -#define cJSON_True (1 << 1) -#define cJSON_NULL (1 << 2) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) #define cJSON_Number (1 << 3) #define cJSON_String (1 << 4) -#define cJSON_Array (1 << 5) +#define cJSON_Array (1 << 5) #define cJSON_Object (1 << 6) -#define cJSON_Raw (1 << 7) /* raw json */ +#define cJSON_Raw (1 << 7) /* raw json */ #define cJSON_IsReference 256 #define cJSON_StringIsConst 512 /* The cJSON structure: */ typedef struct cJSON { - /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + /* next/prev allow you to walk array/object chains. Alternatively, use + GetArraySize/GetArrayItem/GetObjectItem */ struct cJSON *next; struct cJSON *prev; - /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + /* An array or object item will have a child pointer pointing to a chain of + the items in the array/object. */ struct cJSON *child; /* The type of the item, as above. */ @@ -116,74 +124,101 @@ typedef struct cJSON { /* The item's number, if type==cJSON_Number */ double valuedouble; - /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + /* The item's name string, if this item is the child of, or is in the list + of subitems of an object. */ char *string; } cJSON; typedef struct cJSON_Hooks { - /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + /* malloc/free are CDECL on Windows regardless of the default calling + * convention of the compiler, so ensure the hooks allow passing those + * functions directly. */ void *(CJSON_CDECL *malloc_fn)(size_t sz); - void (CJSON_CDECL *free_fn)(void *ptr); + void(CJSON_CDECL *free_fn)(void *ptr); } cJSON_Hooks; typedef int cJSON_bool; -/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. - * This is to prevent stack overflows. */ +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse + them. This is to prevent stack overflows. */ #ifndef CJSON_NESTING_LIMIT #define CJSON_NESTING_LIMIT 1000 #endif /* returns the version of cJSON as a string */ -CJSON_PUBLIC(const char*) cJSON_Version(void); +CJSON_PUBLIC(const char *) cJSON_Version(void); /* Supply malloc, realloc and free functions to cJSON */ -CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks *hooks); -/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ -/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +/* Memory Management: the caller is always responsible to free the results from + * all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib + * free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is + * cJSON_PrintPreallocated, where the caller has full responsibility of the + * buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. + */ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); -/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ -/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ -CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +/* ParseWithOpts allows you to require (and check) that the JSON is null + * terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then + * return_parse_end will contain a pointer to the error so will match + * cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) +cJSON_ParseWithOpts(const char *value, const char **return_parse_end, + cJSON_bool require_null_terminated); /* Render a cJSON entity to text for transfer/storage. */ CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); /* Render a cJSON entity to text for transfer/storage without any formatting. */ CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); -/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ -CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); -/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ -/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ -CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess + * at the final size. guessing well reduces reallocation. fmt=0 gives + * unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) +cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with + * given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will + * use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) +cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, + const cJSON_bool format); /* Delete a cJSON entity and all subentities. */ CJSON_PUBLIC(void) cJSON_Delete(cJSON *c); /* Returns the number of items in an array (or object). */ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); -/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +/* Retrieve item number "index" from array "array". Returns NULL if + * unsuccessful. */ CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); /* Get item "string" from object. Case insensitive. */ -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); -CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); -/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItem(const cJSON *const object, const char *const string); +CJSON_PUBLIC(cJSON *) +cJSON_GetObjectItemCaseSensitive(const cJSON *const object, + const char *const string); +CJSON_PUBLIC(cJSON_bool) +cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. + * You'll probably need to look a few chars back to make sense of it. Defined + * when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); /* Check if the item is a string and return its valuestring */ CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item); /* These functions check the type of an item */ -CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); -CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item); /* These calls create a cJSON item of the appropriate type. */ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); @@ -198,10 +233,10 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); /* Create a string where valuestring references a string so - * it will not be freed by cJSON_Delete */ + it will not be freed by cJSON_Delete */ CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); /* Create an object/arrray that only references it's elements so - * they will not be freed by cJSON_Delete */ + they will not be freed by cJSON_Delete */ CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); @@ -213,64 +248,110 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count); /* Append item to the specified array/object. */ CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item); -CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); -/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. - * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before - * writing to `item->string` */ -CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); -/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(void) +cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and + * will definitely survive the cJSON object. WARNING: When this function was + * used, make sure to always check that (item->type & cJSON_StringIsConst) is + * zero before writing to `item->string` */ +CJSON_PUBLIC(void) +cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you + * want to add an existing cJSON to a new cJSON, but don't want to corrupt your + * existing cJSON. */ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); -CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); +CJSON_PUBLIC(void) +cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); /* Remove/Detatch items from Arrays/Objects. */ -CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemViaPointer(cJSON *parent, cJSON *const item); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); -CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); -CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); -CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); -CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) +cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) +cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) +cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); /* Update array items. */ -CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ -CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); -CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); -CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); -CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(void) +cJSON_InsertItemInArray( + cJSON *array, int which, + cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) +cJSON_ReplaceItemViaPointer(cJSON *const parent, cJSON *const item, + cJSON *replacement); +CJSON_PUBLIC(void) +cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(void) +cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem); +CJSON_PUBLIC(void) +cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, + cJSON *newitem); /* Duplicate a cJSON item */ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); -/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will - need to be released. With recurse!=0, it will duplicate any children connected to the item. - The item->next and ->prev pointers are always zero on return from Duplicate. */ -/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. - * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ -CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new + memory that will need to be released. With recurse!=0, it will duplicate any + children connected to the item. The item->next and ->prev pointers are always + zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or + * invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or + * case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) +cJSON_Compare(const cJSON *const a, const cJSON *const b, + const cJSON_bool case_sensitive); CJSON_PUBLIC(void) cJSON_Minify(char *json); /* Helper functions for creating and adding items to an object at the same time. - * They return the added item or NULL on failure. */ -CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); -CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); -CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); -CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); -CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); -CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); -CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); -CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); -CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); + They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON *) +cJSON_AddNullToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) +cJSON_AddTrueToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) +cJSON_AddFalseToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) +cJSON_AddBoolToObject(cJSON *const object, const char *const name, + const cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) +cJSON_AddNumberToObject(cJSON *const object, const char *const name, + const double number); +CJSON_PUBLIC(cJSON *) +cJSON_AddStringToObject(cJSON *const object, const char *const name, + const char *const string); +CJSON_PUBLIC(cJSON *) +cJSON_AddRawToObject(cJSON *const object, const char *const name, + const char *const raw); +CJSON_PUBLIC(cJSON *) +cJSON_AddObjectToObject(cJSON *const object, const char *const name); +CJSON_PUBLIC(cJSON *) +cJSON_AddArrayToObject(cJSON *const object, const char *const name); -/* When assigning an integer value, it needs to be propagated to valuedouble too. */ -#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* When assigning an integer value, it needs to be propagated to valuedouble + too. */ +#define cJSON_SetIntValue(object, number) \ + ((object) ? (object)->valueint = (object)->valuedouble = (number) \ + : (number)) /* helper for the cJSON_SetNumberValue macro */ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); -#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) +#define cJSON_SetNumberValue(object, number) \ + ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) \ + : (number)) /* Macro for iterating over an array or object */ -#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) +#define cJSON_ArrayForEach(element, array) \ + for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \ + element = element->next) -/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +/* malloc/free objects using the malloc/free functions that have been set with + cJSON_InitHooks */ CJSON_PUBLIC(void *) cJSON_malloc(size_t size); CJSON_PUBLIC(void) cJSON_free(void *object); diff --git a/test-tools/host-tool/src/host_tool_utils.c b/test-tools/host-tool/src/host_tool_utils.c index 183ed0d57..2b9051bbf 100644 --- a/test-tools/host-tool/src/host_tool_utils.c +++ b/test-tools/host-tool/src/host_tool_utils.c @@ -24,37 +24,40 @@ 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); } -char* attr_container_get_attr_begin(const attr_container_t *attr_cont, - uint32_t *p_total_length, - uint16_t *p_attr_num); +char * +attr_container_get_attr_begin(const attr_container_t *attr_cont, + uint32_t *p_total_length, uint16_t *p_attr_num); -cJSON *attr2json(const attr_container_t *attr_cont) +cJSON * +attr2json(const attr_container_t *attr_cont) { uint32_t total_length; uint16_t attr_num, i, j, type; @@ -87,91 +90,95 @@ cJSON *attr2json(const attr_container_t *attr_cont) type = *p++; switch (type) { - case ATTR_TYPE_SHORT: - bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); - if (NULL == (obj = cJSON_CreateNumber(value.s))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - /* another approach: cJSON_AddNumberToObject(root, key, value.s) */ - p += 2; - break; - case ATTR_TYPE_INT: - bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); - if (NULL == (obj = cJSON_CreateNumber(value.i))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += 4; - break; - case ATTR_TYPE_INT64: - bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); - if (NULL == (obj = cJSON_CreateNumber(value.j))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += 8; - break; - case ATTR_TYPE_BYTE: - bh_memcpy_s(&value.b, 1, p, 1); - if (NULL == (obj = cJSON_CreateNumber(value.b))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p++; - break; - case ATTR_TYPE_UINT16: - bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); - if (NULL == (obj = cJSON_CreateNumber(value.c))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += 2; - break; - case ATTR_TYPE_FLOAT: - bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); - if (NULL == (obj = cJSON_CreateNumber(value.f))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += 4; - break; - case ATTR_TYPE_DOUBLE: - bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double)); - if (NULL == (obj = cJSON_CreateNumber(value.d))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += 8; - break; - case ATTR_TYPE_BOOLEAN: - bh_memcpy_s(&value.z, 1, p, 1); - if (NULL == (obj = cJSON_CreateBool(value.z))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p++; - break; - case ATTR_TYPE_STRING: - if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t)))) - goto fail; - cJSON_AddItemToObject(root, key, obj); - p += sizeof(uint16_t) + get_uint16(p); - break; - case ATTR_TYPE_BYTEARRAY: - if (NULL == (obj = cJSON_CreateArray())) - goto fail; - cJSON_AddItemToObject(root, key, obj); - for (j = 0; j < get_uint32(p); j++) { - cJSON *item = cJSON_CreateNumber(*(p + sizeof(uint32_t) + j)); - if (item == NULL) + case ATTR_TYPE_SHORT: + bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); + if (NULL == (obj = cJSON_CreateNumber(value.s))) goto fail; - cJSON_AddItemToArray(obj, item); - } - p += sizeof(uint32_t) + get_uint32(p); - break; + cJSON_AddItemToObject(root, key, obj); + /* another approach: cJSON_AddNumberToObject(root, key, value.s) + */ + p += 2; + break; + case ATTR_TYPE_INT: + bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); + if (NULL == (obj = cJSON_CreateNumber(value.i))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += 4; + break; + case ATTR_TYPE_INT64: + bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); + if (NULL == (obj = cJSON_CreateNumber(value.j))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += 8; + break; + case ATTR_TYPE_BYTE: + bh_memcpy_s(&value.b, 1, p, 1); + if (NULL == (obj = cJSON_CreateNumber(value.b))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p++; + break; + case ATTR_TYPE_UINT16: + bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); + if (NULL == (obj = cJSON_CreateNumber(value.c))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += 2; + break; + case ATTR_TYPE_FLOAT: + bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); + if (NULL == (obj = cJSON_CreateNumber(value.f))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += 4; + break; + case ATTR_TYPE_DOUBLE: + bh_memcpy_s(&value.d, sizeof(double), p, sizeof(double)); + if (NULL == (obj = cJSON_CreateNumber(value.d))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += 8; + break; + case ATTR_TYPE_BOOLEAN: + bh_memcpy_s(&value.z, 1, p, 1); + if (NULL == (obj = cJSON_CreateBool(value.z))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p++; + break; + case ATTR_TYPE_STRING: + if (NULL == (obj = cJSON_CreateString(p + sizeof(uint16_t)))) + goto fail; + cJSON_AddItemToObject(root, key, obj); + p += sizeof(uint16_t) + get_uint16(p); + break; + case ATTR_TYPE_BYTEARRAY: + if (NULL == (obj = cJSON_CreateArray())) + goto fail; + cJSON_AddItemToObject(root, key, obj); + for (j = 0; j < get_uint32(p); j++) { + cJSON *item = + cJSON_CreateNumber(*(p + sizeof(uint32_t) + j)); + if (item == NULL) + goto fail; + cJSON_AddItemToArray(obj, item); + } + p += sizeof(uint32_t) + get_uint32(p); + break; } } return root; - fail: cJSON_Delete(root); +fail: + cJSON_Delete(root); return NULL; } -attr_container_t *json2attr(const cJSON *json_obj) +attr_container_t * +json2attr(const cJSON *json_obj) { attr_container_t *attr_cont; cJSON *item; @@ -187,20 +194,24 @@ attr_container_t *json2attr(const cJSON *json_obj) if (cJSON_IsNumber(item)) { attr_container_set_double(&attr_cont, item->string, - item->valuedouble); - } else if (cJSON_IsTrue(item)) { + item->valuedouble); + } + else if (cJSON_IsTrue(item)) { attr_container_set_bool(&attr_cont, item->string, true); - } else if (cJSON_IsFalse(item)) { + } + else if (cJSON_IsFalse(item)) { attr_container_set_bool(&attr_cont, item->string, false); - } else if (cJSON_IsString(item)) { + } + else if (cJSON_IsString(item)) { attr_container_set_string(&attr_cont, item->string, - item->valuestring); - } else if (cJSON_IsArray(item)) { + item->valuestring); + } + else if (cJSON_IsArray(item)) { int8_t *array; int i = 0, len = sizeof(int8_t) * cJSON_GetArraySize(item); cJSON *array_item; - if (0 == len || NULL == (array = (int8_t *) malloc(len))) + if (0 == len || NULL == (array = (int8_t *)malloc(len))) goto fail; memset(array, 0, len); @@ -210,24 +221,26 @@ attr_container_t *json2attr(const cJSON *json_obj) if (!cJSON_IsNumber(array_item)) break; /* TODO: if array_item->valuedouble > 127 or < -128 */ - array[i++] = (int8_t) array_item->valuedouble; + array[i++] = (int8_t)array_item->valuedouble; } if (i > 0) attr_container_set_bytearray(&attr_cont, item->string, array, - i); + i); free(array); } } return attr_cont; - fail: attr_container_destroy(attr_cont); +fail: + attr_container_destroy(attr_cont); return NULL; } int g_mid = 0; -int gen_random_id() +int +gen_random_id() { static bool init = false; int r; @@ -283,7 +296,8 @@ read_file_to_buffer(const char *filename, int *ret_size) return buffer; } -int wirte_buffer_to_file(const char *filename, const char *buffer, int size) +int +wirte_buffer_to_file(const char *filename, const char *buffer, int size) { int file, ret; diff --git a/test-tools/host-tool/src/host_tool_utils.h b/test-tools/host-tool/src/host_tool_utils.h index aaf427b9c..9b30b41ab 100644 --- a/test-tools/host-tool/src/host_tool_utils.h +++ b/test-tools/host-tool/src/host_tool_utils.h @@ -22,7 +22,8 @@ extern "C" { * * @warning the return object should be deleted with cJSON_Delete by caller */ -cJSON *attr2json(const attr_container_t *attr); +cJSON * +attr2json(const attr_container_t *attr); /** * @brief Convert cJSON object to attribute container object. @@ -33,14 +34,16 @@ cJSON *attr2json(const attr_container_t *attr); * * @warning the return object should be deleted with attr_container_destroy */ -attr_container_t *json2attr(const cJSON *json); +attr_container_t * +json2attr(const cJSON *json); /** * @brief Generate a random 32 bit integer. * * @return the generated random integer */ -int gen_random_id(); +int +gen_random_id(); /** * @brief Read file content to buffer. @@ -48,11 +51,13 @@ int gen_random_id(); * @param filename the file name to read * @param ret_size pointer of integer to save file size once return success * - * @return the created buffer which contains file content if not NULL, NULL means fail + * @return the created buffer which contains file content if not NULL, NULL + * means fail * * @warning the return buffer should be deleted with free by caller */ -char *read_file_to_buffer(const char *filename, int *ret_size); +char * +read_file_to_buffer(const char *filename, int *ret_size); /** * @brief Write buffer content to file. @@ -63,7 +68,8 @@ char *read_file_to_buffer(const char *filename, int *ret_size); * * @return < 0 means fail, > 0 means the number of bytes actually written */ -int wirte_buffer_to_file(const char *filename, const char *buffer, int size); +int +wirte_buffer_to_file(const char *filename, const char *buffer, int size); #ifdef __cplusplus } /* end of extern "C" */ diff --git a/test-tools/host-tool/src/main.c b/test-tools/host-tool/src/main.c index 305abc7ad..7db2f5614 100644 --- a/test-tools/host-tool/src/main.c +++ b/test-tools/host-tool/src/main.c @@ -17,7 +17,7 @@ #include "coap_ext.h" #include "cJSON.h" #include "app_manager_export.h" /* for Module_WASM_App */ -#include "host_link.h" /* for REQUEST_PACKET */ +#include "host_link.h" /* for REQUEST_PACKET */ #include "transport.h" #define BUF_SIZE 1024 @@ -105,7 +105,8 @@ extern int g_mid; extern unsigned char leading[2]; /* -1 fail, 0 success */ -static int send_request(request_t *request, uint16_t msg_type) +static int +send_request(request_t *request, uint16_t msg_type) { char *req_p; int req_size, req_size_n, ret = -1; @@ -119,12 +120,12 @@ static int send_request(request_t *request, uint16_t msg_type) /* message type */ msg_type = htons(msg_type); - if (!host_tool_send_data(g_conn_fd, (char *) &msg_type, sizeof(msg_type))) + if (!host_tool_send_data(g_conn_fd, (char *)&msg_type, sizeof(msg_type))) goto ret; /* payload length */ req_size_n = htonl(req_size); - if (!host_tool_send_data(g_conn_fd, (char *) &req_size_n, + if (!host_tool_send_data(g_conn_fd, (char *)&req_size_n, sizeof(req_size_n))) goto ret; @@ -144,7 +145,8 @@ ret: /** * return: 0: success, others: fail */ -static int install(inst_info *info) +static int +install(inst_info *info) { request_t request[1] = { 0 }; char *app_file_buf; @@ -155,25 +157,25 @@ static int install(inst_info *info) if (info->module_type != NULL && url_remain_space > 0) snprintf(url + strlen(url), url_remain_space, "&type=%s", - info->module_type); + info->module_type); if (info->heap_size > 0 && url_remain_space > 0) snprintf(url + strlen(url), url_remain_space, "&heap=%d", - info->heap_size); + info->heap_size); if (info->timers > 0 && url_remain_space > 0) snprintf(url + strlen(url), url_remain_space, "&timers=%d", - info->timers); + info->timers); if (info->watchdog_interval > 0 && url_remain_space > 0) snprintf(url + strlen(url), url_remain_space, "&wd=%d", - info->watchdog_interval); + info->watchdog_interval); if ((app_file_buf = read_file_to_buffer(info->file, &app_size)) == NULL) return -1; - init_request(request, url, COAP_PUT, FMT_APP_RAW_BINARY, - app_file_buf, app_size); + init_request(request, url, COAP_PUT, FMT_APP_RAW_BINARY, app_file_buf, + app_size); request->mid = gen_random_id(); if (info->module_type == NULL || strcmp(info->module_type, "wasm") == 0) @@ -186,7 +188,8 @@ static int install(inst_info *info) return ret; } -static int uninstall(uninst_info *info) +static int +uninstall(uninst_info *info) { request_t request[1] = { 0 }; char url[URL_MAX_LEN] = { 0 }; @@ -197,14 +200,14 @@ static int uninstall(uninst_info *info) snprintf(url + strlen(url), url_remain_space, "&type=%s", info->module_type); - init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, - NULL, 0); + init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, NULL, 0); request->mid = gen_random_id(); return send_request(request, REQUEST_PACKET); } -static int query(query_info *info) +static int +query(query_info *info) { request_t request[1] = { 0 }; char url[URL_MAX_LEN] = { 0 }; @@ -220,7 +223,8 @@ static int query(query_info *info) return send_request(request, REQUEST_PACKET); } -static int request(req_info *info) +static int +request(req_info *info) { request_t request[1] = { 0 }; attr_container_t *payload = NULL; @@ -232,7 +236,8 @@ static int request(req_info *info) int payload_file_size; if ((payload_file = read_file_to_buffer(info->json_payload_file, - &payload_file_size)) == NULL) + &payload_file_size)) + == NULL) return -1; if (NULL == (json = cJSON_Parse(payload_file))) { @@ -251,8 +256,8 @@ static int request(req_info *info) free(payload_file); } - init_request(request, (char *)info->url, info->action, - FMT_ATTR_CONTAINER, payload, payload_len); + init_request(request, (char *)info->url, info->action, FMT_ATTR_CONTAINER, + payload, payload_len); request->mid = gen_random_id(); ret = send_request(request, REQUEST_PACKET); @@ -268,7 +273,8 @@ fail: * TODO: currently only support 1 url. * how to handle multiple responses and set process's exit code? */ -static int subscribe(reg_info *info) +static int +subscribe(reg_info *info) { request_t request[1] = { 0 }; int ret = -1; @@ -293,15 +299,15 @@ static int subscribe(reg_info *info) char url[URL_MAX_LEN] = { 0 }; char *prefix = info->urls[0] == '/' ? "/event" : "/event/"; snprintf(url, URL_MAX_LEN, "%s%s", prefix, info->urls); - init_request(request, url, COAP_PUT, FMT_ATTR_CONTAINER, - NULL, 0); + init_request(request, url, COAP_PUT, FMT_ATTR_CONTAINER, NULL, 0); request->mid = gen_random_id(); ret = send_request(request, REQUEST_PACKET); #endif return ret; } -static int unsubscribe(unreg_info *info) +static int +unsubscribe(unreg_info *info) { request_t request[1] = { 0 }; int ret = -1; @@ -325,15 +331,15 @@ static int unsubscribe(unreg_info *info) #else char url[URL_MAX_LEN] = { 0 }; snprintf(url, URL_MAX_LEN, "%s%s", "/event/", info->urls); - init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, - NULL, 0); + init_request(request, url, COAP_DELETE, FMT_ATTR_CONTAINER, NULL, 0); request->mid = gen_random_id(); ret = send_request(request, REQUEST_PACKET); #endif return ret; } -static int init() +static int +init() { if (g_connection_mode == CONNECTION_MODE_TCP) { int fd; @@ -353,12 +359,14 @@ static int init() return -1; } -static void deinit() +static void +deinit() { close(g_conn_fd); } -static int parse_action(const char *str) +static int +parse_action(const char *str) { if (strcasecmp(str, "PUT") == 0) return COAP_PUT; @@ -371,70 +379,55 @@ static int parse_action(const char *str) return -1; } +/* clang-format off */ static void showUsage() { - printf("\n"); - printf("Usage:\n\thost_tool -i|-u|-q|-r|-s|-d ...\n\n"); + printf("Usages:\n"); + printf(" host_tool -i|-u|-q|-r|-s|-d ...\n"); + printf(" host_tool -i -f \n" + " [--type=]\n" + " [--heap=]\n" + " [--timers=]\n" + " [--watchdog=]\n" + " [ ...] \n"); + printf(" host_tool -u [ ...]\n"); + printf(" host_tool -q [] [ ...]\n"); + printf(" host_tool -r -A [-p ] [ ...]\n"); + printf(" host_tool -s [ ...]\n"); + printf(" host_tool -d [ ...]\n"); - printf("\thost_tool -i -f \n" - "\t\t [--type=]\n" - "\t\t [--heap=]\n" - "\t\t [--timers=]\n" - "\t\t [--watchdog=]\n" - "\t\t [ ...] \n"); - printf("\thost_tool -u [ ...]\n"); - printf("\thost_tool -q[][ ...]\n"); - printf( - "\thost_tool -r -A [-p ] [ ...]\n"); - printf("\thost_tool -s [ ...]\n"); - printf("\thost_tool -d [ ...]\n\n"); + printf("\nGeneral Options:\n"); + printf(" -i, --install Install an application\n"); + printf(" -u, --uninstall Uninstall an application\n"); + printf(" -q, --query Query all applications\n"); + printf(" -r, --request Send a request\n"); + printf(" -s, --register Register event(s)\n"); + printf(" -d, --deregister De-register event(s)\n"); + printf(" -f, --file Specify app binary file path\n"); + printf(" -A, --action Specify action of the request\n"); + printf(" -p, --payload Specify payload of the request\n"); - printf( - "\t-i, --install Install an application\n"); - printf( - "\t-u, --uninstall Uninstall an application\n"); - printf( - "\t-q, --query Query all applications\n"); - printf("\t-r, --request Send a request\n"); - printf("\t-s, --register Register event(s)\n"); - printf("\t-d, --deregister De-register event(s)\n"); - printf( - "\t-f, --file Specify app binary file path\n"); - printf( - "\t-A, --action Specify action of the request\n"); - printf( - "\t-p, --payload Specify payload of the request\n"); - printf("\n"); - - printf("\n\tControl Options:\n"); - printf(" \t-S
|--address=
Set server address, default to 127.0.0.1\n"); - printf(" \t-P |--port= Set server port, default to 8888\n"); - printf(" \t-D |--uart= Set uart device, default to /dev/ttyS2\n"); - printf(" \t-B |--baudrate= Set uart device baudrate, default to 115200\n"); - - printf( - "\t-t |--timeout= Operation timeout in ms, default to 5000\n"); - printf( - "\t-a |--alive= Alive time in ms after last operation done, default to 0\n"); - printf( - "\t-o |--output= Redirect the output to output a file\n"); - printf( - "\t-U |--udp= Redirect the output to an UDP port in local machine\n"); + printf("\nControl Options:\n"); + printf(" -S
|--address=
Set server address, default to 127.0.0.1\n"); + printf(" -P |--port= Set server port, default to 8888\n"); + printf(" -D |--uart= Set uart device, default to /dev/ttyS2\n"); + printf(" -B |--baudrate= Set uart device baudrate, default to 115200\n"); + printf(" -t |--timeout= Operation timeout in ms, default to 5000\n"); + printf(" -a |--alive= Alive time in ms after last operation done, default to 0\n"); + printf(" -o |--output= Redirect the output to output a file\n"); + printf(" -U |--udp= Redirect the output to an UDP port in local machine\n"); printf("\nNotes:\n"); - printf("\t=name of the application\n"); - printf("\t=path of the application binary file in wasm format\n"); - printf( - "\t=resource descriptor, such as /app//res1 or /res1\n"); - printf( - "\t=event url list separated by ',', such as /event1,/event2,/event3\n"); - printf( - "\t=action of the request, can be PUT, GET, DELETE or POST (case insensitive)\n"); - printf("\t=path of the payload file in json format\n"); - printf("\t=Type of app. Can be 'wasm'(default) or 'jeff'\n"); - printf("\t=Heap size of app.\n"); - printf("\t=Max timers number app can use.\n"); - printf("\t=Watchdog interval in ms.\n"); + printf(" =name of the application\n"); + printf(" =path of the application binary file in wasm format\n"); + printf(" =resource descriptor, such as /app//res1 or /res1\n"); + printf(" =event url list separated by ',', such as /event1,/event2,/event3\n"); + printf(" =action of the request, can be PUT, GET, DELETE or POST (case insensitive)\n"); + printf(" =path of the payload file in json format\n"); + printf(" =Type of app. Can be 'wasm'(default) or 'jeff'\n"); + printf(" =Heap size of app.\n"); + printf(" =Max timers number app can use.\n"); + printf(" =Watchdog interval in ms.\n"); } #define CHECK_DUPLICATE_OPERATION do { \ diff --git a/test-tools/host-tool/src/transport.c b/test-tools/host-tool/src/transport.c index a5d16d2bf..edadde25f 100644 --- a/test-tools/host-tool/src/transport.c +++ b/test-tools/host-tool/src/transport.c @@ -22,7 +22,8 @@ unsigned char leading[2] = { 0x12, 0x34 }; -bool tcp_init(const char *address, uint16_t port, int *fd) +bool +tcp_init(const char *address, uint16_t port, int *fd) { int sock; struct sockaddr_in servaddr; @@ -35,7 +36,7 @@ bool tcp_init(const char *address, uint16_t port, int *fd) servaddr.sin_addr.s_addr = inet_addr(address); servaddr.sin_port = htons(port); - if (connect(sock, (SA*) &servaddr, sizeof(servaddr)) != 0) { + if (connect(sock, (SA *)&servaddr, sizeof(servaddr)) != 0) { close(sock); return false; } @@ -44,7 +45,8 @@ bool tcp_init(const char *address, uint16_t port, int *fd) return true; } -int parse_baudrate(int baud) +int +parse_baudrate(int baud) { switch (baud) { case 9600: @@ -88,7 +90,8 @@ int parse_baudrate(int baud) } } -bool uart_init(const char *device, int baudrate, int *fd) +bool +uart_init(const char *device, int baudrate, int *fd) { int uart_fd; struct termios uart_term; @@ -118,12 +121,13 @@ bool uart_init(const char *device, int baudrate, int *fd) return true; } -bool udp_send(const char *address, int port, const char *buf, int len) +bool +udp_send(const char *address, int port, const char *buf, int len) { int sockfd; struct sockaddr_in servaddr; - if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return false; memset(&servaddr, 0, sizeof(servaddr)); @@ -132,14 +136,15 @@ bool udp_send(const char *address, int port, const char *buf, int len) servaddr.sin_port = htons(port); servaddr.sin_addr.s_addr = INADDR_ANY; - sendto(sockfd, buf, len, MSG_CONFIRM, - (const struct sockaddr *)&servaddr, sizeof(servaddr)); + sendto(sockfd, buf, len, MSG_CONFIRM, (const struct sockaddr *)&servaddr, + sizeof(servaddr)); close(sockfd); return true; } -bool host_tool_send_data(int fd, const char *buf, unsigned int len) +bool +host_tool_send_data(int fd, const char *buf, unsigned int len) { int cnt = 0; ssize_t ret; @@ -170,7 +175,11 @@ resend: return (ret == len); } -#define SET_RECV_PHASE(ctx, new_phase) {ctx->phase = new_phase; ctx->size_in_phase = 0;} +#define SET_RECV_PHASE(ctx, new_phase) \ + do { \ + ctx->phase = new_phase; \ + ctx->size_in_phase = 0; \ + } while (0) /* * input: 1 byte from remote @@ -180,7 +189,8 @@ resend: * 0 completed packet * 2 in receiving payload */ -int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx) +int +on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx) { if (ctx->phase == Phase_Non_Start) { if (ctx->message.payload) { @@ -206,7 +216,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx) } } else if (ctx->phase == Phase_Type) { - unsigned char *p = (unsigned char *) &ctx->message.message_type; + unsigned char *p = (unsigned char *)&ctx->message.message_type; p[ctx->size_in_phase++] = ch; if (ctx->size_in_phase == sizeof(ctx->message.message_type)) { @@ -215,7 +225,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx) } } else if (ctx->phase == Phase_Size) { - unsigned char * p = (unsigned char *) &ctx->message.payload_size; + unsigned char *p = (unsigned char *)&ctx->message.payload_size; p[ctx->size_in_phase++] = ch; if (ctx->size_in_phase == sizeof(ctx->message.payload_size)) { @@ -233,7 +243,7 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx) return 0; } - ctx->message.payload = (char *) malloc(ctx->message.payload_size); + ctx->message.payload = (char *)malloc(ctx->message.payload_size); SET_RECV_PHASE(ctx, Phase_Payload); } } diff --git a/test-tools/host-tool/src/transport.h b/test-tools/host-tool/src/transport.h index ea180cf13..449f438f8 100644 --- a/test-tools/host-tool/src/transport.h +++ b/test-tools/host-tool/src/transport.h @@ -19,7 +19,11 @@ typedef struct { /* The receive phase of IMRT link message */ typedef enum { - Phase_Non_Start, Phase_Leading, Phase_Type, Phase_Size, Phase_Payload + Phase_Non_Start, + Phase_Leading, + Phase_Type, + Phase_Size, + Phase_Payload } recv_phase_t; /* The receive context of IMRT link message */ @@ -38,7 +42,8 @@ typedef struct { * * @return true if success, false if fail */ -bool host_tool_send_data(int fd, const char *buf, unsigned int len); +bool +host_tool_send_data(int fd, const char *buf, unsigned int len); /** * @brief Handle one byte of IMRT link message @@ -51,7 +56,8 @@ bool host_tool_send_data(int fd, const char *buf, unsigned int len); * 0 completed packet * 2 in receiving payload */ -int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx); +int +on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx); /** * @brief Initialize TCP connection with remote server. @@ -62,7 +68,8 @@ int on_imrt_link_byte_arrive(unsigned char ch, imrt_link_recv_context_t *ctx); * * @return true if success, false if fail */ -bool tcp_init(const char *address, uint16_t port, int *fd); +bool +tcp_init(const char *address, uint16_t port, int *fd); /** * @brief Initialize UART connection with remote. @@ -73,7 +80,8 @@ bool tcp_init(const char *address, uint16_t port, int *fd); * * @return true if success, false if fail */ -bool uart_init(const char *device, int baudrate, int *fd); +bool +uart_init(const char *device, int baudrate, int *fd); /** * @brief Parse UART baudrate from an integer @@ -90,7 +98,8 @@ bool uart_init(const char *device, int baudrate, int *fd); * ... * @endcode */ -int parse_baudrate(int baud); +int +parse_baudrate(int baud); /** * @brief Send data over UDP. @@ -102,7 +111,8 @@ int parse_baudrate(int baud); * * @return true if success, false if fail */ -bool udp_send(const char *address, int port, const char *buf, int len); +bool +udp_send(const char *address, int port, const char *buf, int len); #ifdef __cplusplus } /* end of extern "C" */ diff --git a/wamr-compiler/main.c b/wamr-compiler/main.c index c57eff2d6..8fae66d77 100644 --- a/wamr-compiler/main.c +++ b/wamr-compiler/main.c @@ -9,315 +9,314 @@ #include "wasm_export.h" #include "aot_export.h" - #if WASM_ENABLE_REF_TYPES != 0 extern void wasm_set_ref_types_flag(bool enable); #endif +/* clang-format off */ static int print_help() { - printf("Usage: wamrc [options] -o output_file wasm_file\n"); - printf(" --target= Set the target arch, which has the general format: \n"); - printf(" = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n"); - printf(" riscv64, riscv32.\n"); - printf(" Default is host arch, e.g. x86_64\n"); - printf(" = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n"); - printf(" Use --target=help to list supported targets\n"); - printf(" --target-abi= Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n"); - printf(" Default is gnu if target isn't riscv64 or riscv32\n"); - printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n"); - printf(" Use --target-abi=help to list all the ABI supported\n"); - printf(" --cpu= Set the target CPU (default: host CPU, e.g. skylake)\n"); - printf(" Use --cpu=help to list all the CPU supported\n"); - printf(" --cpu-features= Enable or disable the CPU features\n"); - printf(" Use +feature to enable a feature, or -feature to disable it\n"); - printf(" For example, --cpu-features=+feature1,-feature2\n"); - printf(" Use --cpu-features=+help to list all the features supported\n"); - printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n"); - printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n"); - printf(" -sgx Generate code for SGX platform (Intel Software Guard Extention)\n"); - printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n"); - printf(" by default it is disabled in all 64-bit platforms except SGX and\n"); - printf(" in these platforms runtime does bounds checks with hardware trap,\n"); - printf(" and by default it is enabled in all 32-bit platforms\n"); - printf(" --format= Specifies the format of the output file\n"); - printf(" The format supported:\n"); - printf(" aot (default) AoT file\n"); - printf(" object Native object file\n"); - printf(" llvmir-unopt Unoptimized LLVM IR\n"); - printf(" llvmir-opt Optimized LLVM IR\n"); - printf(" --enable-bulk-memory Enable the post-MVP bulk memory feature\n"); - printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n"); - printf(" thread-mgr will be enabled automatically\n"); - printf(" --enable-tail-call Enable the post-MVP tail call feature\n"); - printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n"); - printf(" currently 128-bit SIMD is only supported for x86-64 target,\n"); - printf(" and by default it is enabled in x86-64 target and disabled\n"); - printf(" in other targets\n"); - printf(" --enable-ref-types Enable the post-MVP reference types feature\n"); - printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n"); - printf(" --enable-dump-call-stack Enable stack trace feature\n"); - printf(" --enable-perf-profiling Enable function performance profiling\n"); - printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n"); - printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n"); - printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n"); - printf("Examples: wamrc -o test.aot test.wasm\n"); - printf(" wamrc --target=i386 -o test.aot test.wasm\n"); - printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n"); - return 1; + printf("Usage: wamrc [options] -o output_file wasm_file\n"); + printf(" --target= Set the target arch, which has the general format: \n"); + printf(" = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n"); + printf(" riscv64, riscv32.\n"); + printf(" Default is host arch, e.g. x86_64\n"); + printf(" = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n"); + printf(" Use --target=help to list supported targets\n"); + printf(" --target-abi= Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n"); + printf(" Default is gnu if target isn't riscv64 or riscv32\n"); + printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n"); + printf(" Use --target-abi=help to list all the ABI supported\n"); + printf(" --cpu= Set the target CPU (default: host CPU, e.g. skylake)\n"); + printf(" Use --cpu=help to list all the CPU supported\n"); + printf(" --cpu-features= Enable or disable the CPU features\n"); + printf(" Use +feature to enable a feature, or -feature to disable it\n"); + printf(" For example, --cpu-features=+feature1,-feature2\n"); + printf(" Use --cpu-features=+help to list all the features supported\n"); + printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n"); + printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n"); + printf(" -sgx Generate code for SGX platform (Intel Software Guard Extention)\n"); + printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n"); + printf(" by default it is disabled in all 64-bit platforms except SGX and\n"); + printf(" in these platforms runtime does bounds checks with hardware trap,\n"); + printf(" and by default it is enabled in all 32-bit platforms\n"); + printf(" --format= Specifies the format of the output file\n"); + printf(" The format supported:\n"); + printf(" aot (default) AoT file\n"); + printf(" object Native object file\n"); + printf(" llvmir-unopt Unoptimized LLVM IR\n"); + printf(" llvmir-opt Optimized LLVM IR\n"); + printf(" --enable-bulk-memory Enable the post-MVP bulk memory feature\n"); + printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n"); + printf(" thread-mgr will be enabled automatically\n"); + printf(" --enable-tail-call Enable the post-MVP tail call feature\n"); + printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n"); + printf(" currently 128-bit SIMD is only supported for x86-64 target,\n"); + printf(" and by default it is enabled in x86-64 target and disabled\n"); + printf(" in other targets\n"); + printf(" --enable-ref-types Enable the post-MVP reference types feature\n"); + printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n"); + printf(" --enable-dump-call-stack Enable stack trace feature\n"); + printf(" --enable-perf-profiling Enable function performance profiling\n"); + printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n"); + printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n"); + printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n"); + printf("Examples: wamrc -o test.aot test.wasm\n"); + printf(" wamrc --target=i386 -o test.aot test.wasm\n"); + printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n"); + return 1; } +/* clang-format on */ int main(int argc, char *argv[]) { - char *wasm_file_name = NULL, *out_file_name = NULL; - uint8 *wasm_file = NULL; - uint32 wasm_file_size; - wasm_module_t wasm_module = NULL; - aot_comp_data_t comp_data = NULL; - aot_comp_context_t comp_ctx = NULL; - RuntimeInitArgs init_args; - AOTCompOption option = { 0 }; - char error_buf[128]; - int log_verbose_level = 2; - bool sgx_mode = false; + char *wasm_file_name = NULL, *out_file_name = NULL; + uint8 *wasm_file = NULL; + uint32 wasm_file_size; + wasm_module_t wasm_module = NULL; + aot_comp_data_t comp_data = NULL; + aot_comp_context_t comp_ctx = NULL; + RuntimeInitArgs init_args; + AOTCompOption option = { 0 }; + char error_buf[128]; + int log_verbose_level = 2; + bool sgx_mode = false; - option.opt_level = 3; - option.size_level = 3; - option.output_format = AOT_FORMAT_FILE; - /* default value, enable or disable depends on the platform */ - option.bounds_checks = 2; - option.enable_simd = true; - option.enable_aux_stack_check = true; + option.opt_level = 3; + option.size_level = 3; + option.output_format = AOT_FORMAT_FILE; + /* default value, enable or disable depends on the platform */ + option.bounds_checks = 2; + option.enable_simd = true; + option.enable_aux_stack_check = true; - /* Process options. */ - for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { - if (!strcmp(argv[0], "-o")) { - argc--, argv++; - if (argc < 2) - return print_help(); - out_file_name = argv[0]; - } - else if (!strncmp(argv[0], "--target=", 9)) { - if (argv[0][9] == '\0') - return print_help(); - option.target_arch = argv[0] + 9; - } - else if (!strncmp(argv[0], "--target-abi=", 13)) { - if (argv[0][13] == '\0') - return print_help(); - option.target_abi = argv[0] + 13; - } - else if (!strncmp(argv[0], "--cpu=", 6)) { - if (argv[0][6] == '\0') - return print_help(); - option.target_cpu = argv[0] + 6; - } - else if (!strncmp(argv[0], "--cpu-features=", 15)) { - if (argv[0][15] == '\0') - return print_help(); - option.cpu_features = argv[0] + 15; - } - else if (!strncmp(argv[0], "--opt-level=", 12)) { - if (argv[0][12] == '\0') - return print_help(); - option.opt_level = (uint32)atoi(argv[0] + 12); - if (option.opt_level > 3) - option.opt_level = 3; - } - else if (!strncmp(argv[0], "--size-level=", 13)) { - if (argv[0][13] == '\0') - return print_help(); - option.size_level = (uint32)atoi(argv[0] + 13); - if (option.size_level > 3) - option.size_level = 3; - } - else if (!strcmp(argv[0], "-sgx")) { - sgx_mode = true; - } - else if (!strncmp(argv[0], "--bounds-checks=", 16)) { - option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0; - } - else if (!strncmp(argv[0], "--format=", 9)) { - if (argv[0][9] == '\0') - return print_help(); - if (!strcmp(argv[0] + 9, "aot")) - option.output_format = AOT_FORMAT_FILE; - else if (!strcmp(argv[0] + 9, "object")) - option.output_format = AOT_OBJECT_FILE; - else if (!strcmp(argv[0] + 9, "llvmir-unopt")) - option.output_format = AOT_LLVMIR_UNOPT_FILE; - else if (!strcmp(argv[0] + 9, "llvmir-opt")) - option.output_format = AOT_LLVMIR_OPT_FILE; - else { - printf("Invalid format %s.\n", argv[0] + 9); - return print_help(); + /* Process options */ + for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) { + if (!strcmp(argv[0], "-o")) { + argc--, argv++; + if (argc < 2) + return print_help(); + out_file_name = argv[0]; } - } - else if (!strncmp(argv[0], "-v=", 3)) { - log_verbose_level = atoi(argv[0] + 3); - if (log_verbose_level < 0 || log_verbose_level > 5) + else if (!strncmp(argv[0], "--target=", 9)) { + if (argv[0][9] == '\0') + return print_help(); + option.target_arch = argv[0] + 9; + } + else if (!strncmp(argv[0], "--target-abi=", 13)) { + if (argv[0][13] == '\0') + return print_help(); + option.target_abi = argv[0] + 13; + } + else if (!strncmp(argv[0], "--cpu=", 6)) { + if (argv[0][6] == '\0') + return print_help(); + option.target_cpu = argv[0] + 6; + } + else if (!strncmp(argv[0], "--cpu-features=", 15)) { + if (argv[0][15] == '\0') + return print_help(); + option.cpu_features = argv[0] + 15; + } + else if (!strncmp(argv[0], "--opt-level=", 12)) { + if (argv[0][12] == '\0') + return print_help(); + option.opt_level = (uint32)atoi(argv[0] + 12); + if (option.opt_level > 3) + option.opt_level = 3; + } + else if (!strncmp(argv[0], "--size-level=", 13)) { + if (argv[0][13] == '\0') + return print_help(); + option.size_level = (uint32)atoi(argv[0] + 13); + if (option.size_level > 3) + option.size_level = 3; + } + else if (!strcmp(argv[0], "-sgx")) { + sgx_mode = true; + } + else if (!strncmp(argv[0], "--bounds-checks=", 16)) { + option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0; + } + else if (!strncmp(argv[0], "--format=", 9)) { + if (argv[0][9] == '\0') + return print_help(); + if (!strcmp(argv[0] + 9, "aot")) + option.output_format = AOT_FORMAT_FILE; + else if (!strcmp(argv[0] + 9, "object")) + option.output_format = AOT_OBJECT_FILE; + else if (!strcmp(argv[0] + 9, "llvmir-unopt")) + option.output_format = AOT_LLVMIR_UNOPT_FILE; + else if (!strcmp(argv[0] + 9, "llvmir-opt")) + option.output_format = AOT_LLVMIR_OPT_FILE; + else { + printf("Invalid format %s.\n", argv[0] + 9); + return print_help(); + } + } + else if (!strncmp(argv[0], "-v=", 3)) { + log_verbose_level = atoi(argv[0] + 3); + if (log_verbose_level < 0 || log_verbose_level > 5) + return print_help(); + } + else if (!strcmp(argv[0], "--enable-bulk-memory")) { + option.enable_bulk_memory = true; + } + else if (!strcmp(argv[0], "--enable-multi-thread")) { + option.enable_bulk_memory = true; + option.enable_thread_mgr = true; + } + else if (!strcmp(argv[0], "--enable-tail-call")) { + option.enable_tail_call = true; + } + else if (!strcmp(argv[0], "--enable-simd")) { + /* obsolete option, kept for compatibility */ + option.enable_simd = true; + } + else if (!strcmp(argv[0], "--disable-simd")) { + option.enable_simd = false; + } + else if (!strcmp(argv[0], "--enable-ref-types")) { + option.enable_ref_types = true; + } + else if (!strcmp(argv[0], "--disable-aux-stack-check")) { + option.enable_aux_stack_check = false; + } + else if (!strcmp(argv[0], "--enable-dump-call-stack")) { + option.enable_aux_stack_frame = true; + } + else if (!strcmp(argv[0], "--enable-perf-profiling")) { + option.enable_aux_stack_frame = true; + } + else if (!strcmp(argv[0], "--enable-indirect-mode")) { + option.is_indirect_mode = true; + } + else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) { + option.disable_llvm_intrinsics = true; + } + else return print_help(); } - else if (!strcmp(argv[0], "--enable-bulk-memory")) { - option.enable_bulk_memory = true; - } - else if (!strcmp(argv[0], "--enable-multi-thread")) { - option.enable_bulk_memory = true; - option.enable_thread_mgr = true; - } - else if (!strcmp(argv[0], "--enable-tail-call")) { - option.enable_tail_call = true; - } - else if (!strcmp(argv[0], "--enable-simd")) { - /* obsolete option, kept for compatibility */ - option.enable_simd = true; - } - else if (!strcmp(argv[0], "--disable-simd")) { - option.enable_simd = false; - } - else if (!strcmp(argv[0], "--enable-ref-types")) { - option.enable_ref_types = true; - } - else if (!strcmp(argv[0], "--disable-aux-stack-check")) { - option.enable_aux_stack_check = false; - } - else if (!strcmp(argv[0], "--enable-dump-call-stack")) { - option.enable_aux_stack_frame = true; - } - else if (!strcmp(argv[0], "--enable-perf-profiling")) { - option.enable_aux_stack_frame = true; - } - else if (!strcmp(argv[0], "--enable-indirect-mode")) { - option.is_indirect_mode = true; - } - else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) { - option.disable_llvm_intrinsics = true; - } - else - return print_help(); - } - if (argc == 0 || !out_file_name) - return print_help(); + if (argc == 0 || !out_file_name) + return print_help(); - if (sgx_mode) { - option.size_level = 1; - option.is_sgx_platform = true; - } + if (sgx_mode) { + option.size_level = 1; + option.is_sgx_platform = true; + } #if WASM_ENABLE_REF_TYPES != 0 - wasm_set_ref_types_flag(option.enable_ref_types); + wasm_set_ref_types_flag(option.enable_ref_types); #endif - wasm_file_name = argv[0]; + wasm_file_name = argv[0]; - memset(&init_args, 0, sizeof(RuntimeInitArgs)); + memset(&init_args, 0, sizeof(RuntimeInitArgs)); - init_args.mem_alloc_type = Alloc_With_Allocator; - init_args.mem_alloc_option.allocator.malloc_func = malloc; - init_args.mem_alloc_option.allocator.realloc_func = realloc; - init_args.mem_alloc_option.allocator.free_func = free; + init_args.mem_alloc_type = Alloc_With_Allocator; + init_args.mem_alloc_option.allocator.malloc_func = malloc; + init_args.mem_alloc_option.allocator.realloc_func = realloc; + init_args.mem_alloc_option.allocator.free_func = free; - /* initialize runtime environment */ - if (!wasm_runtime_full_init(&init_args)) { - printf("Init runtime environment failed.\n"); - return -1; - } + /* initialize runtime environment */ + if (!wasm_runtime_full_init(&init_args)) { + printf("Init runtime environment failed.\n"); + return -1; + } - bh_log_set_verbose_level(log_verbose_level); + bh_log_set_verbose_level(log_verbose_level); - bh_print_time("Begin to load wasm file"); + bh_print_time("Begin to load wasm file"); - /* load WASM byte buffer from WASM bin file */ - if (!(wasm_file = (uint8*) - bh_read_file_to_buffer(wasm_file_name, &wasm_file_size))) - goto fail1; + /* load WASM byte buffer from WASM bin file */ + if (!(wasm_file = + (uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size))) + goto fail1; - /* load WASM module */ - if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, - error_buf, sizeof(error_buf)))) { - printf("%s\n", error_buf); - goto fail2; - } + /* load WASM module */ + if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf, + sizeof(error_buf)))) { + printf("%s\n", error_buf); + goto fail2; + } - if (!(comp_data = aot_create_comp_data(wasm_module))) { - printf("%s\n", aot_get_last_error()); - goto fail3; - } + if (!(comp_data = aot_create_comp_data(wasm_module))) { + printf("%s\n", aot_get_last_error()); + goto fail3; + } #if WASM_ENABLE_DEBUG_AOT != 0 - if (!create_dwarf_extractor(comp_data, wasm_file_name)) { - printf("%s:create dwarf extractor failed\n", wasm_file_name); - } + if (!create_dwarf_extractor(comp_data, wasm_file_name)) { + printf("%s:create dwarf extractor failed\n", wasm_file_name); + } #endif - bh_print_time("Begin to create compile context"); + bh_print_time("Begin to create compile context"); - if (!(comp_ctx = aot_create_comp_context(comp_data, - &option))) { - printf("%s\n", aot_get_last_error()); - goto fail4; - } + if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) { + printf("%s\n", aot_get_last_error()); + goto fail4; + } - bh_print_time("Begin to compile"); + bh_print_time("Begin to compile"); - if (!aot_compile_wasm(comp_ctx)) { - printf("%s\n", aot_get_last_error()); - goto fail5; - } + if (!aot_compile_wasm(comp_ctx)) { + printf("%s\n", aot_get_last_error()); + goto fail5; + } - switch (option.output_format) { - case AOT_LLVMIR_UNOPT_FILE: - case AOT_LLVMIR_OPT_FILE: - if (!aot_emit_llvm_file(comp_ctx, out_file_name)) { - printf("%s\n", aot_get_last_error()); - goto fail5; - } - break; - case AOT_OBJECT_FILE: - if (!aot_emit_object_file(comp_ctx, out_file_name)) { - printf("%s\n", aot_get_last_error()); - goto fail5; - } - break; - case AOT_FORMAT_FILE: - if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) { - printf("%s\n", aot_get_last_error()); - goto fail5; - } - break; - default: - break; - } + switch (option.output_format) { + case AOT_LLVMIR_UNOPT_FILE: + case AOT_LLVMIR_OPT_FILE: + if (!aot_emit_llvm_file(comp_ctx, out_file_name)) { + printf("%s\n", aot_get_last_error()); + goto fail5; + } + break; + case AOT_OBJECT_FILE: + if (!aot_emit_object_file(comp_ctx, out_file_name)) { + printf("%s\n", aot_get_last_error()); + goto fail5; + } + break; + case AOT_FORMAT_FILE: + if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) { + printf("%s\n", aot_get_last_error()); + goto fail5; + } + break; + default: + break; + } - bh_print_time("Compile end"); + bh_print_time("Compile end"); - printf("Compile success, file %s was generated.\n", out_file_name); + printf("Compile success, file %s was generated.\n", out_file_name); fail5: - /* Destroy compiler context */ - aot_destroy_comp_context(comp_ctx); + /* Destroy compiler context */ + aot_destroy_comp_context(comp_ctx); fail4: - /* Destroy compile data */ - aot_destroy_comp_data(comp_data); + /* Destroy compile data */ + aot_destroy_comp_data(comp_data); fail3: - /* Unload WASM module */ - wasm_runtime_unload(wasm_module); + /* Unload WASM module */ + wasm_runtime_unload(wasm_module); fail2: - /* free the file buffer */ - wasm_runtime_free(wasm_file); + /* free the file buffer */ + wasm_runtime_free(wasm_file); fail1: - /* Destroy runtime environment */ - wasm_runtime_destroy(); + /* Destroy runtime environment */ + wasm_runtime_destroy(); - bh_print_time("wamrc return"); - return 0; + bh_print_time("wamrc return"); + return 0; } -