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

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

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-10-21 13:58:34 +08:00
committed by GitHub
parent 225f5d0a64
commit 32242988ed
143 changed files with 5377 additions and 4627 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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