[JIRA Issue - 1092] IoTivity Zigbee sample application wrongly freeing memory
[platform/upstream/iotivity.git] / plugins / samples / linux / IotivityandZigbeeServer.c
index 3b66e64..b978074 100644 (file)
 #include <signal.h>
 #include <ocstack.h>
 #include <logger.h>
+#include "oic_string.h"
+#include "oic_malloc.h"
 
 #define TAG "IoTivityZigbeeServer"
-
-int main(int argc, char* argv[])
+#define defaultComPort "/dev/ttyUSB0"
+int main()
 {
-    OCStackResult result;
-    OC_LOG(INFO, TAG, "Initializing IoTivity...");
-
-    // Initialize Stack
-    result = OCInit(NULL, 0, OC_SERVER);
+    OIC_LOG(INFO, TAG, "Initializing IoTivity...");
+    OCStackResult result = OCInit(NULL, 0, OC_SERVER);
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
+        OIC_LOG_V(ERROR, TAG, "OCInit Failed %d", result);
         return -1;
     }
 
-    // Set Platform info
     result = SetPlatformInfo();
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
+        OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed %d", result);
         goto IotivityStop;
     }
 
-    // Set Device Info
     result  = SetDeviceInfo();
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
+        OIC_LOG_V(ERROR, TAG, "SetPlatformInfo Failed: %d", result);
         goto IotivityStop;
     }
-    // Start Presence
+
     result  = OCStartPresence(0);
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
+        OIC_LOG_V(ERROR, TAG, "OCStartPresence Failed: %d", result);
         goto IotivityStop;
     }
 
     // PIStartPlugin
-    PIPluginBase* plugin = NULL;
-    OC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
-    result = PIStartPlugin(PLUGIN_ZIGBEE, &plugin);
+    PIPlugin* plugin = NULL;
+    OIC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
+    result = PIStartPlugin(defaultComPort, PLUGIN_ZIGBEE, &plugin);
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
+        OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Start Failed: %d", result);
         goto IotivityStop;
     }
 
     if (signal(SIGINT, processCancel) == SIG_ERR)
     {
-        OC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
+        OIC_LOG(ERROR, TAG, "Unable to catch SIGINT, terminating...");
     }
     else
     {
-        OC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
+        OIC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
         // Loop until sigint
-        while (processSignal(false) && result == OC_STACK_OK)
+        while (!processSignal(false) && result == OC_STACK_OK)
         {
             result = OCProcess();
             if (result != OC_STACK_OK)
             {
-                OC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
+                OIC_LOG_V(ERROR, TAG, "OCProcess Failed: %d", result);
                 break;
             }
 
             result = PIProcess(plugin);
             if (result != OC_STACK_OK)
             {
-                OC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
+                OIC_LOG_V(ERROR, TAG, "PIProcess Failed: %d", result);
             }
         }
     }
 
-    OC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
-    // PIStopPlugin
-    OC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
+    OIC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
     result = PIStopPlugin(plugin);
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
+        OIC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
     }
-
+    OIC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
     // OCStop
 IotivityStop:
-    OC_LOG(INFO, TAG, "Stopping IoTivity...");
+    OIC_LOG(INFO, TAG, "Stopping IoTivity...");
     result = OCStop();
     if (result != OC_STACK_OK)
     {
-        OC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
+        OIC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
         return 0;
     }
 
-    OC_LOG(INFO, TAG, "Application Completed Successfully");
+    OIC_LOG(INFO, TAG, "Application Completed Successfully");
+    return 0;
 }
 
 OCStackResult SetPlatformInfo()
@@ -140,26 +136,27 @@ OCStackResult SetPlatformInfo()
 
 OCStackResult SetDeviceInfo()
 {
-    static const OCDeviceInfo deviceInfo =
+    static OCDeviceInfo deviceInfo =
         {
-            .deviceName = "IoTivity/Zigbee Server Sample"
+            .deviceName = "IoTivity/Zigbee Server Sample",
+            .specVersion = "IoTivity/Zigbee Device Spec Version",
+            .dataModleVersion = "IoTivity/Zigbee Data Model Version",
         };
-
+    char *dup = OICStrdup("oic.wk.d");
+    deviceInfo.types = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+    deviceInfo.types->value = dup;
     return OCSetDeviceInfo(deviceInfo);
 }
 
 bool processSignal(bool set)
 {
-    static bool signal = false;
+    static sig_atomic_t signal = 0;
     if (set)
     {
-        // boolean assignments are atomic, and since we
-        // only have a single modifier, and only in one direction,
-        // this does not require locking.
-        signal = true;
+        signal = 1;
     }
 
-    return signal;
+    return signal == 1;
 }
 
 void processCancel(int signal)
@@ -169,4 +166,3 @@ void processCancel(int signal)
         processSignal(true);
     }
 }
-