diff --git a/encode.c b/encode.c index dc394a4d..8afd6035 100644 --- a/encode.c +++ b/encode.c @@ -1,6 +1,6 @@ /* encode.c -- encoding and decoding of CoAP data types * - * (c) 2010 Carsten Bormann + * (c) 2010 Olaf Bergmann , Carsten Bormann */ #ifndef NDEBUG @@ -17,3 +17,26 @@ int coap_fls(unsigned int i) { return n; } +unsigned int +coap_decode_var_bytes(unsigned char *buf,unsigned int len) { + unsigned int i, n = 0; + for (i = 0; i < len; ++i) + n = (n << 8) + buf[i]; + + return n; +} + +unsigned int +coap_encode_var_bytes(unsigned char *buf, unsigned int val) { + int n, i = val >> 1; + for (n = 0; i; n++) /* FIXME: coap_fls() */ + i >>= 1; + + for (i = n / 8 + 1; i; --i) { + buf[i-1] = val & 0xff; + val >>= 8; + } + + return n / 8 + 1; +} + diff --git a/encode.h b/encode.h index d5814451..d790c312 100644 --- a/encode.h +++ b/encode.h @@ -1,6 +1,6 @@ /* encode.h -- encoding and decoding of CoAP data types * - * (c) 2010 Carsten Bormann + * (c) 2010 Olaf Bergmann , Carsten Bormann */ #ifndef _COAP_ENCODE_H_ @@ -32,4 +32,18 @@ extern int coap_fls(unsigned int i); #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - N, (v >> ls) & MMASK) + ls) #define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - N, (s = (((v + ((1<> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls)) +/** + * Decodes multiple-length byte sequences. buf points to an input byte + * sequence of length len. Returns the decoded value. + */ +unsigned int coap_decode_var_bytes(unsigned char *buf,unsigned int len); + +/** + * Encodes multiple-length byte sequences. buf points to an output + * buffer of sufficient length to store the encoded bytes. val is + * the value to encode. Returns the number of bytes used to encode + * val or 0 on error. + */ +unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val); + #endif /* _COAP_ENCODE_H_ */ diff --git a/pdu.h b/pdu.h index 5121bfd5..2b08bd2f 100644 --- a/pdu.h +++ b/pdu.h @@ -181,6 +181,21 @@ typedef union { /* do not forget to adjust this when coap_opt_t is changed! */ #define COAP_OPT_SIZE(opt) ( COAP_OPT_LENGTH(opt) + ( COAP_OPT_ISEXTENDED(opt) ? 2: 1 ) ) +/** + * Structures for more convenient handling of options. (To be used with ordered + * coap_list_t.) The option's data will be added to the end of the coap_option + * structure (see macro COAP_OPTION_DATA). + */ +typedef struct { + unsigned short key; /* the option key (no delta coding) */ + unsigned int length; +} coap_option; + +#define COAP_OPTION_KEY(option) (option).key +#define COAP_OPTION_LENGTH(option) (option).length +#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option)) + + /** Header structure for CoAP PDUs */ typedef struct {