iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / ocrandom / include / ocrandom.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #ifndef _RANDOM_H
23 #define _RANDOM_H
24
25 #include <stdint.h>
26 #include <stdlib.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 #if defined(__ANDROID__) || defined(__linux__)
33 #include <time.h>
34 #elif defined ARDUINO
35 // MEGA has 16 input pins whereas Due has only 12 input pins
36 #define ANALOG_IN (10)
37 #endif
38
39 /**
40  * Seed the random number generator. Seeding depends on platform.
41  * Android and Linux uses current time. Arduino uses Analog reading on pin ANALOG_IN
42  * @retval 0 for Success, otherwise some error value
43  */
44 int8_t OCSeedRandom();
45
46 /**
47  * Generate a uniformly [0,2^32] distributed random number
48  * @retval On Success, it returns the random value.
49  */
50 uint32_t OCGetRandom();
51
52 /**
53  * Generate a uniformly [0,2^8] distributed random number
54  * @retval On Success, it returns the random value, otherwise -1 for error.
55  */
56 uint8_t OCGetRandomByte(void);
57
58 /**
59  * Generate a uniformly distributed 8-bit (byte) array random numbers
60  * @param[out] location
61  *              memory location to start filling with random bytes
62  * @param[in] len
63  *              length of array to be filled with random bytes
64  */
65 void OCFillRandomMem(uint8_t * location, uint16_t len);
66
67 /*
68  * Generate a uniformly distributed number on the defined bounded range
69  * @param[in] firstBound
70  *              the first bound of the range
71  * @param[in] secondBound
72  *              the second bound of the range
73  */
74 uint32_t OCGetRandomRange(uint32_t firstBound, uint32_t secondBound);
75
76 #ifdef __cplusplus
77 }
78 #endif
79
80 #endif //_RANDOM_H