mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-30 10:17:08 +03:00
helper functions for pseudo-FP encoding according to draft-bormann-coap-misc-04
This commit is contained in:
1 parent
07ca868a95
commit
66a138491d
3 files changed
+58
-2
No files matched your search
@@ -1,9 +1,9 @@
|
||||
PROGRAM:=coap
|
||||
VERSION:=0.01
|
||||
|
||||
SOURCES:= pdu.c net.c
|
||||
SOURCES:= pdu.c net.c encode.c
|
||||
OBJECTS:= $(patsubst %.c, %.o, $(SOURCES))
|
||||
HEADERS:=coap.h debug.h pdu.h net.h
|
||||
HEADERS:=coap.h debug.h pdu.h net.h encode.h
|
||||
CFLAGS:=-g -Wall -std=c99 -pedantic
|
||||
DISTDIR=$(PROGRAM)-$(VERSION)
|
||||
FILES:=main.c Makefile $(SOURCES) $(HEADERS)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/* encode.c -- encoding and decoding of CoAP data types
|
||||
*
|
||||
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
|
||||
*/
|
||||
|
||||
#ifndef NDEBUG
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
|
||||
#include "encode.h"
|
||||
|
||||
unsigned int
|
||||
coap_pseudo_encode( unsigned int val ) {
|
||||
unsigned int e = 0;
|
||||
|
||||
if ( val < HIBIT )
|
||||
return val;
|
||||
|
||||
if ( val > MAX_VALUE ) {
|
||||
#ifndef NDEBUG
|
||||
fprintf(stderr, "cannot encode value %u\n", val);
|
||||
#endif
|
||||
return ( 1 << N ) - 1;
|
||||
}
|
||||
|
||||
while ( HIBIT < (val & ~0xff) ) {
|
||||
val = val >> 1;
|
||||
e += 1;
|
||||
}
|
||||
|
||||
return (val & ~EMASK) | e;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/* encode.h -- encoding and decoding of CoAP data types
|
||||
*
|
||||
* (c) 2010 Olaf Bergmann <bergmann@tzi.org>
|
||||
*/
|
||||
|
||||
#ifndef _COAP_ENCODE_H_
|
||||
#define _COAP_ENCODE_H_
|
||||
|
||||
#include "encode.h"
|
||||
|
||||
#define N 8
|
||||
#define E 4
|
||||
#define HIBIT (1 << (N - 1))
|
||||
#define EMASK ((1 << E) - 1)
|
||||
#define MMASK ((1 << N) - 1 - EMASK)
|
||||
#define MAX_VALUE ( (1 << N) - (1 << E) ) * (1 << ((1 << E) - 1))
|
||||
|
||||
/* internal function, better use macro COAP_PSEUDOFP_ENCODE() instead */
|
||||
unsigned int coap_pseudo_encode( unsigned int val );
|
||||
|
||||
#define COAP_PSEUDOFP_ENCODE(r) coap_pseudo_encode( (r) )
|
||||
#define COAP_PSEUDOFP_DECODE(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
|
||||
|
||||
#endif /* _COAP_ENCODE_H_ */
|
||||
Reference in new issue
Block a user