1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1b, Real-time extensions
24 // (IEEE Std 1003.1b-1993) specification
26 // For this specific file, see use of clock_gettime,
27 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
28 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
29 #ifndef _POSIX_C_SOURCE
30 #define _POSIX_C_SOURCE 200809L
33 #if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)
39 #if defined(__ANDROID__)
41 #include <linux/time.h>
47 #if !defined(__ANDROID__) && (defined(__linux__) || defined(__APPLE__))
48 #include <uuid/uuid.h>
51 #if !defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) || defined(__TIZEN__)
52 #define NANO_SEC 1000000000
58 // ARM GCC compiler doesnt define srandom function.
59 #if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
60 #define HAVE_SRANDOM 1
63 uint8_t GetRandomBitRaw()
65 return analogRead((uint8_t)ANALOG_IN) & 0x1;
68 uint8_t GetRandomBitRaw2()
73 a = GetRandomBitRaw() | (GetRandomBitRaw()<<1);
76 return 0; // 1 to 0 transition: log a zero bit
80 return 1;// 0 to 1 transition: log a one bit
82 // For other cases, try again.
86 uint8_t GetRandomBit()
91 a = GetRandomBitRaw2() | (GetRandomBitRaw2()<<1);
94 return 0; // 1 to 0 transition: log a zero bit
98 return 1;// 0 to 1 transition: log a one bit
100 // For other cases, try again.
105 int8_t OCSeedRandom()
108 // Get current time to Seed.
109 uint64_t currentTime = 0;
111 struct timespec getTs;
112 clock_gettime(CLOCK_MONOTONIC, &getTs);
113 currentTime = (getTs.tv_sec * (uint64_t)NANO_SEC + getTs.tv_nsec)/1000;
114 #elif _POSIX_TIMERS > 0
116 clock_gettime(CLOCK_MONOTONIC, &ts);
117 currentTime = (ts.tv_sec * (uint64_t)NANO_SEC + ts.tv_nsec)/ 1000;
120 gettimeofday(&tv, NULL);
121 currentTime = tv.tv_sec * (uint64_t)1000000 + tv.tv_usec;
123 #if defined(__unix__) || defined(__APPLE__)
124 int32_t fd = open("/dev/urandom", O_RDONLY);
127 uint32_t randomSeed = 0;
128 uint32_t totalRead = 0; //how many integers were read
129 int32_t currentRead = 0;
130 while (totalRead < sizeof(randomSeed))
132 currentRead = read(fd, (uint8_t*) &randomSeed + totalRead,
133 sizeof(randomSeed) - totalRead);
136 totalRead += currentRead;
140 srand(randomSeed | currentTime);
145 // Do time based seed when problem in accessing "/dev/urandom"
150 #elif defined ARDUINO
155 result += result + GetRandomBit();
167 void OCFillRandomMem(uint8_t * location, uint16_t len)
175 *location++ = OCGetRandomByte();
179 uint32_t OCGetRandom()
182 OCFillRandomMem((uint8_t*) &result, 4);
186 uint8_t OCGetRandomByte(void)
188 #if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) || defined(__msys_nt__)
189 return rand() & 0x00FF;
190 #elif defined ARDUINO
192 return random() & 0x00FF;
194 return rand() & 0x00FF;
199 uint32_t OCGetRandomRange(uint32_t firstBound, uint32_t secondBound)
204 if (firstBound > secondBound)
207 diff = firstBound - secondBound;
209 else if (firstBound < secondBound)
212 diff = secondBound - firstBound;
218 result = ((float)OCGetRandom()/((float)(0xFFFFFFFF))*(float)diff) + (float) base;
222 #if defined(__ANDROID__)
223 uint8_t parseUuidChar(char c)
234 uint8_t parseUuidPart(const char *c)
236 return (parseUuidChar(c[0])<<4) + parseUuidChar(c[1]);
240 OCRandomUuidResult OCGenerateUuid(uint8_t uuid[UUID_SIZE])
244 return RAND_UUID_INVALID_PARAM;
246 #if defined(__ANDROID__)
247 char uuidString[UUID_STRING_SIZE];
248 int8_t ret = OCGenerateUuidString(uuidString);
255 uuid[ 0] = parseUuidPart(&uuidString[0]);
256 uuid[ 1] = parseUuidPart(&uuidString[2]);
257 uuid[ 2] = parseUuidPart(&uuidString[4]);
258 uuid[ 3] = parseUuidPart(&uuidString[6]);
260 uuid[ 4] = parseUuidPart(&uuidString[9]);
261 uuid[ 5] = parseUuidPart(&uuidString[11]);
263 uuid[ 6] = parseUuidPart(&uuidString[14]);
264 uuid[ 7] = parseUuidPart(&uuidString[16]);
266 uuid[ 8] = parseUuidPart(&uuidString[19]);
267 uuid[ 9] = parseUuidPart(&uuidString[21]);
269 uuid[10] = parseUuidPart(&uuidString[24]);
270 uuid[11] = parseUuidPart(&uuidString[26]);
271 uuid[12] = parseUuidPart(&uuidString[28]);
272 uuid[13] = parseUuidPart(&uuidString[30]);
273 uuid[14] = parseUuidPart(&uuidString[32]);
274 uuid[15] = parseUuidPart(&uuidString[34]);
277 #elif (!defined(__ANDROID__) && !defined(__msys_nt__)) && (defined(__linux__) || defined(__APPLE__))
278 // note: uuid_t is typedefed as unsigned char[16] on linux/apple
282 // Fallback for all platforms is filling the array with random data
283 OCFillRandomMem(uuid, UUID_SIZE);
288 OCRandomUuidResult OCGenerateUuidString(char uuidString[UUID_STRING_SIZE])
292 return RAND_UUID_INVALID_PARAM;
294 #if defined(__ANDROID__)
295 int32_t fd = open("/proc/sys/kernel/random/uuid", O_RDONLY);
298 ssize_t readResult = read(fd, uuidString, UUID_STRING_SIZE - 1);
302 return RAND_UUID_READ_ERROR;
304 else if (readResult < UUID_STRING_SIZE - 1)
306 uuidString[0] = '\0';
307 return RAND_UUID_READ_ERROR;
310 uuidString[UUID_STRING_SIZE - 1] = '\0';
311 for (char* p = uuidString; *p; ++p)
320 return RAND_UUID_READ_ERROR;
322 #elif !defined(__ANDROID__) && (defined(__linux__) || defined(__APPLE__))
323 uint8_t uuid[UUID_SIZE];
324 int8_t ret = OCGenerateUuid(uuid);
331 uuid_unparse_lower(uuid, uuidString);
335 uint8_t uuid[UUID_SIZE];
336 OCGenerateUuid(uuid);
338 return OCConvertUuidToString(uuid, uuidString);
342 OCRandomUuidResult OCConvertUuidToString(const uint8_t uuid[UUID_SIZE],
343 char uuidString[UUID_STRING_SIZE])
345 if (uuid == NULL || uuidString == NULL)
347 return RAND_UUID_INVALID_PARAM;
351 int ret = snprintf(uuidString, UUID_STRING_SIZE,
352 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
353 uuid[0], uuid[1], uuid[2], uuid[3],
354 uuid[4], uuid[5], uuid[6], uuid[7],
355 uuid[8], uuid[9], uuid[10], uuid[11],
356 uuid[12], uuid[13], uuid[14], uuid[15]
359 if (ret != UUID_STRING_SIZE - 1)
361 return RAND_UUID_CONVERT_ERROR;