libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
prng.h
Go to the documentation of this file.
1 /* prng.h -- Pseudo Random Numbers
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
14 #ifndef _COAP_PRNG_H_
15 #define _COAP_PRNG_H_
16 
17 #include "config.h"
18 
24 #ifndef WITH_CONTIKI
25 #include <stdlib.h>
26 
32 static inline int
33 coap_prng_impl(unsigned char *buf, size_t len) {
34  while (len--)
35  *buf++ = rand() & 0xFF;
36  return 1;
37 }
38 #else /* WITH_CONTIKI */
39 #include <string.h>
40 
46 static inline int
47 contiki_prng_impl(unsigned char *buf, size_t len) {
48  unsigned short v = random_rand();
49  while (len > sizeof(v)) {
50  memcpy(buf, &v, sizeof(v));
51  len -= sizeof(v);
52  buf += sizeof(v);
53  }
54 
55  memcpy(buf, &v, len);
56  return 1;
57 }
58 
59 #define prng(Buf,Length) contiki_prng_impl((Buf), (Length))
60 #define prng_init(Value) random_init((unsigned short)(Value))
61 #endif /* WITH_CONTIKI */
62 
63 #ifndef prng
64 
69 #define prng(Buf,Length) coap_prng_impl((Buf), (Length))
70 #endif
71 
72 #ifndef prng_init
73 
79 #define prng_init(Value) srand((unsigned long)(Value))
80 #endif
81 
84 #endif /* _COAP_PRNG_H_ */