diff --git a/blob.h b/blob.h index 13bc7cc..03e4ec3 100644 --- a/blob.h +++ b/blob.h @@ -122,18 +122,34 @@ blob_pad_len(const struct blob_attr *attr) return len; } +static inline bool +blob_get_bool(const struct blob_attr *attr) +{ + return !!(*((uint8_t *)attr->data)); +} + static inline uint8_t -blob_get_u8(const struct blob_attr *attr) +__blob_get_u8(const struct blob_attr *attr) { return *((uint8_t *) attr->data); } +static inline __deprecated uint8_t +blob_get_u8(const struct blob_attr *attr) +{ + return __blob_get_u8(attr); +} static inline uint16_t -blob_get_u16(const struct blob_attr *attr) +__blob_get_u16(const struct blob_attr *attr) { uint16_t *tmp = (uint16_t*)attr->data; return be16_to_cpu(*tmp); } +static inline __deprecated uint16_t +blob_get_u16(const struct blob_attr *attr) +{ + return __blob_get_u16(attr); +} static inline uint32_t blob_get_u32(const struct blob_attr *attr) @@ -151,16 +167,16 @@ blob_get_u64(const struct blob_attr *attr) return tmp; } -static inline int8_t +static inline __deprecated int8_t blob_get_int8(const struct blob_attr *attr) { - return blob_get_u8(attr); + return __blob_get_u8(attr); } -static inline int16_t +static inline __deprecated int16_t blob_get_int16(const struct blob_attr *attr) { - return blob_get_u16(attr); + return __blob_get_u16(attr); } static inline int32_t @@ -210,12 +226,18 @@ blob_put_string(struct blob_buf *buf, int id, const char *str) } static inline struct blob_attr * +blob_put_bool(struct blob_buf *buf, int id, bool val) +{ + return blob_put(buf, id, &val, sizeof(val)); +} + +static inline __deprecated 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 * +static inline __deprecated struct blob_attr * blob_put_u16(struct blob_buf *buf, int id, uint16_t val) { val = cpu_to_be16(val); diff --git a/blobmsg.h b/blobmsg.h index f2fc0d0..4a21e63 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -192,6 +192,12 @@ blobmsg_parse_array_attr(const struct blobmsg_policy *policy, int policy_len, return blobmsg_parse_array(policy, policy_len, tb, blobmsg_data(data), blobmsg_len(data)); } +static inline int +blobmsg_add_bool(struct blob_buf *buf, const char *name, bool val) +{ + return blobmsg_add_field(buf, BLOBMSG_TYPE_BOOL, name, &val, 1); +} + static inline int blobmsg_add_double(struct blob_buf *buf, const char *name, double val) { @@ -205,17 +211,27 @@ 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_u8(struct blob_buf *buf, const char *name, uint8_t val) { return blobmsg_add_field(buf, BLOBMSG_TYPE_INT8, name, &val, 1); } +static inline __deprecated int +blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val) +{ + return __blobmsg_add_u8(buf, name, val); +} static inline int -blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val) +__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); } +static inline __deprecated int +blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val) +{ + return __blobmsg_add_u16(buf, name, val); +} static inline int blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val) @@ -275,20 +291,28 @@ 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) +static inline uint8_t __blobmsg_get_u8(struct blob_attr *attr) { return *(uint8_t *) blobmsg_data(attr); } +static inline __deprecated uint8_t blobmsg_get_u8(struct blob_attr *attr) +{ + return __blobmsg_get_u8(attr); +} static inline bool blobmsg_get_bool(struct blob_attr *attr) { - return *(uint8_t *) blobmsg_data(attr); + return !!(*(uint8_t *) blobmsg_data(attr)); } -static inline uint16_t blobmsg_get_u16(struct blob_attr *attr) +static inline uint16_t __blobmsg_get_u16(struct blob_attr *attr) { return be16_to_cpu(*(uint16_t *) blobmsg_data(attr)); } +static inline __deprecated uint16_t blobmsg_get_u16(struct blob_attr *attr) +{ + return __blobmsg_get_u16(attr); +} static inline uint32_t blobmsg_get_u32(struct blob_attr *attr) { @@ -312,9 +336,9 @@ static inline uint64_t blobmsg_cast_u64(struct blob_attr *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); + tmp = __blobmsg_get_u16(attr); else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) - tmp = blobmsg_get_u8(attr); + tmp = __blobmsg_get_u8(attr); return tmp; } @@ -328,9 +352,9 @@ static inline int64_t blobmsg_cast_s64(struct blob_attr *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); + tmp = (int16_t)__blobmsg_get_u16(attr); else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) - tmp = (int8_t)blobmsg_get_u8(attr); + tmp = (int8_t)__blobmsg_get_u8(attr); return tmp; } diff --git a/blobmsg_json.c b/blobmsg_json.c index 31eec09..b974254 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -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); diff --git a/tests/test-blobmsg-types.c b/tests/test-blobmsg-types.c index 4d77a27..5055b2d 100644 --- a/tests/test-blobmsg-types.c +++ b/tests/test-blobmsg-types.c @@ -139,13 +139,13 @@ static void dump_message(struct blob_buf *buf) if (tb[FOO_INT32_MIN]) fprintf(stderr, "int32_min: %" PRId32 "\n", (int32_t)blobmsg_get_u32(tb[FOO_INT32_MIN])); if (tb[FOO_INT16_MAX]) - fprintf(stderr, "int16_max: %" PRId16 "\n", (int16_t)blobmsg_get_u16(tb[FOO_INT16_MAX])); + fprintf(stderr, "int16_max: %" PRId16 "\n", (int16_t)__blobmsg_get_u16(tb[FOO_INT16_MAX])); if (tb[FOO_INT16_MIN]) - fprintf(stderr, "int16_min: %" PRId16 "\n", (int16_t)blobmsg_get_u16(tb[FOO_INT16_MIN])); + fprintf(stderr, "int16_min: %" PRId16 "\n", (int16_t)__blobmsg_get_u16(tb[FOO_INT16_MIN])); if (tb[FOO_INT8_MAX]) - fprintf(stderr, "int8_max: %" PRId8 "\n", (int8_t)blobmsg_get_u8(tb[FOO_INT8_MAX])); + fprintf(stderr, "int8_max: %" PRId8 "\n", (int8_t)__blobmsg_get_u8(tb[FOO_INT8_MAX])); if (tb[FOO_INT8_MIN]) - fprintf(stderr, "int8_min: %" PRId8 "\n", (int8_t)blobmsg_get_u8(tb[FOO_INT8_MIN])); + fprintf(stderr, "int8_min: %" PRId8 "\n", (int8_t)__blobmsg_get_u8(tb[FOO_INT8_MIN])); if (tb[FOO_DOUBLE_MAX]) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) @@ -242,10 +242,10 @@ static void dump_message_json(struct blob_buf *buf) fprintf(stderr, "int16_min: %" PRId32 "\n", blobmsg_get_u32(tb[FOO_INT16_MIN])); /* u8 is converted to boolean (true: all values != 0/false: value 0) in json */ if (tb[FOO_INT8_MAX]) - fprintf(stderr, "int8_max: %" PRId8 "\n", blobmsg_get_u8(tb[FOO_INT8_MAX])); + fprintf(stderr, "int8_max: %" PRId8 "\n", __blobmsg_get_u8(tb[FOO_INT8_MAX])); /* u8 is converted to boolean (true: all values != 0/false: value 0) in json */ if (tb[FOO_INT8_MIN]) - fprintf(stderr, "int8_min: %" PRId8 "\n", blobmsg_get_u8(tb[FOO_INT8_MIN])); + fprintf(stderr, "int8_min: %" PRId8 "\n", __blobmsg_get_u8(tb[FOO_INT8_MIN])); if (tb[FOO_DOUBLE_MAX]) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) @@ -324,10 +324,10 @@ fill_message(struct blob_buf *buf) blobmsg_add_u64(buf, "int64_min", INT64_MIN); blobmsg_add_u32(buf, "int32_max", INT32_MAX); blobmsg_add_u32(buf, "int32_min", INT32_MIN); - blobmsg_add_u16(buf, "int16_max", INT16_MAX); - blobmsg_add_u16(buf, "int16_min", INT16_MIN); - blobmsg_add_u8(buf, "int8_max", INT8_MAX); - blobmsg_add_u8(buf, "int8_min", INT8_MIN); + __blobmsg_add_u16(buf, "int16_max", INT16_MAX); + __blobmsg_add_u16(buf, "int16_min", INT16_MIN); + __blobmsg_add_u8(buf, "int8_max", INT8_MAX); + __blobmsg_add_u8(buf, "int8_min", INT8_MIN); blobmsg_add_double(buf, "double_max", DBL_MAX); blobmsg_add_double(buf, "double_min", DBL_MIN); } diff --git a/tests/test-blobmsg.c b/tests/test-blobmsg.c index 2224853..782faa8 100644 --- a/tests/test-blobmsg.c +++ b/tests/test-blobmsg.c @@ -41,10 +41,10 @@ static void dump_attr_data(struct blob_attr *data, int indent, int next_indent) indent_printf(indent, "%s (str)\n", blobmsg_get_string(data)); break; case BLOBMSG_TYPE_INT8: - indent_printf(indent, "%d (i8)\n", (int8_t) blobmsg_get_u8(data)); + indent_printf(indent, "%d (i8)\n", (int8_t) __blobmsg_get_u8(data)); break; case BLOBMSG_TYPE_INT16: - indent_printf(indent, "%d (i16)\n", (int16_t) blobmsg_get_u16(data)); + indent_printf(indent, "%d (i16)\n", (int16_t) __blobmsg_get_u16(data)); break; case BLOBMSG_TYPE_INT32: indent_printf(indent, "%d (i32)\n", (int32_t) blobmsg_get_u32(data)); @@ -121,12 +121,12 @@ fill_message(struct blob_buf *buf) tbl = blobmsg_open_table(buf, "testdata"); blobmsg_add_double(buf, "dbl-min", DBL_MIN); blobmsg_add_double(buf, "dbl-max", DBL_MAX); - blobmsg_add_u8(buf, "foo", 0); - blobmsg_add_u8(buf, "poo", 100); - blobmsg_add_u8(buf, "moo-min", INT8_MIN); - blobmsg_add_u8(buf, "moo-max", INT8_MAX); - blobmsg_add_u16(buf, "bar-min", INT16_MIN); - blobmsg_add_u16(buf, "bar-max", INT16_MAX); + __blobmsg_add_u8(buf, "foo", 0); + __blobmsg_add_u8(buf, "poo", 100); + __blobmsg_add_u8(buf, "moo-min", INT8_MIN); + __blobmsg_add_u8(buf, "moo-max", INT8_MAX); + __blobmsg_add_u16(buf, "bar-min", INT16_MIN); + __blobmsg_add_u16(buf, "bar-max", INT16_MAX); blobmsg_add_u32(buf, "baz-min", INT32_MIN); blobmsg_add_u32(buf, "baz-max", INT32_MAX); blobmsg_add_u64(buf, "taz-min", INT64_MIN); @@ -135,12 +135,12 @@ fill_message(struct blob_buf *buf) blobmsg_close_table(buf, tbl); tbl = blobmsg_open_array(buf, "list"); - blobmsg_add_u8(buf, NULL, 0); - blobmsg_add_u8(buf, NULL, 100); - blobmsg_add_u8(buf, NULL, INT8_MIN); - blobmsg_add_u8(buf, NULL, INT8_MAX); - blobmsg_add_u16(buf, NULL, INT16_MIN); - blobmsg_add_u16(buf, NULL, INT16_MAX); + __blobmsg_add_u8(buf, NULL, 0); + __blobmsg_add_u8(buf, NULL, 100); + __blobmsg_add_u8(buf, NULL, INT8_MIN); + __blobmsg_add_u8(buf, NULL, INT8_MAX); + __blobmsg_add_u16(buf, NULL, INT16_MIN); + __blobmsg_add_u16(buf, NULL, INT16_MAX); blobmsg_add_u32(buf, NULL, INT32_MIN); blobmsg_add_u32(buf, NULL, INT32_MAX); blobmsg_add_u64(buf, NULL, INT64_MIN); diff --git a/utils.h b/utils.h index dacac6e..5178826 100644 --- a/utils.h +++ b/utils.h @@ -219,6 +219,10 @@ int clock_gettime(int type, struct timespec *tv); #define __constructor __attribute__((constructor)) #endif +#ifndef __deprecated +#define __deprecated __attribute__((deprecated)) +#endif + #ifndef __destructor #define __destructor __attribute__((destructor)) #endif