Add more types and APIs for attr_container (#1841)

Add more types for attr_container, e.g. uint8, uint32, uint64
Add more APIs for attr_container for int8, int16 and int32 types
Rename fields of the union 'jvalue' and refactor some files that use attr_container
This commit is contained in:
Qiang 2023-01-09 21:05:30 +08:00 committed by GitHub
parent df4b135086
commit 4cd88a96d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 403 additions and 75 deletions

View File

@ -7,11 +7,14 @@
typedef union jvalue { typedef union jvalue {
bool z; bool z;
int8_t b; int8_t i8;
uint16_t c; uint8_t u8;
int16_t s; int16_t i16;
int32_t i; uint16_t u16;
int64_t j; int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
float f; float f;
double d; double d;
} jvalue; } jvalue;
@ -27,7 +30,9 @@ get_int16(const char *buf)
static inline uint16_t static inline uint16_t
get_uint16(const char *buf) get_uint16(const char *buf)
{ {
return get_int16(buf); uint16_t ret;
bh_memcpy_s(&ret, sizeof(uint16_t), buf, sizeof(uint16_t));
return ret;
} }
static inline int32_t static inline int32_t
@ -41,7 +46,9 @@ get_int32(const char *buf)
static inline uint32_t static inline uint32_t
get_uint32(const char *buf) get_uint32(const char *buf)
{ {
return get_int32(buf); uint32_t ret;
bh_memcpy_s(&ret, sizeof(uint32_t), buf, sizeof(uint32_t));
return ret;
} }
static inline int64_t static inline int64_t
@ -55,7 +62,9 @@ get_int64(const char *buf)
static inline uint64_t static inline uint64_t
get_uint64(const char *buf) get_uint64(const char *buf)
{ {
return get_int64(buf); uint64_t ret;
bh_memcpy_s(&ret, sizeof(uint64_t), buf, sizeof(uint64_t));
return ret;
} }
static inline void static inline void
@ -145,8 +154,8 @@ attr_container_get_attr_next(const char *curr_attr)
p += sizeof(uint16_t) + get_uint16(p); p += sizeof(uint16_t) + get_uint16(p);
type = *p++; type = *p++;
/* Short type to Boolean type */ /* Byte type to Boolean type */
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN) { if (type >= ATTR_TYPE_BYTE && type <= ATTR_TYPE_BOOLEAN) {
p += 1 << (type & 3); p += 1 << (type & 3);
return p; return p;
} }
@ -342,7 +351,7 @@ attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
/* key len + key + '\0' + type */ /* key len + key + '\0' + type */
attr_len = sizeof(uint16_t) + strlen(key) + 1 + 1; attr_len = sizeof(uint16_t) + strlen(key) + 1 + 1;
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN) if (type >= ATTR_TYPE_BYTE && type <= ATTR_TYPE_BOOLEAN)
attr_len += 1 << (type & 3); attr_len += 1 << (type & 3);
else if (type == ATTR_TYPE_STRING) else if (type == ATTR_TYPE_STRING)
attr_len += sizeof(uint16_t) + value_length; attr_len += sizeof(uint16_t) + value_length;
@ -362,7 +371,7 @@ attr_container_set_attr(attr_container_t **p_attr_cont, const char *key,
p += str_len; p += str_len;
*p++ = type; *p++ = type;
if (type >= ATTR_TYPE_SHORT && type <= ATTR_TYPE_BOOLEAN) if (type >= ATTR_TYPE_BYTE && type <= ATTR_TYPE_BOOLEAN)
bh_memcpy_s(p, 1 << (type & 3), value, 1 << (type & 3)); bh_memcpy_s(p, 1 << (type & 3), value, 1 << (type & 3));
else if (type == ATTR_TYPE_STRING) { else if (type == ATTR_TYPE_STRING) {
set_uint16(p, value_length); set_uint16(p, value_length);
@ -460,6 +469,14 @@ attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
2); 2);
} }
bool
attr_container_set_int16(attr_container_t **p_attr_cont, const char *key,
int16_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT16, &value,
2);
}
bool bool
attr_container_set_int(attr_container_t **p_attr_cont, const char *key, attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value) int value)
@ -467,6 +484,22 @@ attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT, &value, 4); return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT, &value, 4);
} }
bool
attr_container_set_int32(attr_container_t **p_attr_cont, const char *key,
int32_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT32, &value,
4);
}
bool
attr_container_set_uint32(attr_container_t **p_attr_cont, const char *key,
uint32_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT32, &value,
4);
}
bool bool
attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
int64_t value) int64_t value)
@ -475,6 +508,14 @@ attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
8); 8);
} }
bool
attr_container_set_uint64(attr_container_t **p_attr_cont, const char *key,
uint64_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT64, &value,
8);
}
bool bool
attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
int8_t value) int8_t value)
@ -482,6 +523,21 @@ attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTE, &value, 1); return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_BYTE, &value, 1);
} }
bool
attr_container_set_int8(attr_container_t **p_attr_cont, const char *key,
int8_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_INT8, &value, 1);
}
bool
attr_container_set_uint8(attr_container_t **p_attr_cont, const char *key,
uint8_t value)
{
return attr_container_set_attr(p_attr_cont, key, ATTR_TYPE_UINT8, &value,
1);
}
bool bool
attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key, attr_container_set_uint16(attr_container_t **p_attr_cont, const char *key,
uint16_t value) uint16_t value)
@ -552,7 +608,7 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
if (!(attr_addr = attr_container_find_attr(attr_cont, key))) { if (!(attr_addr = attr_container_find_attr(attr_cont, key))) {
attr_container_printf("Get attribute failed: lookup key failed.\r\n"); attr_container_printf("Get attribute failed: lookup key failed.\r\n");
return false; return NULL;
} }
/* key len + key + '\0' */ /* key len + key + '\0' */
@ -566,14 +622,17 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
uint8_t type; \ uint8_t type; \
if (!addr) \ if (!addr) \
return 0; \ return 0; \
val.j = 0; \ val.i64 = 0; \
type = *(uint8_t *)addr++; \ type = *(uint8_t *)addr++; \
switch (type) { \ switch (type) { \
case ATTR_TYPE_SHORT: \ case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */ \
case ATTR_TYPE_INT: \ case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */ \
case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */ \
case ATTR_TYPE_INT64: \ case ATTR_TYPE_INT64: \
case ATTR_TYPE_BYTE: \ case ATTR_TYPE_UINT8: \
case ATTR_TYPE_UINT16: \ case ATTR_TYPE_UINT16: \
case ATTR_TYPE_UINT32: \
case ATTR_TYPE_UINT64: \
case ATTR_TYPE_FLOAT: \ case ATTR_TYPE_FLOAT: \
case ATTR_TYPE_DOUBLE: \ case ATTR_TYPE_DOUBLE: \
case ATTR_TYPE_BOOLEAN: \ case ATTR_TYPE_BOOLEAN: \
@ -608,31 +667,67 @@ attr_container_get_attr(const attr_container_t *attr_cont, const char *key)
short short
attr_container_get_as_short(const attr_container_t *attr_cont, const char *key) attr_container_get_as_short(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i16);
}
int16_t
attr_container_get_as_int16(const attr_container_t *attr_cont, const char *key)
{
return (int16_t)attr_container_get_as_short(attr_cont, key);
} }
int int
attr_container_get_as_int(const attr_container_t *attr_cont, const char *key) attr_container_get_as_int(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i32);
}
int32_t
attr_container_get_as_int32(const attr_container_t *attr_cont, const char *key)
{
return (int32_t)attr_container_get_as_int(attr_cont, key);
}
uint32_t
attr_container_get_as_uint32(const attr_container_t *attr_cont, const char *key)
{
return (uint32_t)attr_container_get_as_int(attr_cont, key);
} }
int64_t int64_t
attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key) attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, j); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i64);
}
uint64_t
attr_container_get_as_uint64(const attr_container_t *attr_cont, const char *key)
{
return (uint64_t)attr_container_get_as_int64(attr_cont, key);
} }
int8_t int8_t
attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key) attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, b); TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, i8);
}
int8_t
attr_container_get_as_int8(const attr_container_t *attr_cont, const char *key)
{
return attr_container_get_as_byte(attr_cont, key);
}
uint8_t
attr_container_get_as_uint8(const attr_container_t *attr_cont, const char *key)
{
return (uint8_t)attr_container_get_as_byte(attr_cont, key);
} }
uint16_t uint16_t
attr_container_get_as_uint16(const attr_container_t *attr_cont, const char *key) attr_container_get_as_uint16(const attr_container_t *attr_cont, const char *key)
{ {
TEMPLATE_ATTR_BUF_TO_VALUE(attr_cont, key, s); return (uint16_t)attr_container_get_as_short(attr_cont, key);
} }
float float
@ -671,11 +766,14 @@ attr_container_get_as_bytearray(const attr_container_t *attr_cont,
type = *(uint8_t *)addr++; type = *(uint8_t *)addr++;
switch (type) { switch (type) {
case ATTR_TYPE_SHORT: case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */
case ATTR_TYPE_INT: case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */
case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */
case ATTR_TYPE_INT64: case ATTR_TYPE_INT64:
case ATTR_TYPE_BYTE: case ATTR_TYPE_UINT8:
case ATTR_TYPE_UINT16: case ATTR_TYPE_UINT16:
case ATTR_TYPE_UINT32:
case ATTR_TYPE_UINT64:
case ATTR_TYPE_FLOAT: case ATTR_TYPE_FLOAT:
case ATTR_TYPE_DOUBLE: case ATTR_TYPE_DOUBLE:
case ATTR_TYPE_BOOLEAN: case ATTR_TYPE_BOOLEAN:
@ -807,34 +905,52 @@ attr_container_dump(const attr_container_t *attr_cont)
attr_container_printf(" key: %s", key); attr_container_printf(" key: %s", key);
switch (type) { switch (type) {
case ATTR_TYPE_SHORT: case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); bh_memcpy_s(&value.i8, 1, p, 1);
attr_container_printf(", type: byte, value: 0x%x\n",
value.i8 & 0xFF);
p++;
break;
case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */
bh_memcpy_s(&value.i16, sizeof(int16_t), p, sizeof(int16_t));
attr_container_printf(", type: short, value: 0x%x\n", attr_container_printf(", type: short, value: 0x%x\n",
value.s & 0xFFFF); value.i16 & 0xFFFF);
p += 2; p += 2;
break; break;
case ATTR_TYPE_INT: case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); bh_memcpy_s(&value.i32, sizeof(int32_t), p, sizeof(int32_t));
attr_container_printf(", type: int, value: 0x%x\n", value.i); attr_container_printf(", type: int, value: 0x%x\n", value.i32);
p += 4; p += 4;
break; break;
case ATTR_TYPE_INT64: case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); bh_memcpy_s(&value.i64, sizeof(int64_t), p, sizeof(int64_t));
attr_container_printf(", type: int64, value: 0x%llx\n", attr_container_printf(", type: int64, value: 0x%llx\n",
(long long unsigned int)(value.j)); (long long unsigned int)(value.i64));
p += 8; p += 8;
break; break;
case ATTR_TYPE_BYTE: case ATTR_TYPE_UINT8:
bh_memcpy_s(&value.b, 1, p, 1); bh_memcpy_s(&value.u8, 1, p, 1);
attr_container_printf(", type: byte, value: 0x%x\n", attr_container_printf(", type: uint8, value: 0x%x\n", value.u8);
value.b & 0xFF);
p++; p++;
break; break;
case ATTR_TYPE_UINT16: case ATTR_TYPE_UINT16:
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); bh_memcpy_s(&value.u16, sizeof(uint16_t), p, sizeof(uint16_t));
attr_container_printf(", type: uint16, value: 0x%x\n", value.c); attr_container_printf(", type: uint16, value: 0x%x\n",
value.u16);
p += 2; p += 2;
break; break;
case ATTR_TYPE_UINT32:
bh_memcpy_s(&value.u32, sizeof(uint32_t), p, sizeof(uint32_t));
attr_container_printf(", type: uint32, value: 0x%x\n",
value.u32);
p += 4;
break;
case ATTR_TYPE_UINT64:
bh_memcpy_s(&value.u64, sizeof(uint64_t), p, sizeof(uint64_t));
attr_container_printf(", type: int64, value: 0x%llx\n",
(long long unsigned int)(value.u64));
p += 8;
break;
case ATTR_TYPE_FLOAT: case ATTR_TYPE_FLOAT:
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
attr_container_printf(", type: float, value: %f\n", value.f); attr_container_printf(", type: float, value: %f\n", value.f);

View File

@ -20,13 +20,27 @@ extern "C" {
/* Attribute type */ /* Attribute type */
enum { enum {
ATTR_TYPE_BEGIN = 1, ATTR_TYPE_BEGIN = 0,
ATTR_TYPE_SHORT = ATTR_TYPE_BEGIN, ATTR_TYPE_BYTE = ATTR_TYPE_BEGIN,
ATTR_TYPE_INT8 = ATTR_TYPE_BYTE,
ATTR_TYPE_SHORT,
ATTR_TYPE_INT16 = ATTR_TYPE_SHORT,
ATTR_TYPE_INT, ATTR_TYPE_INT,
ATTR_TYPE_INT32 = ATTR_TYPE_INT,
ATTR_TYPE_INT64, ATTR_TYPE_INT64,
ATTR_TYPE_BYTE, ATTR_TYPE_UINT8,
ATTR_TYPE_UINT16, ATTR_TYPE_UINT16,
ATTR_TYPE_FLOAT, ATTR_TYPE_UINT32,
ATTR_TYPE_UINT64,
/**
* Why ATTR_TYPE_FLOAT = 10?
* We determine the number of bytes that should be copied through 1<<(type &
* 3). ATTR_TYPE_BYTE = 0, so the number of bytes is 1 << 0 = 1.
* ATTR_TYPE_UINT64 = 7, so the number of bytes is 1 << 3 = 8.
* Since the float type takes up 4 bytes, ATTR_TYPE_FLOAT should be 10.
* Calculation: (1 << (10&3)) = (1 << 2) = 4
*/
ATTR_TYPE_FLOAT = 10,
ATTR_TYPE_DOUBLE, ATTR_TYPE_DOUBLE,
ATTR_TYPE_BOOLEAN, ATTR_TYPE_BOOLEAN,
ATTR_TYPE_STRING, ATTR_TYPE_STRING,
@ -89,6 +103,20 @@ bool
attr_container_set_short(attr_container_t **p_attr_cont, const char *key, attr_container_set_short(attr_container_t **p_attr_cont, const char *key,
short value); short value);
/**
* Set int16 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_int16(attr_container_t **p_attr_cont, const char *key,
int16_t value);
/** /**
* Set int attribute in attribute container * Set int attribute in attribute container
* *
@ -103,6 +131,34 @@ bool
attr_container_set_int(attr_container_t **p_attr_cont, const char *key, attr_container_set_int(attr_container_t **p_attr_cont, const char *key,
int value); int value);
/**
* Set int32 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_int32(attr_container_t **p_attr_cont, const char *key,
int32_t value);
/**
* Set uint32 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_uint32(attr_container_t **p_attr_cont, const char *key,
uint32_t value);
/** /**
* Set int64 attribute in attribute container * Set int64 attribute in attribute container
* *
@ -117,6 +173,20 @@ bool
attr_container_set_int64(attr_container_t **p_attr_cont, const char *key, attr_container_set_int64(attr_container_t **p_attr_cont, const char *key,
int64_t value); int64_t value);
/**
* Set uint64 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_uint64(attr_container_t **p_attr_cont, const char *key,
uint64_t value);
/** /**
* Set byte attribute in attribute container * Set byte attribute in attribute container
* *
@ -131,6 +201,34 @@ bool
attr_container_set_byte(attr_container_t **p_attr_cont, const char *key, attr_container_set_byte(attr_container_t **p_attr_cont, const char *key,
int8_t value); int8_t value);
/**
* Set int8 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_int8(attr_container_t **p_attr_cont, const char *key,
int8_t value);
/**
* Set uint8 attribute in attribute container
*
* @param p_attr_cont pointer to attribute container to set attribute, and
* return the new attribute container if it is re-created
* @param key the attribute key
* @param value the attribute value
*
* @return true if success, false otherwise
*/
bool
attr_container_set_uint8(attr_container_t **p_attr_cont, const char *key,
uint8_t value);
/** /**
* Set uint16 attribute in attribute container * Set uint16 attribute in attribute container
* *
@ -259,6 +357,18 @@ attr_container_contain_key(const attr_container_t *attr_cont, const char *key);
short short
attr_container_get_as_short(const attr_container_t *attr_cont, const char *key); attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as int16 value,
* return 0 if attribute isn't found in message.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the short value of the attribute, 0 if key isn't found
*/
int16_t
attr_container_get_as_int16(const attr_container_t *attr_cont, const char *key);
/** /**
* Get attribute from attribute container and return it as int value, * Get attribute from attribute container and return it as int value,
* return 0 if attribute isn't found in message. * return 0 if attribute isn't found in message.
@ -271,6 +381,31 @@ attr_container_get_as_short(const attr_container_t *attr_cont, const char *key);
int int
attr_container_get_as_int(const attr_container_t *attr_cont, const char *key); attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as int32 value,
* return 0 if attribute isn't found in message.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the int value of the attribute, 0 if key isn't found
*/
int32_t
attr_container_get_as_int32(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as uint32 value,
* return 0 if attribute isn't found in message.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the unsigned int value of the attribute, 0 if key isn't found
*/
uint32_t
attr_container_get_as_uint32(const attr_container_t *attr_cont,
const char *key);
/** /**
* Get attribute from attribute container and return it as int64 value, * Get attribute from attribute container and return it as int64 value,
* return 0 if attribute isn't found in attribute container. * return 0 if attribute isn't found in attribute container.
@ -283,6 +418,19 @@ attr_container_get_as_int(const attr_container_t *attr_cont, const char *key);
int64_t int64_t
attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key); attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as uint64 value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the unsigned long value of the attribute, 0 if key isn't found
*/
uint64_t
attr_container_get_as_uint64(const attr_container_t *attr_cont,
const char *key);
/** /**
* Get attribute from attribute container and return it as byte value, * Get attribute from attribute container and return it as byte value,
* return 0 if attribute isn't found in attribute container. * return 0 if attribute isn't found in attribute container.
@ -295,6 +443,30 @@ attr_container_get_as_int64(const attr_container_t *attr_cont, const char *key);
int8_t int8_t
attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key); attr_container_get_as_byte(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as int8 value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the byte value of the attribute, 0 if key isn't found
*/
int8_t
attr_container_get_as_int8(const attr_container_t *attr_cont, const char *key);
/**
* Get attribute from attribute container and return it as uint8 value,
* return 0 if attribute isn't found in attribute container.
*
* @param attr_cont the attribute container
* @param key the attribute key
*
* @return the uint8 value of the attribute, 0 if key isn't found
*/
uint8_t
attr_container_get_as_uint8(const attr_container_t *attr_cont, const char *key);
/** /**
* Get attribute from attribute container and return it as uint16 value, * Get attribute from attribute container and return it as uint16 value,
* return 0 if attribute isn't found in attribute container. * return 0 if attribute isn't found in attribute container.

View File

@ -25,9 +25,9 @@ SRCS += $(APP_FRAMEWORK_DIR)/wgl/app/src/*.c
all: all:
@$(CC) $(CFLAGS) $(SRCS) \ @$(CC) $(CFLAGS) $(SRCS) \
--target=wasm32 -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \ --target=wasm32-wasi -O3 -z stack-size=2048 -Wl,--initial-memory=65536 \
-Wl,--allow-undefined \ -nostdlib -Wl,--allow-undefined \
-Wl,--strip-all,--no-entry -nostdlib \ -Wl,--strip-all,--no-entry \
-Wl,--export=on_init -Wl,--export=on_timer_callback \ -Wl,--export=on_init -Wl,--export=on_timer_callback \
-Wl,--export=on_widget_event \ -Wl,--export=on_widget_event \
-Wl,--export=__heap_base,--export=__data_end \ -Wl,--export=__heap_base,--export=__data_end \

View File

@ -33,7 +33,7 @@
</h1> </h1>
<p> <p>
1. Download a simple runtime (build for ubuntu 20.04 64 bits, other platforms please build 1. Download a simple runtime (build for ubuntu 20.04 64 bits, other platforms please build
from the <a href="https://github.com/intel/wasm-micro-runtime">source code</a>) from the <a href="https://github.com/bytecodealliance/wasm-micro-runtime/tree/main/samples/simple">source code</a>)
</p> </p>
<p> <p>
2. In the terminal: <code>cd ~/Download && ./simple -a 82.156.57.236</code> 2. In the terminal: <code>cd ~/Download && ./simple -a 82.156.57.236</code>
@ -44,7 +44,7 @@
<h5> <h5>
Notes: Notes:
</h5> We also have a <strong>UI-enabled runtime</strong>, please <a </h5> We also have a <strong>UI-enabled runtime</strong>, please <a
href="../static/upload/simple">download here</a> and enjoy.</strong> It may require href="../static/upload/wasm_runtime_wgl">download here</a> and enjoy.</strong> It may require
a few more setups. a few more setups.
<p>Before running the UI-enabled runtime, please install some required softwares:</p> <p>Before running the UI-enabled runtime, please install some required softwares:</p>
<p><code>sudo apt-get install libsdl2-2.0-0:i386</code> </p> <p><code>sudo apt-get install libsdl2-2.0-0:i386</code> </p>

View File

@ -16,14 +16,18 @@ import logging
import os import os
attr_type_list = [ attr_type_list = [
"ATTR_NONE", "ATTR_TYPE_BYTE", # = ATTR_TYPE_INT8
"ATTR_TYPE_SHORT", "ATTR_TYPE_SHORT",# = ATTR_TYPE_INT16
"ATTR_TYPE_INT", "ATTR_TYPE_INT", # = ATTR_TYPE_INT32
"ATTR_TYPE_INT64", "ATTR_TYPE_INT64",
"ATTR_TYPE_BYTE", "ATTR_TYPE_UINT8",
"ATTR_TYPE_UINT16", "ATTR_TYPE_UINT16",
"ATTR_TYPE_UINT32",
"ATTR_TYPE_UINT64",
"ATTR_TYPE_FLOAT", "ATTR_TYPE_FLOAT",
"ATTR_TYPE_DOUBLE", "ATTR_TYPE_DOUBLE",
"ATTR_NONE",
"ATTR_NONE",
"ATTR_TYPE_BOOLEAN", "ATTR_TYPE_BOOLEAN",
"ATTR_TYPE_STRING", "ATTR_TYPE_STRING",
"ATTR_TYPE_BYTEARRAY" "ATTR_TYPE_BYTEARRAY"
@ -140,26 +144,38 @@ def decode_attr_container(msg):
attr_type = attr_type_list[int(type_index[0])] attr_type = attr_type_list[int(type_index[0])]
buf = buf[1 : ] buf = buf[1 : ]
if attr_type == "ATTR_TYPE_SHORT": if attr_type == "ATTR_TYPE_BYTE": # = ATTR_TYPE_INT8
(attr_value, ) = struct.unpack('@c', buf[0 : 1])
buf = buf[1 : ]
# continue
elif attr_type == "ATTR_TYPE_SHORT": # = ATTR_TYPE_INT16
(attr_value, ) = struct.unpack('@h', buf[0 : 2]) (attr_value, ) = struct.unpack('@h', buf[0 : 2])
buf = buf[2 : ] buf = buf[2 : ]
# continue # continue
elif attr_type == "ATTR_TYPE_INT": elif attr_type == "ATTR_TYPE_INT": # = ATTR_TYPE_INT32
(attr_value, ) = struct.unpack('@I', buf[0 : 4]) (attr_value, ) = struct.unpack('@i', buf[0 : 4])
buf = buf[4 : ] buf = buf[4 : ]
# continue # continue
elif attr_type == "ATTR_TYPE_INT64": elif attr_type == "ATTR_TYPE_INT64":
(attr_value, ) = struct.unpack('@q', buf[0 : 8]) (attr_value, ) = struct.unpack('@q', buf[0 : 8])
buf = buf[8 : ] buf = buf[8 : ]
# continue # continue
elif attr_type == "ATTR_TYPE_BYTE": elif attr_type == "ATTR_TYPE_UINT8":
(attr_value, ) = struct.unpack('@c', buf[0 : 1]) (attr_value, ) = struct.unpack('@B', buf[0 : 1])
buf = buf[1 : ] buf = buf[1 : ]
# continue # continue
elif attr_type == "ATTR_TYPE_UINT16": elif attr_type == "ATTR_TYPE_UINT16":
(attr_value, ) = struct.unpack('@H', buf[0 : 2]) (attr_value, ) = struct.unpack('@H', buf[0 : 2])
buf = buf[2 : ] buf = buf[2 : ]
# continue # continue
elif attr_type == "ATTR_TYPE_UINT32":
(attr_value, ) = struct.unpack('@I', buf[0 : 4])
buf = buf[4 : ]
# continue
elif attr_type == "ATTR_TYPE_UINT64":
(attr_value, ) = struct.unpack('@Q', buf[0 : 8])
buf = buf[8 : ]
# continue
elif attr_type == "ATTR_TYPE_FLOAT": elif attr_type == "ATTR_TYPE_FLOAT":
(attr_value, ) = struct.unpack('@f', buf[0 : 4]) (attr_value, ) = struct.unpack('@f', buf[0 : 4])
buf = buf[4 : ] buf = buf[4 : ]

View File

@ -15,11 +15,14 @@
typedef union jvalue { typedef union jvalue {
bool z; bool z;
int8_t b; int8_t i8;
uint16_t c; uint8_t u8;
int16_t s; int16_t i16;
int32_t i; uint16_t u16;
int64_t j; int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
float f; float f;
double d; double d;
} jvalue; } jvalue;
@ -90,43 +93,64 @@ attr2json(const attr_container_t *attr_cont)
type = *p++; type = *p++;
switch (type) { switch (type) {
case ATTR_TYPE_SHORT: case ATTR_TYPE_BYTE: /* = ATTR_TYPE_INT8 */
bh_memcpy_s(&value.s, sizeof(int16_t), p, sizeof(int16_t)); bh_memcpy_s(&value.i8, 1, p, 1);
if (NULL == (obj = cJSON_CreateNumber(value.s))) if (NULL == (obj = cJSON_CreateNumber(value.i8)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p++;
break;
case ATTR_TYPE_SHORT: /* = ATTR_TYPE_INT16 */
bh_memcpy_s(&value.i16, sizeof(int16_t), p, sizeof(int16_t));
if (NULL == (obj = cJSON_CreateNumber(value.i16)))
goto fail; goto fail;
cJSON_AddItemToObject(root, key, obj); cJSON_AddItemToObject(root, key, obj);
/* another approach: cJSON_AddNumberToObject(root, key, value.s) /* another approach: cJSON_AddNumberToObject(root, key, value.s)
*/ */
p += 2; p += 2;
break; break;
case ATTR_TYPE_INT: case ATTR_TYPE_INT: /* = ATTR_TYPE_INT32 */
bh_memcpy_s(&value.i, sizeof(int32_t), p, sizeof(int32_t)); bh_memcpy_s(&value.i32, sizeof(int32_t), p, sizeof(int32_t));
if (NULL == (obj = cJSON_CreateNumber(value.i))) if (NULL == (obj = cJSON_CreateNumber(value.i32)))
goto fail; goto fail;
cJSON_AddItemToObject(root, key, obj); cJSON_AddItemToObject(root, key, obj);
p += 4; p += 4;
break; break;
case ATTR_TYPE_INT64: case ATTR_TYPE_INT64:
bh_memcpy_s(&value.j, sizeof(uint64_t), p, sizeof(uint64_t)); bh_memcpy_s(&value.i64, sizeof(int64_t), p, sizeof(int64_t));
if (NULL == (obj = cJSON_CreateNumber(value.j))) if (NULL == (obj = cJSON_CreateNumber(value.i64)))
goto fail; goto fail;
cJSON_AddItemToObject(root, key, obj); cJSON_AddItemToObject(root, key, obj);
p += 8; p += 8;
break; break;
case ATTR_TYPE_BYTE: case ATTR_TYPE_UINT8:
bh_memcpy_s(&value.b, 1, p, 1); bh_memcpy_s(&value.u8, 1, p, 1);
if (NULL == (obj = cJSON_CreateNumber(value.b))) if (NULL == (obj = cJSON_CreateNumber(value.u8)))
goto fail; goto fail;
cJSON_AddItemToObject(root, key, obj); cJSON_AddItemToObject(root, key, obj);
p++; p++;
break; break;
case ATTR_TYPE_UINT16: case ATTR_TYPE_UINT16:
bh_memcpy_s(&value.c, sizeof(uint16_t), p, sizeof(uint16_t)); bh_memcpy_s(&value.u16, sizeof(uint16_t), p, sizeof(uint16_t));
if (NULL == (obj = cJSON_CreateNumber(value.c))) if (NULL == (obj = cJSON_CreateNumber(value.u16)))
goto fail; goto fail;
cJSON_AddItemToObject(root, key, obj); cJSON_AddItemToObject(root, key, obj);
p += 2; p += 2;
break; break;
case ATTR_TYPE_UINT32:
bh_memcpy_s(&value.u32, sizeof(uint32_t), p, sizeof(uint32_t));
if (NULL == (obj = cJSON_CreateNumber(value.u32)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 4;
break;
case ATTR_TYPE_UINT64:
bh_memcpy_s(&value.u64, sizeof(uint64_t), p, sizeof(uint64_t));
if (NULL == (obj = cJSON_CreateNumber(value.u64)))
goto fail;
cJSON_AddItemToObject(root, key, obj);
p += 8;
break;
case ATTR_TYPE_FLOAT: case ATTR_TYPE_FLOAT:
bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float)); bh_memcpy_s(&value.f, sizeof(float), p, sizeof(float));
if (NULL == (obj = cJSON_CreateNumber(value.f))) if (NULL == (obj = cJSON_CreateNumber(value.f)))