diff --git a/blobmsg.c b/blobmsg.c index 7da4183..93172ab 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -195,9 +195,17 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, continue; if (policy[i].type != BLOBMSG_TYPE_UNSPEC && + policy[i].type != BLOBMSG_CAST_INT64 && blob_id(attr) != policy[i].type) continue; + 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)) + continue; + if (blobmsg_namelen(hdr) != pslen[i]) continue; diff --git a/blobmsg.h b/blobmsg.h index 4565082..4a151c7 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -35,6 +35,7 @@ enum blobmsg_type { __BLOBMSG_TYPE_LAST, BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1, BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8, + BLOBMSG_CAST_INT64, }; struct blobmsg_hdr { @@ -288,6 +289,38 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr) return tmp; } +static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr) +{ + uint64_t tmp = 0; + + if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + 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; +} + +static inline int64_t blobmsg_cast_s64(struct blob_attr *attr) +{ + int64_t tmp = 0; + + if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + 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; +} + static inline double blobmsg_get_double(struct blob_attr *attr) { union {