X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fc_common%2Focrandom%2Fsrc%2Focrandom.c;h=e0dcdc57778d7099b773907304f4d890e9e1e070;hb=9a19f4a70b1df5398a41ef7c6471d3c0d7121fa5;hp=ff0911218015fa09e1e3f736db94e10410c6df4c;hpb=3e08f0b76cfa2aebd58a6acd4d8a6ae64c5d3cf4;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/c_common/ocrandom/src/ocrandom.c b/resource/c_common/ocrandom/src/ocrandom.c index ff09112..e0dcdc5 100644 --- a/resource/c_common/ocrandom/src/ocrandom.c +++ b/resource/c_common/ocrandom/src/ocrandom.c @@ -30,23 +30,44 @@ #define _POSIX_C_SOURCE 200809L #endif -#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) -#include "fcntl.h" -#include "unistd.h" +#include "iotivity_config.h" + +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H #include +#endif +#ifdef HAVE_STRING_H +#include +#elif defined(HAVE_STRINGS_H) +#include +#endif +#ifdef HAVE_SYS_TIME_H #include +#endif +#ifdef HAVE_TIME_H #include +#endif #if defined(__ANDROID__) +#include #include #endif +#ifdef HAVE_WINDOWS_H +#include #endif #include "ocrandom.h" #include -#if !defined(__ANDROID__) && (defined(__linux__) || defined(__APPLE__)) +#ifdef HAVE_UUID_UUID_H #include #endif +#define NANO_SEC 1000000000 + #ifdef ARDUINO #include "Arduino.h" @@ -99,23 +120,28 @@ uint8_t GetRandomBit() int8_t OCSeedRandom() { -#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) || defined(__TIZEN__) +#ifndef ARDUINO // Get current time to Seed. uint64_t currentTime = 0; #ifdef __ANDROID__ struct timespec getTs; clock_gettime(CLOCK_MONOTONIC, &getTs); - currentTime = (getTs.tv_sec * (uint64_t)1000000000 + getTs.tv_nsec)/1000; + currentTime = (getTs.tv_sec * (uint64_t)NANO_SEC + getTs.tv_nsec)/1000; +#elif _WIN32 + LARGE_INTEGER count; + if (QueryPerformanceCounter(&count)) { + currentTime = count.QuadPart; + } #elif _POSIX_TIMERS > 0 struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); - currentTime = ts.tv_sec * 1000000 + ts.tv_nsec / 1000; + currentTime = (ts.tv_sec * (uint64_t)NANO_SEC + ts.tv_nsec)/ 1000; #else struct timeval tv; gettimeofday(&tv, NULL); - currentTime = tv.tv_sec * 1000000 + tv.tv_usec; + currentTime = tv.tv_sec * (uint64_t)1000000 + tv.tv_usec; #endif - +#if defined(__unix__) || defined(__APPLE__) || defined(__TIZENRT__) int32_t fd = open("/dev/urandom", O_RDONLY); if (fd >= 0) { @@ -135,6 +161,7 @@ int8_t OCSeedRandom() srand(randomSeed | currentTime); } else +#endif { // Do time based seed when problem in accessing "/dev/urandom" srand(currentTime); @@ -179,15 +206,11 @@ uint32_t OCGetRandom() uint8_t OCGetRandomByte(void) { -#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__) - return rand() & 0x00FF; -#elif defined ARDUINO #ifdef HAVE_SRANDOM return random() & 0x00FF; #else return rand() & 0x00FF; #endif -#endif } uint32_t OCGetRandomRange(uint32_t firstBound, uint32_t secondBound) @@ -268,7 +291,7 @@ OCRandomUuidResult OCGenerateUuid(uint8_t uuid[UUID_SIZE]) uuid[15] = parseUuidPart(&uuidString[34]); return RAND_UUID_OK; -#elif !defined(__ANDROID__) && (defined(__linux__) || defined(__APPLE__)) +#elif defined(HAVE_UUID_UUID_H) // note: uuid_t is typedefed as unsigned char[16] on linux/apple uuid_generate(uuid); return RAND_UUID_OK; @@ -310,10 +333,9 @@ OCRandomUuidResult OCGenerateUuidString(char uuidString[UUID_STRING_SIZE]) } else { - close(fd); return RAND_UUID_READ_ERROR; } -#elif !defined(__ANDROID__) && (defined(__linux__) || defined(__APPLE__)) +#elif defined(HAVE_UUID_UUID_H) uint8_t uuid[UUID_SIZE]; int8_t ret = OCGenerateUuid(uuid); @@ -357,3 +379,39 @@ OCRandomUuidResult OCConvertUuidToString(const uint8_t uuid[UUID_SIZE], return RAND_UUID_OK; } + +OCRandomUuidResult OCConvertStringToUuid(const char uuidString[UUID_STRING_SIZE], + uint8_t uuid[UUID_SIZE]) +{ + if(NULL == uuidString || NULL == uuid) + { + return RAND_UUID_INVALID_PARAM; + } + + size_t urnIdx = 0; + size_t uuidIdx = 0; + size_t strUuidLen = 0; + char convertedUuid[UUID_SIZE * 2] = {0}; + + strUuidLen = strlen(uuidString); + if((UUID_STRING_SIZE - 1) == strUuidLen) + { + for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_SIZE ; uuidIdx++, urnIdx+=2) + { + if(*(uuidString + urnIdx) == '-') + { + urnIdx++; + } + sscanf(uuidString + urnIdx, "%2hhx", &convertedUuid[uuidIdx]); + } + } + else + { + return RAND_UUID_CONVERT_ERROR; + } + + memcpy(uuid, convertedUuid, UUID_SIZE); + + return RAND_UUID_OK; +} +