moved some stuff from client implementation to libcoap

This commit is contained in:
Olaf Bergmann committed 2010-07-12 17:37:48 +02:00
1 parent 28a14dbb11
commit 96498de78e
3 files changed
+54 -2

No files matched your search

+24 -1
View File
@@ -1,6 +1,6 @@
/* encode.c -- encoding and decoding of CoAP data types
*
* (c) 2010 Carsten Bormann
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>, 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;
}
+15 -1
View File
@@ -1,6 +1,6 @@
/* encode.h -- encoding and decoding of CoAP data types
*
* (c) 2010 Carsten Bormann
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>, 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<<E<<ls)-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_ */
+15
View File
@@ -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 {