Enable compilation and functionality on darwin(osx)
authorCharlie Lenahan <charlie.lenahan@intel.com>
Tue, 7 Apr 2015 18:59:58 +0000 (14:59 -0400)
committerErich Keane <erich.keane@intel.com>
Wed, 8 Apr 2015 19:30:10 +0000 (19:30 +0000)
Currently, the iotivity project does not support compiling/
running on Apple Darwin platforms.  This patch enables
compilation and minimal sample functionality for darwin.

It accomplishes this by adding 'darwin' as a valid
target_os in all build-scripts.  glib2 compiling/linking
is also added for the darwin build, which was previously
missing and blocking building connectivity-abstraction.

Many positions in the code had #ifdef __linux__, which has
been switched to also recognize __APPLE__.

Finally, the logger time-functionality used linux-specific
clock_gettime, so this functionality has been broken up
into an osalGetTime function, with an implementation for
__APPLE__ included in this patch.

Change-Id: Iddc2462db44198be90a975536e0273f63c521ab6
Signed-off-by: Charlie Lenahan <charlie.lenahan@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/668
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
16 files changed:
SConstruct
build_common/darwin/SConscript
resource/SConscript
resource/csdk/SConscript
resource/csdk/connectivity/SConscript
resource/csdk/connectivity/common/inc/logger.h
resource/csdk/connectivity/lib/libcoap-4.1.1/SConscript
resource/csdk/connectivity/lib/libcoap-4.1.1/net.c
resource/csdk/connectivity/src/SConscript
resource/csdk/connectivity/src/camessagehandler.c
resource/csdk/connectivity/src/ethernet_adapter/SConscript
resource/csdk/connectivity/src/ethernet_adapter/linux/caethernetnwmonitor.c
resource/csdk/logger/src/logger.c
resource/csdk/ocrandom/src/ocrandom.c
resource/csdk/stack/samples/linux/SimpleClientServer/SConscript
service/SConscript

index 6f2f254..318541f 100644 (file)
@@ -47,7 +47,7 @@ build_dir = env.get('BUILD_DIR')
 # Build 'resource' sub-project
 SConscript(build_dir + 'resource/SConscript')
 
-if target_os != 'arduino':
+if target_os not in ['arduino','darwin','ios']:
        SConscript(build_dir + 'examples/OICMiddle/SConscript')
 
 # Build 'service' sub-project
index 7d57774..747144a 100644 (file)
@@ -35,6 +35,9 @@ if env.get('RELEASE'):
        env.AppendUnique(CPPDEFINES = ['NDEBUG'])
 else:
        env.AppendUnique(CCFLAGS = ['-g'])
+       env.AppendUnique(LINKFLAGS = ['-g'])
+
+env.ParseConfig("pkg-config glib-2.0 gthread-2.0 --cflags --libs")
 
 if target_os == 'darwin':
        sys_root = '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX' + sys_version + '.sdk/'
index 77af932..8673abf 100644 (file)
@@ -39,7 +39,7 @@ SConscript('csdk/connectivity/SConscript')
 # Build liboctbstack
 SConscript('csdk/SConscript')
 
-if target_os != 'arduino':
+if target_os not in ['arduino','darwin']:
        # Build liboc_logger
        SConscript('oc_logger/SConscript')
 
@@ -58,7 +58,9 @@ if target_os == 'linux':
 
        # Build C/C++ unit tests
        SConscript('unit_tests.scons')
-
+elif target_os == 'darwin':
+       # Build linux samples for now
+       SConscript('csdk/stack/samples/linux/SimpleClientServer/SConscript')
 elif target_os == 'arduino':
        SConscript('csdk/stack/samples/arduino/SimpleClientServer/ocserver/SConscript')
 
index 8bc655c..fb6a9cd 100644 (file)
@@ -66,7 +66,7 @@ liboctbstack_env.AppendUnique(LIBS = ['coap', 'm'])
 
 if target_os == 'arduino':
        liboctbstack_env.AppendUnique(CPPDEFINES = ['NDEBUG', 'WITH_ARDUINO'])
-else:
+elif target_os not in ['darwin','ios']:
        liboctbstack_env.AppendUnique(CFLAGS = ['-fPIC'])
 
 if target_os in ['darwin', 'ios']:
@@ -95,7 +95,7 @@ liboctbstack_src = [
        'ocrandom/src/ocrandom.c',
        'ocmalloc/src/ocmalloc.c'
        ]
-if target_os == 'arduino':
+if target_os in ['arduino','darwin'] :
        liboctbstack = liboctbstack_env.StaticLibrary('octbstack', liboctbstack_src)
 else:
        liboctbstack = liboctbstack_env.SharedLibrary('octbstack', liboctbstack_src)
index 5123612..17db6bf 100644 (file)
@@ -23,6 +23,8 @@ if 'ALL' in transport:
                env.AppendUnique(CPPDEFINES = ['WIFI_ADAPTER', 'ETHERNET_ADAPTER','NO_EDR_ADAPTER','NO_LE_ADAPTER'])
        elif target_os == 'tizen':
                env.AppendUnique(CPPDEFINES = ['WIFI_ADAPTER', 'NO_ETHERNET_ADAPTER','EDR_ADAPTER','LE_ADAPTER'])
+       elif target_os == 'darwin':
+               env.AppendUnique(CPPDEFINES = ['NO_WIFI_ADAPTER','ETHERNET_ADAPTER','NO_EDR_ADAPTER','NO_LE_ADAPTER'])
        else:
                env.AppendUnique(CPPDEFINES = ['WIFI_ADAPTER', 'ETHERNET_ADAPTER','EDR_ADAPTER','LE_ADAPTER'])
        print "CA Transport is ALL"
index fab9daa..61f363d 100644 (file)
@@ -75,7 +75,7 @@ typedef enum
 #ifdef __TIZEN__
 #define OICLog(level,tag,mes) LOG(level,tag,mes)
 #define OICLogv(level,tag,fmt,args...) LOG(level,tag,fmt,##args)
-#elif defined(ANDROID) || defined(__linux__)
+#elif defined(ANDROID) || defined(__linux__) || defined(__APPLE__)
 /**
  * Configure logger to use a context that defines a custom logger function
  *
index eb1491a..761b328 100644 (file)
@@ -7,6 +7,7 @@ Import('env')
 libcoap_env = env.Clone()
 
 target_os = env.get('TARGET_OS')
+
 # As in the source code(C) includes arduino Time library head file(C++)
 # It requires compile the .c with g++
 if target_os == 'arduino':
@@ -29,7 +30,7 @@ if target_os == 'arduino':
 
 if target_os not in ['arduino', 'windows', 'winrt']:
        libcoap_env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])
-       libcoap_env.AppendUnique(CFLAGS = ['-std=gnu99', '-fPIC'])
+       libcoap_env.AppendUnique(CFLAGS = ['-std=gnu99','-fPIC'])
 
 if target_os not in ['windows', 'winrt']:
        libcoap_env.AppendUnique(CFLAGS = ['-Wall', '-ffunction-sections',
index 6c6fe54..02bc6ec 100644 (file)
@@ -49,7 +49,7 @@
 
 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
 
-time_t clock_offset;
+time_t clock_offset=0;
 
 static inline coap_queue_t *
 coap_malloc_node()
index 6dc68cd..40e9eee 100644 (file)
@@ -27,6 +27,12 @@ env.AppendUnique(CPPPATH = [root_dir + '/inc/'])
 env.AppendUnique(CPPPATH = [root_dir + '/lib/libcoap-4.1.1/'])
 env.AppendUnique(CPPPATH = [root_dir + '/common/inc/'])
 
+if ca_os not in ['arduino', 'windows', 'winrt']:
+       env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])
+
+if ca_os in ['darwin']:
+       env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
+
 #Getting common source files
 env.SConscript('./../common/SConscript')
 
index 6e88639..1ea2b7d 100644 (file)
@@ -29,6 +29,7 @@
 #include "cainterfacecontroller.h"
 #include "caprotocolmessage.h"
 #include "caretransmission.h"
+#include "caadapterutils.h"
 #include "uqueue.h"
 #include "logger.h"
 #include "config.h" /* for coap protocol */
index 7758ee5..9e48b3b 100644 (file)
@@ -18,7 +18,7 @@ else:
         env.AppendUnique(CA_SRC=[src_dir+'caethernetadapter.c'])
 
 #Source files to build in Linux platform
-if target_os == 'linux':
+if target_os in ['linux','darwin']:
        env.AppendUnique(CA_SRC=[src_dir+'linux/caethernetclient.c',
                        src_dir+'linux/caethernetserver.c',
                        src_dir+'linux/caethernetnwmonitor.c',
index fe4d50b..0e2311b 100644 (file)
 
 #define ETHERNET_MONITOR_TAG "ETHERNET_MONITOR"
 
+#ifdef __APPLE__
+#define ETHERNET_INF_PREFIX "en"
+#else
+#define ETHERNET_INF_PREFIX "eth"
+#endif
+
 /**
  * @var nwConnectivityStatus
  * @brief  Maintains network status.
@@ -112,8 +118,8 @@ static CAEthernetConnectionStateChangeCallback g_networkChangeCb = NULL;
  * @fn CAEthernetGetInterfaceInformation
  * @brief This methods gets local interface name and IP address information.
  */
-static void CAEthernetGetInterfaceInformation(char **interfaceName, char **ipAddress,
-        char **subnetMask);
+static void CAEthernetGetInterfaceInformation(const char *interfacePrefix,
+        char **interfaceName, char **ipAddress,char **subnetMask);
 
 static void CANetworkMonitorThread(void *threadData);
 
@@ -129,7 +135,7 @@ CAResult_t CAEthernetInitializeNetworkMonitor(const u_thread_pool_t threadPool)
     }
 
     u_mutex_lock(g_ethernetNetInfoMutex);
-    CAEthernetGetInterfaceInformation(&g_ethernetInterfaceName, &g_ethernetIPAddress,
+    CAEthernetGetInterfaceInformation(ETHERNET_INF_PREFIX,&g_ethernetInterfaceName, &g_ethernetIPAddress,
                                       &g_ethernetSubnetMask);
     u_mutex_unlock(g_ethernetNetInfoMutex);
 
@@ -287,7 +293,7 @@ void CAEthernetSetConnectionStateChangeCallback
     OIC_LOG(DEBUG, ETHERNET_MONITOR_TAG, "OUT");
 }
 
-void CAEthernetGetInterfaceInformation(char **interfaceName,
+void CAEthernetGetInterfaceInformation(const char *interfaceNamePrefix,char **interfaceName,
     char **ipAddress, char **subnetMask)
 {
     if (!interfaceName || !ipAddress || !subnetMask)
@@ -328,9 +334,8 @@ void CAEthernetGetInterfaceInformation(char **interfaceName,
             continue;
         }
 
-        const char matchName[] = "eth";
-        int matchNameLen = strlen(matchName);
-        if (!strncasecmp(ifa->ifa_name, matchName, matchNameLen))
+        int matchNameLen = strlen(interfaceNamePrefix);
+        if (!strncasecmp(ifa->ifa_name, interfaceNamePrefix, matchNameLen))
         {
             // get the interface ip address
             if (0 != getnameinfo(ifa->ifa_addr, len, interfaceAddress,
@@ -375,7 +380,7 @@ void CANetworkMonitorThread(void *threadData)
         char *interfaceName = NULL;
         char *ipAddress = NULL;
         char *subnetMask = NULL;
-        CAEthernetGetInterfaceInformation(&interfaceName, &ipAddress, &subnetMask);
+        CAEthernetGetInterfaceInformation(ETHERNET_INF_PREFIX,&interfaceName, &ipAddress, &subnetMask);
 
         // check current network status
         CANetworkStatus_t currNetworkStatus;
index fd93e74..020a7b0 100644 (file)
 #include <time.h>
 #endif
 
+#ifdef __APPLE__
+#include <sys/time.h>
+#endif
+
 #include "logger.h"
 #include "string.h"
 #include "oc_logger.h"
@@ -59,7 +63,7 @@ static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 + 1;  // Show 16 bytes, 2
 // Convert LogLevel to platform-specific severity level.  Store in PROGMEM on Arduino
 #ifdef __ANDROID__
     static android_LogPriority LEVEL[] = {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
-#elif defined __linux__
+#elif defined(__linux__) || defined(__APPLE__)
     static const char * LEVEL[] __attribute__ ((unused)) = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
 #elif defined ARDUINO
     #include <stdarg.h>
@@ -96,7 +100,7 @@ void OCLogInit() {
 }
 
 void OCLogShutdown() {
-#ifdef __linux__
+#if defined(__linux__) || defined(__APPLE__)
     if (logCtx && logCtx->destroy)
     {
         logCtx->destroy(logCtx);
@@ -125,6 +129,36 @@ void OCLogv(LogLevel level, const char * tag, const char * format, ...) {
     OCLog(level, tag, buffer);
 }
 
+#if defined(__linux__)
+static void  osalGetTime(int *min,int *sec, int *ms)
+{
+    if (min && sec && ms)
+    {
+        struct timespec when = {};
+        if (!clock_gettime(CLOCK_REALTIME_COARSE, &when))
+        {
+            *min = (when.tv_sec / 60) % 60;
+            *sec = when.tv_sec % 60;
+            *ms = when.tv_nsec / 1000000;
+        }
+    }
+}
+#elif defined(__APPLE__)
+static void  osalGetTime(int *min,int *sec, int *ms)
+{
+    if (min && sec && ms)
+    {
+        struct timeval now;
+        if (!gettimeofday(&now, NULL))
+        {
+            *min = (now.tv_sec / 60) % 60;
+            *sec = now.tv_sec % 60;
+            *ms = now.tv_usec * 1000;
+        }
+    }
+}
+#endif
+
 /**
  * Output a log string with the specified priority level.
  * Only defined for Linux and Android
@@ -138,29 +172,24 @@ void OCLog(LogLevel level, const char * tag, const char * logStr) {
         return;
     }
 
-    #ifdef __ANDROID__
-        __android_log_write(LEVEL[level], tag, logStr);
-    #elif defined __linux__
-        if (logCtx && logCtx->write_level)
-        {
-            logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
+#ifdef __ANDROID__
+    __android_log_write(LEVEL[level], tag, logStr);
+#elif defined(__linux__) || defined(__APPLE__)
+    if (logCtx && logCtx->write_level)
+    {
+        logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
+    }
+    else
+    {
 
-        }
-        else
-        {
-            struct timespec when = {};
-            int min = 0;
-            int sec = 0;
-            int ms = 0;
-            if (!clock_gettime(CLOCK_REALTIME_COARSE, &when))
-            {
-                min = (when.tv_sec / 60) % 60;
-                sec = when.tv_sec % 60;
-                ms = when.tv_nsec / 1000000;
-            }
-            printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
-        }
-    #endif
+        int min = 0;
+        int sec = 0;
+        int ms = 0;
+        osalGetTime(&min,&sec,&ms);
+
+        printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
+    }
+#endif
 }
 
 /**
@@ -386,3 +415,4 @@ void OCLogBuffer(LogLevel level, const char * tag, const uint8_t * buffer, uint1
 #endif
 
 
+
index 8c2d0a4..648f72e 100644 (file)
@@ -19,7 +19,7 @@
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 
-#if defined(__ANDROID__) || defined(__linux__)
+#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
 #include "fcntl.h"
 #include "unistd.h"
 #endif
@@ -62,7 +62,7 @@ uint8_t GetRandomBit() {
 #endif
 
 int8_t OCSeedRandom() {
-#if defined(__ANDROID__) || defined(__linux__)
+#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
     int32_t fd = open("/dev/urandom", O_RDONLY);
     if (fd > 0) {
         uint32_t randomSeed;
@@ -109,7 +109,7 @@ uint32_t OCGetRandom() {
 }
 
 uint8_t OCGetRandomByte(void) {
-#if defined(__ANDROID__) || defined(__linux__)
+#if defined(__ANDROID__) || defined(__linux__) || defined(__APPLE__)
     return rand() & 0x00FF;
 #elif defined ARDUINO
     return random(256) & 0x00FF;
index a9b3aac..714dd86 100644 (file)
@@ -19,7 +19,7 @@
 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 Import('env')
-
+target_os = env.get('TARGET_OS')
 samples_env = env.Clone()
 ######################################################################
 # Build flags
@@ -35,9 +35,14 @@ samples_env.PrependUnique(CPPPATH = [
 
 samples_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread'])
 samples_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
-samples_env.AppendUnique(LIBS = ['-lpthread'])
+samples_env.AppendUnique(LIBS = ['-lpthread' ])
 samples_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
-samples_env.PrependUnique(LIBS = ['m', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap'])
+
+if target_os in ['darwin']:
+       samples_env.PrependUnique(LIBS = ['m','octbstack', 'connectivity_abstraction','coap' ])
+else:
+       samples_env.PrependUnique(LIBS = ['m', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'coap'])
+
 if env.get('SECURED') == '1':
     samples_env.AppendUnique(LIBS = ['tinydtls'])
 samples_env.ParseConfig('pkg-config --libs glib-2.0');
index 6819e2d..337ac13 100644 (file)
@@ -26,7 +26,7 @@ Import('env')
 
 target_os = env.get('TARGET_OS')
 
-if target_os != 'arduino':
+if target_os not in ['arduino','darwin']:
        # Build things manager project
        SConscript('things-manager/SConscript')