Placed OC random functionality at common place for RI and CA to use 01/4101/12
authorkoushik.girijala <g.koushik@samsung.com>
Tue, 10 Nov 2015 07:50:08 +0000 (13:20 +0530)
committerJon A. Cruz <jonc@osg.samsung.com>
Tue, 17 Nov 2015 18:56:33 +0000 (18:56 +0000)
Moved OC random implementation to c_common folder to make it available for CA as well. As Seed Random should be called at common place for RI and CA.

Change-Id: I0a7cd674b9c4212b3a1d1e382aa1f455bec1a6d2
Signed-off-by: koushik.girijala <g.koushik@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4101
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
29 files changed:
examples/OICMiddle/SConscript
examples/OICSensorBoard/Makefile
resource/c_common/SConscript
resource/c_common/ocrandom/include/ocrandom.h [moved from resource/csdk/ocrandom/include/ocrandom.h with 100% similarity]
resource/c_common/ocrandom/src/ocrandom.c [moved from resource/csdk/ocrandom/src/ocrandom.c with 99% similarity]
resource/c_common/ocrandom/test/SConscript [moved from resource/csdk/ocrandom/test/SConscript with 97% similarity]
resource/c_common/ocrandom/test/android/randomtest.cpp [moved from resource/csdk/ocrandom/test/android/randomtest.cpp with 100% similarity]
resource/c_common/ocrandom/test/arduino/randomtest.cpp [moved from resource/csdk/ocrandom/test/arduino/randomtest.cpp with 100% similarity]
resource/c_common/ocrandom/test/linux/randomtest.cpp [moved from resource/csdk/ocrandom/test/linux/randomtest.cpp with 100% similarity]
resource/csdk/SConscript
resource/csdk/connectivity/build/tizen/packaging/com.oic.ca.spec
resource/csdk/connectivity/src/caconnectivitymanager.c
resource/csdk/connectivity/src/caprotocolmessage.c
resource/csdk/doc/Doxyfile
resource/csdk/routing/SConscript
resource/csdk/security/SConscript
resource/csdk/security/provisioning/SConscript
resource/csdk/security/provisioning/ck_manager/sample/SConscript
resource/csdk/stack/src/ocstack.c
resource/csdk/stack/test/SConscript
resource/examples/SConscript
resource/src/SConscript
resource/unit_tests.scons
resource/unittests/SConscript
service/simulator/SConscript
service/third_party_libs.scons
tools/darwin/mkfwk_ios.sh
tools/darwin/mkfwk_osx.sh
tools/tizen/iotivity.spec

index 6748493..8c1152d 100644 (file)
@@ -34,7 +34,7 @@ examples_env = lib_env.Clone()
 examples_env.AppendUnique(CPPPATH = [
                '../../resource/include/',
                '../../resource/csdk/stack/include',
-               '../../resource/csdk/ocrandom/include',
+               '../../resource/c_common/ocrandom/include',
                '../../resource/csdk/logger/include',
                '../../resource/oc_logger/include'
                ])
index 8a6939b..ab12e21 100644 (file)
@@ -7,7 +7,7 @@ YOCTOCXXFLAGS=-I$(SDKTARGETSYSROOT)/usr/include/iotivity/resource/ -I$(SDKTARGET
 
 YOCTOLDFLAGS=-loc -loctbstack -loc_logger -lmraa
 
-CXXFLAGS=-I../../resource/include -I../../resource/csdk/stack/include -I../../resource/csdk/ocrandom/include -I../../resource/csdk/logger/include -I../../resource/oc_logger/include
+CXXFLAGS=-I../../resource/include -I../../resource/csdk/stack/include -I../../resource/c_common/ocrandom/include -I../../resource/csdk/logger/include -I../../resource/oc_logger/include
 
 LDFLAGS=-L../../out/linux/$(CLIENTARCH)/$(BUILDTYPE) -loc -loctbstack -loc_logger
 
index bdbf06b..2136b4f 100644 (file)
@@ -28,7 +28,8 @@ env.AppendUnique(CPPPATH = [
             os.path.join(Dir('.').abspath),
             os.path.join(Dir('.').abspath, 'oic_malloc/include'),
             os.path.join(Dir('.').abspath, 'oic_string/include'),
-            os.path.join(Dir('.').abspath, 'oic_time/include')
+            os.path.join(Dir('.').abspath, 'oic_time/include'),
+            os.path.join(Dir('.').abspath, 'ocrandom/include')
         ])
 
 if target_os == 'tizen':
@@ -36,7 +37,10 @@ if target_os == 'tizen':
 else:
        env.AppendUnique(LIBPATH = [os.path.join(env.get('BUILD_DIR'), 'resource/c_common')])
 
-env.AppendUnique(LIBS = ['c_common'])
+if target_os in ['tizen', 'linux']:
+       env.ParseConfig("pkg-config --cflags --libs uuid")
+
+env.PrependUnique(LIBS = ['c_common'])
 
 common_env = env.Clone()
 
@@ -48,10 +52,11 @@ common_env = env.Clone()
 # Source files and Targets
 ######################################################################
 common_src = [
-    'oic_string/src/oic_string.c',
-    'oic_malloc/src/oic_malloc.c',
-    'oic_time/src/oic_time.c'
-    ]
+       'oic_string/src/oic_string.c',
+       'oic_malloc/src/oic_malloc.c',
+       'oic_time/src/oic_time.c',
+       'ocrandom/src/ocrandom.c',
+       ]
 
 commonlib = common_env.StaticLibrary('c_common', common_src)
 common_env.InstallTarget(commonlib, 'c_common')
similarity index 99%
rename from resource/csdk/ocrandom/src/ocrandom.c
rename to resource/c_common/ocrandom/src/ocrandom.c
index debbee1..78d2bc9 100644 (file)
@@ -143,7 +143,7 @@ int8_t OCSeedRandom()
     {
         result += result + GetRandomBit();
     }
-    randomSeed(result);
+    srandom(result);
     return 0;
 #endif
 
@@ -173,7 +173,7 @@ uint8_t OCGetRandomByte(void)
 #if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
     return rand() & 0x00FF;
 #elif defined ARDUINO
-    return random(256) & 0x00FF;
+    return random() & 0x00FF;
 #endif
 }
 
similarity index 97%
rename from resource/csdk/ocrandom/test/SConscript
rename to resource/c_common/ocrandom/test/SConscript
index 2e7bc52..86e08a1 100644 (file)
@@ -63,4 +63,4 @@ if env.get('TEST') == '1':
                 from tools.scons.RunTest import *
                 run_test(randomtest_env,
                          'resource_csdk_random_test.memcheck',
-                         'resource/csdk/ocrandom/test/randomtests')
+                         'resource/c_common/ocrandom/test/randomtests')
index 52c075d..a8cc76e 100644 (file)
@@ -44,7 +44,6 @@ liboctbstack_env.PrependUnique(CPPPATH = [
                '../../extlibs/cjson/',
                '../../extlibs/timer/',
                'logger/include',
-               'ocrandom/include',
                'stack/include',
                'stack/include/internal',
                '../oc_logger/include',
@@ -83,8 +82,8 @@ if target_os in ['android', 'linux', 'tizen']:
 
 liboctbstack_env.AppendUnique(LIBS = ['coap', 'm'])
 
-if target_os == 'tizen':
-    liboctbstack_env.ParseConfig("pkg-config --cflags --libs uuid")
+if target_os in ['tizen', 'linux']:
+       liboctbstack_env.ParseConfig("pkg-config --cflags --libs uuid")
 
 if target_os not in ['android', 'arduino', 'windows', 'winrt']:
        liboctbstack_env.AppendUnique(LIBS = ['pthread'])
@@ -137,7 +136,6 @@ liboctbstack_src = [
        OCTBSTACK_SRC + 'occollection.c',
        OCTBSTACK_SRC + 'oicgroup.c',
        'logger/src/logger.c',
-       'ocrandom/src/ocrandom.c',
        OCTBSTACK_SRC + "rdpayload.c"
        ]
 
index 1602200..acb41b7 100644 (file)
@@ -20,6 +20,7 @@ BuildRequires: boost-thread
 BuildRequires: boost-system
 BuildRequires: boost-filesystem
 BuildRequires: scons
+BuildRequires: pkgconfig(uuid)
 
 
 %description
index 9f6aef8..a71b849 100644 (file)
@@ -63,6 +63,11 @@ CAResult_t CAInitialize()
 
     if (!g_isInitialized)
     {
+        if (0 != OCSeedRandom())
+        {
+            OIC_LOG(ERROR, TAG, "Seed Random Failed");
+        }
+
         CAResult_t res = CAInitializeMessageHandler();
         if (res != CA_STATUS_OK)
         {
@@ -71,6 +76,7 @@ CAResult_t CAInitialize()
         }
         g_isInitialized = true;
     }
+
     return CA_STATUS_OK;
 }
 
index d5f9a3e..1a4e2b0 100644 (file)
@@ -24,7 +24,6 @@
 // For glibc information on feature test macros,
 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
 //
-// This file requires #define use due to random() and srandom()
 // For details on compatibility and glibc support,
 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
 #define _DEFAULT_SOURCE
 #include "logger.h"
 #include "oic_malloc.h"
 #include "oic_string.h"
-
-// ARM GCC compiler doesnt define srandom function.
-#if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
-#define HAVE_SRANDOM 1
-#endif
+#include "ocrandom.h"
 
 #define TAG "CA_PRTCL_MSG"
 
@@ -65,8 +60,6 @@
 
 static const char COAP_URI_HEADER[] = "coap://[::]/";
 
-static unsigned int SEED = 0;
-
 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                                    CARequestInfo_t *outReqInfo)
 {
@@ -1041,26 +1034,6 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
         return CA_STATUS_INVALID_PARAM;
     }
 
-    if (SEED == 0)
-    {
-#ifdef ARDUINO
-        SEED = now();
-#else
-        SEED = time(NULL);
-#endif
-        if (SEED == (unsigned int)((time_t)-1))
-        {
-            OIC_LOG(ERROR, TAG, "seed is not made");
-            SEED = 0;
-            return CA_STATUS_FAILED;
-        }
-#if HAVE_SRANDOM
-        srandom(SEED);
-#else
-        srand(SEED);
-#endif
-    }
-
     // memory allocation
     char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
     if (NULL == temp)
@@ -1069,16 +1042,7 @@ CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
         return CA_MEMORY_ALLOC_FAILED;
     }
 
-    // set random byte
-    for (uint8_t index = 0; index < tokenLength; index++)
-    {
-        // use valid characters
-#ifdef ARDUINO
-        temp[index] = rand() & 0x00FF;
-#else
-        temp[index] = random() & 0x00FF;
-#endif
-    }
+    OCFillRandomMem((uint8_t *)temp, tokenLength);
 
     // save token
     *token = temp;
index a95101f..5b6b38b 100644 (file)
@@ -662,7 +662,7 @@ WARN_LOGFILE           =
 INPUT                  = . \
                                                 ../stack/include \
                                                 ../ocsocket/include \
-                                                ../ocrandom/include \
+                                                ../../c_common/ocrandom/include \
                                                 ../occoap/include \
 
 # This tag can be used to specify the character encoding of the source files
index d3d68ba..1d2a067 100644 (file)
@@ -18,7 +18,7 @@ if env.get('ROUTING') == 'GW':
                                os.path.join(Dir('.').abspath, './../stack/include/internal'),
                                os.path.join(Dir('.').abspath, './../logger'),
                                os.path.join(Dir('.').abspath, './../../oc_logger/include'),
-                               os.path.join(Dir('.').abspath, './../ocrandom/include'),
+                               os.path.join(Dir('.').abspath, './../../c_common/ocrandom/include'),
                                os.path.join(Dir('.').abspath, './../connectivity/api'),
                                os.path.join(Dir('.').abspath, './../connectivity/common/inc'),
                                os.path.join(Dir('.').abspath, './../security/include'),
index 6d83044..4dcb4c2 100755 (executable)
@@ -42,7 +42,7 @@ libocsrm_env.PrependUnique(CPPPATH = [
                '../../../extlibs/cjson/',
                '../../../extlibs/tinydtls/',
                '../logger/include',
-               '../ocrandom/include',
+               '../../c_common/ocrandom/include',
                '../stack/include',
                '../stack/include/internal',
                '../../oc_logger/include',
index 65d7855..06cd5bb 100644 (file)
@@ -30,7 +30,7 @@ root_dir = './../../../../'
 provisioning_env.AppendUnique(CPPPATH = [
                '../../stack/include',
                '../../stack/include/internal',
-               '../../ocrandom/include',
+               '../../../c_common/ocrandom/include',
                '../../logger/include',
                '../../../oc_logger/include',
                '../../ocmalloc/include',
index f1d251c..b368fe3 100644 (file)
@@ -28,7 +28,7 @@ provisioning_env = env.Clone()
 ######################################################################
 provisioning_env.AppendUnique(CPPPATH = [
                '../../../../stack/include',
-               '../../../../ocrandom/include',
+               '../../../../../c_common/ocrandom/include',
                '../../../../logger/include',
                '../../../../security/include',
                '../../../../../oc_logger/include',
index 3224ace..f977f09 100755 (executable)
@@ -1880,7 +1880,6 @@ OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlag
 
     defaultDeviceHandler = NULL;
     defaultDeviceHandlerCallbackParameter = NULL;
-    OCSeedRandom();
 
     result = CAResultToOCResult(CAInitialize());
     VERIFY_SUCCESS(result, OC_STACK_OK);
index 6654610..507f011 100644 (file)
@@ -32,7 +32,7 @@ stacktest_env.PrependUnique(CPPPATH = [
                '../../security/include',
                '../../ocsocket/include',
                '../../logger/include',
-               '../../ocrandom/include',
+               '../../../c_common/ocrandom/include',
                '../../stack/include',
                '../../stack/include/internal',
                '../../connectivity/api',
index 731b203..ffb284a 100644 (file)
@@ -34,7 +34,7 @@ examples_env = lib_env.Clone()
 examples_env.AppendUnique(CPPPATH = [
                '../include/',
                '../csdk/stack/include',
-               '../csdk/ocrandom/include',
+               '../c_common/ocrandom/include',
                '../csdk/logger/include',
                '../oc_logger/include'
                ])
index df2f209..b3d92ca 100644 (file)
@@ -34,7 +34,7 @@ oclib_env = lib_env.Clone()
 oclib_env.AppendUnique(CPPPATH = [
                '../include/',
                '../csdk/stack/include',
-               '../csdk/ocrandom/include',
+               '../c_common/ocrandom/include',
                '../csdk/logger/include',
                '../oc_logger/include',
                '../csdk/connectivity/lib/libcoap-4.1.1'
index 06e50c1..08a9ffb 100644 (file)
@@ -42,10 +42,10 @@ if target_os == 'linux':
        SConscript('c_common/oic_string/test/SConscript')
        SConscript('c_common/oic_malloc/test/SConscript')
        SConscript('c_common/oic_time/test/SConscript')
+       SConscript('c_common/ocrandom/test/SConscript')
 
        # Build C unit tests
        SConscript('csdk/stack/test/SConscript')
-       SConscript('csdk/ocrandom/test/SConscript')
 
        SConscript('csdk/connectivity/test/SConscript')
 
index 0506303..65b0921 100644 (file)
@@ -36,7 +36,7 @@ unittests_env.PrependUnique(CPPPATH = [
                '../csdk/connectivity/api',
                '../csdk/connectivity/external/inc',
                '../csdk/ocsocket/include',
-               '../csdk/ocrandom/include',
+               '../c_common/ocrandom/include',
                '../csdk/logger/include',
                '#extlibs/gtest/gtest-1.7.0/include',
                '#extlibs/hippomocks-master/HippoMocks',
index b77e5dc..061df52 100755 (executable)
@@ -40,7 +40,7 @@ simulator_env.AppendUnique(CPPPATH = ['inc', 'src/client', 'src/server', 'src/co
 simulator_env.AppendUnique(CPPPATH = [
                '../../resource/include/',
                '../../resource/csdk/stack/include',
-               '../../resource/csdk/ocrandom/include',
+               '../../resource/c_common/ocrandom/include',
                '../../resource/csdk/logger/include',
                '../../resource/oc_logger/include',
                './ramlparser/raml',
@@ -86,4 +86,4 @@ simulator_env.InstallTarget(simulatorsdk, 'libSimulator')
 
 #Build sample application
 SConscript('examples/server/SConscript')
-SConscript('examples/client/SConscript')
\ No newline at end of file
+SConscript('examples/client/SConscript')
index 6ddeebc..208ec68 100644 (file)
@@ -67,7 +67,7 @@ lib_env.AppendUnique(CPPPATH = [
                resource_path + '/include' ,
                resource_path + '/oc_logger/include',
                resource_path + '/csdk/stack/include',
-               resource_path + '/csdk/ocrandom/include',
+               resource_path + '/c_common/ocrandom/include',
                resource_path + '/csdk/logger/include'
                ])
 
index fb1d23e..27c7dd4 100755 (executable)
@@ -84,7 +84,7 @@ find $OUTDIR/objs -name "*.o" | xargs rm
 echo "Framework: Copying includes..."
 cp -r  resource/csdk/stack/include/*.h  $FRAMEWORK_BUNDLE/Headers
 cp -r  resource/csdk/ocsocket/include/*.h  $FRAMEWORK_BUNDLE/Headers
-cp -r  resource/csdk/ocrandom/include/*.h  $FRAMEWORK_BUNDLE/Headers
+cp -r  resource/c_common/ocrandom/include/*.h  $FRAMEWORK_BUNDLE/Headers
 cp -r  resource/csdk/ocmalloc/include/*.h  $FRAMEWORK_BUNDLE/Headers
 
 echo "Framework: Creating plist..."
index 9fa2e0a..45a0f74 100755 (executable)
@@ -85,7 +85,7 @@ find $OUTDIR/objs -name "*.o" | xargs rm
 
 echo "Framework: Copying includes..."
 cp -r  resource/csdk/stack/include/*.h  $FRAMEWORK_BUNDLE/Headers
-cp -r  resource/csdk/ocrandom/include/*.h  $FRAMEWORK_BUNDLE/Headers
+cp -r  resource/c_common/ocrandom/include/*.h  $FRAMEWORK_BUNDLE/Headers
 cp -r  resource/csdk/ocmalloc/include/*.h  $FRAMEWORK_BUNDLE/Headers
 
 echo "Framework: Creating plist..."
index 6c6fc22..2fba7c4 100755 (executable)
@@ -130,7 +130,7 @@ cp out/tizen/*/%{build_mode}/lib*.so %{buildroot}%{_libdir}
 
 cp resource/csdk/stack/include/*.h %{buildroot}%{_includedir}
 cp resource/csdk/logger/include/*.h %{buildroot}%{_includedir}
-cp resource/csdk/ocrandom/include/*.h %{buildroot}%{_includedir}
+cp resource/c_common/ocrandom/include/*.h %{buildroot}%{_includedir}
 cp -r resource/oc_logger/include/* %{buildroot}%{_includedir}
 cp resource/include/*.h %{buildroot}%{_includedir}