Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[platform/upstream/iotivity.git] / resource / csdk / ocrandom / src / ocrandom.c
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 #if defined(__ANDROID__) || defined(__linux__)
23 #include "fcntl.h"
24 #include "unistd.h"
25 #endif
26 #include "ocrandom.h"
27
28 #ifdef ARDUINO
29 #include "Arduino.h"
30
31 uint8_t GetRandomBitRaw() {
32     return analogRead((uint8_t)ANALOG_IN) & 0x1;
33 }
34
35 uint8_t GetRandomBitRaw2() {
36     int a = 0;
37     for(;;) {
38         a = GetRandomBitRaw() | (GetRandomBitRaw()<<1);
39         if (a==1){
40             return 0; // 1 to 0 transition: log a zero bit
41         }
42         if (a==2){
43             return 1;// 0 to 1 transition: log a one bit
44         }
45         // For other cases, try again.
46     }
47 }
48
49 uint8_t GetRandomBit() {
50     int a = 0;
51     for(;;) {
52         a = GetRandomBitRaw2() | (GetRandomBitRaw2()<<1);
53         if (a==1){
54             return 0; // 1 to 0 transition: log a zero bit
55         }
56         if (a==2){
57             return 1;// 0 to 1 transition: log a one bit
58         }
59         // For other cases, try again.
60     }
61 }
62 #endif
63
64 int8_t OCSeedRandom() {
65 #if defined(__ANDROID__) || defined(__linux__)
66     int32_t fd = open("/dev/urandom", O_RDONLY);
67     if (fd > 0) {
68         uint32_t randomSeed;
69         uint32_t totalRead = 0; //how many integers were read
70         int32_t currentRead = 0;
71         while (totalRead < sizeof(randomSeed)) {
72             currentRead = read(fd, (uint8_t*) &randomSeed + totalRead,
73                     sizeof(randomSeed) - totalRead);
74             if(currentRead > 0){
75                 totalRead += currentRead;
76             }
77         }
78         close(fd);
79         srand(randomSeed);
80         return 0;
81     }
82     close(fd);
83     return -1;
84 #elif defined ARDUINO
85     uint32_t result =0;
86     uint8_t i;
87     for (i=32; i--;){
88         result += result + GetRandomBit();
89     }
90     randomSeed(result);
91     return 0;
92 #endif
93
94 }
95
96 void OCFillRandomMem(uint8_t * location, uint16_t len) {
97     if(!location){
98         return;
99     }
100     for (; len--;){
101         *location++ = OCGetRandomByte();
102     }
103 }
104
105 uint32_t OCGetRandom() {
106     uint32_t result = 0;
107     OCFillRandomMem((uint8_t*) &result, 4);
108     return result;
109 }
110
111 uint8_t OCGetRandomByte(void) {
112 #if defined(__ANDROID__) || defined(__linux__)
113     return rand() & 0x00FF;
114 #elif defined ARDUINO
115     return random(256) & 0x00FF;
116 #endif
117 }
118
119 uint32_t OCGetRandomRange(uint32_t firstBound, uint32_t secondBound){
120     uint32_t base;
121     uint32_t diff;
122     uint32_t result;
123     if(firstBound > secondBound){
124         base = secondBound;
125         diff = firstBound - secondBound;
126     }else if(firstBound < secondBound){
127         base = firstBound;
128         diff = secondBound - firstBound;
129     }else{
130         return secondBound;
131     }
132     result = ((float)OCGetRandom()/((float)(0xFFFFFFFF))*(float)diff) + (float) base;
133     return result;
134 }