Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ blob_nest_end(struct blob_buf *buf, void *cookie)

static const size_t blob_type_minlen[BLOB_ATTR_LAST] = {
[BLOB_ATTR_STRING] = 1,
[BLOB_ATTR_INT8] = sizeof(uint8_t),
[BLOB_ATTR_INT16] = sizeof(uint16_t),
[BLOB_ATTR_BOOL] = sizeof(bool),
[BLOB_ATTR_INT32] = sizeof(uint32_t),
[BLOB_ATTR_INT64] = sizeof(uint64_t),
[BLOB_ATTR_DOUBLE] = sizeof(double),
Expand All @@ -206,7 +205,7 @@ blob_check_type(const void *ptr, unsigned int len, int type)
if (type >= BLOB_ATTR_LAST)
return false;

if (type >= BLOB_ATTR_INT8 && type <= BLOB_ATTR_INT64) {
if (type >= BLOB_ATTR_BOOL && type <= BLOB_ATTR_INT64) {
if (len != blob_type_minlen[type])
return false;
} else {
Expand Down
46 changes: 13 additions & 33 deletions blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enum {
BLOB_ATTR_NESTED,
BLOB_ATTR_BINARY,
BLOB_ATTR_STRING,
BLOB_ATTR_INT8,
BLOB_ATTR_INT16,
BLOB_ATTR_BOOL,
__BLOB_ATTR_DEPRECATED_INT16,
BLOB_ATTR_INT32,
BLOB_ATTR_INT64,
BLOB_ATTR_DOUBLE,
Expand Down Expand Up @@ -122,17 +122,10 @@ blob_pad_len(const struct blob_attr *attr)
return len;
}

static inline uint8_t
blob_get_u8(const struct blob_attr *attr)
{
return *((uint8_t *) attr->data);
}

static inline uint16_t
blob_get_u16(const struct blob_attr *attr)
static inline bool
blob_get_bool(const struct blob_attr *attr)
{
uint16_t *tmp = (uint16_t*)attr->data;
return be16_to_cpu(*tmp);
return *((bool *) attr->data);
}

static inline uint32_t
Expand All @@ -151,17 +144,8 @@ blob_get_u64(const struct blob_attr *attr)
return tmp;
}

static inline int8_t
blob_get_int8(const struct blob_attr *attr)
{
return blob_get_u8(attr);
}

static inline int16_t
blob_get_int16(const struct blob_attr *attr)
{
return blob_get_u16(attr);
}
#define blob_get_u8 blob_get_bool
#define blob_get_u16 blob_get_u32

static inline int32_t
blob_get_int32(const struct blob_attr *attr)
Expand All @@ -181,6 +165,9 @@ blob_get_string(const struct blob_attr *attr)
return attr->data;
}

#define blob_get_int8 blob_get_bool
#define blob_get_int16 blob_get_int32

static inline struct blob_attr *
blob_next(const struct blob_attr *attr)
{
Expand Down Expand Up @@ -210,15 +197,8 @@ blob_put_string(struct blob_buf *buf, int id, const char *str)
}

static inline struct blob_attr *
blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
{
return blob_put(buf, id, &val, sizeof(val));
}

static inline struct blob_attr *
blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
blob_put_bool(struct blob_buf *buf, int id, bool val)
{
val = cpu_to_be16(val);
return blob_put(buf, id, &val, sizeof(val));
}

Expand All @@ -236,8 +216,8 @@ blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
return blob_put(buf, id, &val, sizeof(val));
}

#define blob_put_int8 blob_put_u8
#define blob_put_int16 blob_put_u16
#define blob_put_int8 blob_put_bool
#define blob_put_int16 blob_put_u32
#define blob_put_int32 blob_put_u32
#define blob_put_int64 blob_put_u64

Expand Down
6 changes: 2 additions & 4 deletions blobmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
#include "blobmsg.h"

static const int blob_type[__BLOBMSG_TYPE_LAST] = {
[BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8,
[BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16,
[BLOBMSG_TYPE_BOOL] = BLOB_ATTR_BOOL,
[BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32,
[BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64,
[BLOBMSG_TYPE_DOUBLE] = BLOB_ATTR_DOUBLE,
Expand Down Expand Up @@ -202,8 +201,7 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
if (policy[i].type == BLOBMSG_CAST_INT64 &&
(blob_id(attr) != BLOBMSG_TYPE_INT64 &&
blob_id(attr) != BLOBMSG_TYPE_INT32 &&
blob_id(attr) != BLOBMSG_TYPE_INT16 &&
blob_id(attr) != BLOBMSG_TYPE_INT8))
blob_id(attr) != BLOBMSG_TYPE_BOOL))
continue;

if (blobmsg_namelen(hdr) != pslen[i])
Expand Down
40 changes: 10 additions & 30 deletions blobmsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ enum blobmsg_type {
BLOBMSG_TYPE_STRING,
BLOBMSG_TYPE_INT64,
BLOBMSG_TYPE_INT32,
BLOBMSG_TYPE_INT16,
BLOBMSG_TYPE_INT8,
BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
__BLOBMSG_TYPE_DEPRECATED_INT16,
BLOBMSG_TYPE_BOOL,
BLOBMSG_TYPE_DOUBLE,
__BLOBMSG_TYPE_LAST,
BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1,
Expand Down Expand Up @@ -205,16 +204,9 @@ blobmsg_add_double(struct blob_buf *buf, const char *name, double val)
}

static inline int
blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
blobmsg_add_bool(struct blob_buf *buf, const char *name, bool val)
{
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1);
}

static inline int
blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
{
val = cpu_to_be16(val);
return blobmsg_add_field(buf, BLOBMSG_TYPE_INT16, name, &val, 2);
return blobmsg_add_field(buf, BLOBMSG_TYPE_BOOL, name, &val, 1);
}

static inline int
Expand Down Expand Up @@ -244,6 +236,9 @@ blobmsg_add_blob(struct blob_buf *buf, struct blob_attr *attr)
blobmsg_data(attr), blobmsg_data_len(attr));
}

#define blobmsg_add_u8 blobmsg_add_bool
#define blobmsg_add_u16 blobmsg_add_u32

void *blobmsg_open_nested(struct blob_buf *buf, const char *name, bool array);

static inline void *
Expand Down Expand Up @@ -275,21 +270,11 @@ static inline int blobmsg_buf_init(struct blob_buf *buf)
return blob_buf_init(buf, BLOBMSG_TYPE_TABLE);
}

static inline uint8_t blobmsg_get_u8(struct blob_attr *attr)
{
return *(uint8_t *) blobmsg_data(attr);
}

static inline bool blobmsg_get_bool(struct blob_attr *attr)
{
return *(uint8_t *) blobmsg_data(attr);
}

static inline uint16_t blobmsg_get_u16(struct blob_attr *attr)
{
return be16_to_cpu(*(uint16_t *) blobmsg_data(attr));
}

static inline uint32_t blobmsg_get_u32(struct blob_attr *attr)
{
return be32_to_cpu(*(uint32_t *) blobmsg_data(attr));
Expand All @@ -303,6 +288,9 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
return tmp;
}

#define blobmsg_get_u8 blobmsg_get_bool
#define blobmsg_get_u16 blobmsg_get_u32

static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr)
{
uint64_t tmp = 0;
Expand All @@ -311,10 +299,6 @@ static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr)
tmp = blobmsg_get_u64(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32)
tmp = blobmsg_get_u32(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16)
tmp = blobmsg_get_u16(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8)
tmp = blobmsg_get_u8(attr);

return tmp;
}
Expand All @@ -327,10 +311,6 @@ static inline int64_t blobmsg_cast_s64(struct blob_attr *attr)
tmp = blobmsg_get_u64(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32)
tmp = (int32_t)blobmsg_get_u32(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16)
tmp = (int16_t)blobmsg_get_u16(attr);
else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8)
tmp = (int8_t)blobmsg_get_u8(attr);

return tmp;
}
Expand Down
7 changes: 2 additions & 5 deletions blobmsg_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
blobmsg_add_string(b, name, json_object_get_string(obj));
break;
case json_type_boolean:
blobmsg_add_u8(b, name, json_object_get_boolean(obj));
blobmsg_add_bool(b, name, json_object_get_boolean(obj));
break;
case json_type_int: {
int64_t i64 = json_object_get_int64(obj);
Expand Down Expand Up @@ -248,10 +248,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo
snprintf(buf, sizeof(buf), "null");
break;
case BLOBMSG_TYPE_BOOL:
snprintf(buf, sizeof(buf), "%s", *(uint8_t *)data ? "true" : "false");
break;
case BLOBMSG_TYPE_INT16:
snprintf(buf, sizeof(buf), "%" PRId16, (int16_t) be16_to_cpu(*(uint16_t *)data));
snprintf(buf, sizeof(buf), "%s", *(bool *)data ? "true" : "false");
break;
case BLOBMSG_TYPE_INT32:
snprintf(buf, sizeof(buf), "%" PRId32, (int32_t) be32_to_cpu(*(uint32_t *)data));
Expand Down
Loading