summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorVinicius Costa Gomes <vinicius.gomes@intel.com>2016-09-14 11:23:57 -0300
committerAnas Nashif <nashif@linux.intel.com>2016-10-01 01:11:25 +0000
commit7079a1826745722bfdd43a62887aa65385e83369 (patch)
treeaf1c9519c4ae48f31416f42e6e3cfc4218e49220 /lib
parent441d22a371b08900840f4f71528149cc696321c7 (diff)
iot/zoap: Add helpers for dealing with integer options
A good number of option values represent numbers, so add these helpers so applications can avoid repeating this. Change-Id: Ied2a844c51808dcafe751a77492d00f2063de7d6 Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/iot/zoap/zoap.c65
-rw-r--r--lib/iot/zoap/zoap.h14
2 files changed, 63 insertions, 16 deletions
diff --git a/lib/iot/zoap/zoap.c b/lib/iot/zoap/zoap.c
index b7326fd63..313a24ec9 100644
--- a/lib/iot/zoap/zoap.c
+++ b/lib/iot/zoap/zoap.c
@@ -557,6 +557,28 @@ int zoap_handle_request(struct zoap_packet *pkt,
return -ENOENT;
}
+unsigned int zoap_option_value_to_int(const struct zoap_option *option)
+{
+ switch (option->len) {
+ case 0:
+ return 0;
+ case 1:
+ return option->value[0];
+ case 2:
+ return (option->value[0] << 0) | (option->value[1] << 8);
+ case 3:
+ return (option->value[0] << 0) | (option->value[1] << 8) |
+ (option->value[2] << 16);
+ case 4:
+ return (option->value[0] << 0) | (option->value[1] << 8) |
+ (option->value[2] << 16) | (option->value[3] << 24);
+ default:
+ return 0;
+ }
+
+ return 0;
+}
+
static int get_observe_option(const struct zoap_packet *pkt)
{
struct zoap_option option = {};
@@ -568,22 +590,7 @@ static int get_observe_option(const struct zoap_packet *pkt)
return -ENOENT;
}
- /* The value is in the network order, and has at max 3 bytes. */
- switch (option.len) {
- case 0:
- return 0;
- case 1:
- return option.value[0];
- case 2:
- return (option.value[0] << 0) | (option.value[1] << 8);
- case 3:
- return (option.value[0] << 0) | (option.value[1] << 8) |
- (option.value[2] << 16);
- default:
- return -EINVAL;
- }
-
- return -ENOENT;
+ return zoap_option_value_to_int(&option);
}
struct zoap_reply *zoap_response_received(
@@ -807,6 +814,32 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code,
return 0;
}
+int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code,
+ unsigned int val)
+{
+ uint8_t data[4], len;
+
+ if (val == 0) {
+ data[0] = 0;
+ len = 0;
+ } else if (val < 0xFF) {
+ data[0] = (uint8_t) val;
+ len = 1;
+ } else if (val < 0xFFFF) {
+ sys_put_be16(val, data);
+ len = 2;
+ } else if (val < 0xFFFFFF) {
+ sys_put_be16(val, data);
+ data[2] = val >> 16;
+ len = 3;
+ } else {
+ sys_put_be32(val, data);
+ len = 4;
+ }
+
+ return zoap_add_option(pkt, code, data, len);
+}
+
int zoap_find_options(const struct zoap_packet *pkt, uint16_t code,
struct zoap_option *options, uint16_t veclen)
{
diff --git a/lib/iot/zoap/zoap.h b/lib/iot/zoap/zoap.h
index d1e45c898..6f0b32e77 100644
--- a/lib/iot/zoap/zoap.h
+++ b/lib/iot/zoap/zoap.h
@@ -383,6 +383,20 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code,
const void *value, uint16_t len);
/**
+ * Converts an option to its integer representation. It assumes that
+ * the number is encoded in the network byte order in the option.
+ */
+unsigned int zoap_option_value_to_int(const struct zoap_option *option);
+
+/**
+ * Adds an integer value option to the packet. The option must be
+ * added in numeric order of their codes, and the least amount of
+ * bytes will be used to encode the value.
+ */
+int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code,
+ unsigned int val);
+
+/**
* Return the values associated with the option of value @a code.
*/
int zoap_find_options(const struct zoap_packet *pkt, uint16_t code,