Adding an IoTivity/Zigbee sample server
authorJoseph Morrow <joseph.l.morrow@intel.com>
Sat, 19 Sep 2015 03:24:55 +0000 (20:24 -0700)
committerPatrick Lankswert <patrick.lankswert@intel.com>
Sun, 20 Sep 2015 05:38:19 +0000 (05:38 +0000)
Added functionality for the IoTivity/Zigbee server.  Added file
for addition of the client.  Changed the PluginStart to have the correct
parameters.  Corrected SConscript to properly include the IoTivity
logger and CSDK.

Change-Id: I20aca3403c6a0a1e198fdb337a6affe13c389417
Signed-off-by: Erich Keane <erich.keane@intel.com>
Signed-off-by: Joseph Morrow <joseph.l.morrow@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2122
Reviewed-by: Joseph Morrow <joseph.l.morrow@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2767
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Patrick Lankswert <patrick.lankswert@intel.com>
plugins/include/plugininterface.h
plugins/samples/linux/IotivityandZigbeeClient.c [moved from plugins/samples/linux/IotivityandZigbee.c with 91% similarity]
plugins/samples/linux/IotivityandZigbeeClient.h [moved from plugins/samples/linux/IotivityandZigbee.h with 80% similarity]
plugins/samples/linux/IotivityandZigbeeServer.c [new file with mode: 0644]
plugins/samples/linux/IotivityandZigbeeServer.h [new file with mode: 0644]
plugins/samples/linux/SConscript
plugins/src/plugininterface.c
plugins/unittests/plugininterfacetest.cpp

index 53bddd4..501f07b 100644 (file)
@@ -41,11 +41,12 @@ extern "C" {
  *
  * Makes any required calls to instantiate IoTivity and/or plugin's radio.
  *
- * @param[in] plugin The plugin to be started.
+ * @param[in]  pluginType The type of plugin to start.
+ * @param[out] plugin The plugin handle that will be started.
  *   Note: Please note that the plugin will need to be managed in the
  *         application space.
  */
-OCStackResult PIStartPlugin(PIPluginBase * plugin);
+OCStackResult PIStartPlugin(PIPluginType pluginType, PIPluginBase ** plugin);
 
 /**
  *
similarity index 91%
rename from plugins/samples/linux/IotivityandZigbee.c
rename to plugins/samples/linux/IotivityandZigbeeClient.c
index f21526f..06a085e 100644 (file)
@@ -5,7 +5,7 @@
 // except for resource-specific IoTivity APIs (ie. OCCreateResource(),
 // OCDeleteResource(), EntityHandler()..etc.)
 
-#include "IotivityandZigbee.h"
+#include "IotivityandZigbeeClient.h"
 
 int main(int argc, char* argv[])
 {
similarity index 80%
rename from plugins/samples/linux/IotivityandZigbee.h
rename to plugins/samples/linux/IotivityandZigbeeClient.h
index 53898ac..f8f3e9d 100644 (file)
@@ -1,4 +1,4 @@
-// The source file for sample application "IotivityandZigbee".
+// The source file for sample application "IotivityandZigbeeServer".
 
 // This application will utilize our interface (ie. zpluginz.h).
 // The application may still be responsible for making any IoTivity API calls,
diff --git a/plugins/samples/linux/IotivityandZigbeeServer.c b/plugins/samples/linux/IotivityandZigbeeServer.c
new file mode 100644 (file)
index 0000000..3b66e64
--- /dev/null
@@ -0,0 +1,172 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "IotivityandZigbeeServer.h"
+#include <signal.h>
+#include <ocstack.h>
+#include <logger.h>
+
+#define TAG "IoTivityZigbeeServer"
+
+int main(int argc, char* argv[])
+{
+    OCStackResult result;
+    OC_LOG(INFO, TAG, "Initializing IoTivity...");
+
+    // Initialize Stack
+    result = OCInit(NULL, 0, OC_SERVER);
+    if (result != OC_STACK_OK)
+    {
+        OC_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);
+        goto IotivityStop;
+    }
+
+    // Set Device Info
+    result  = SetDeviceInfo();
+    if (result != OC_STACK_OK)
+    {
+        OC_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);
+        goto IotivityStop;
+    }
+
+    // PIStartPlugin
+    PIPluginBase* plugin = NULL;
+    OC_LOG(INFO, TAG, "IoTivity Initialized properly, Starting Zigbee Plugin...");
+    result = PIStartPlugin(PLUGIN_ZIGBEE, &plugin);
+    if (result != OC_STACK_OK)
+    {
+        OC_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...");
+    }
+    else
+    {
+        OC_LOG(INFO, TAG, "Zigbee Plugin started correctly, press Ctrl-C to terminate application");
+        // Loop until sigint
+        while (processSignal(false) && result == OC_STACK_OK)
+        {
+            result = OCProcess();
+            if (result != OC_STACK_OK)
+            {
+                OC_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);
+            }
+        }
+    }
+
+    OC_LOG(INFO, TAG, "Stopping Zigbee Plugin...");
+    // PIStopPlugin
+    OC_LOG(INFO, TAG, "Zigbee Plugin Stopped");
+    result = PIStopPlugin(plugin);
+    if (result != OC_STACK_OK)
+    {
+        OC_LOG_V(ERROR, TAG, "Zigbee Plugin Stop Failed: %d", result);
+    }
+
+    // OCStop
+IotivityStop:
+    OC_LOG(INFO, TAG, "Stopping IoTivity...");
+    result = OCStop();
+    if (result != OC_STACK_OK)
+    {
+        OC_LOG_V(ERROR, TAG, "OCStop Failed: %d", result);
+        return 0;
+    }
+
+    OC_LOG(INFO, TAG, "Application Completed Successfully");
+}
+
+OCStackResult SetPlatformInfo()
+{
+    static const OCPlatformInfo platformInfo =
+        {
+            .platformID = "IoTivityZigbeeID",
+            .manufacturerName = "IoTivity",
+            .manufacturerUrl = "http://iotivity.org",
+            .modelNumber = "T1000",
+            .dateOfManufacture = "January 14th, 2015",
+            .platformVersion = "0.9.2",
+            .operatingSystemVersion = "7",
+            .hardwareVersion = "0.5",
+            .firmwareVersion = "0",
+            .supportUrl = "http://iotivity.org",
+            .systemTime = ""
+        };
+
+    return OCSetPlatformInfo(platformInfo);
+}
+
+OCStackResult SetDeviceInfo()
+{
+    static const OCDeviceInfo deviceInfo =
+        {
+            .deviceName = "IoTivity/Zigbee Server Sample"
+        };
+
+    return OCSetDeviceInfo(deviceInfo);
+}
+
+bool processSignal(bool set)
+{
+    static bool signal = false;
+    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;
+    }
+
+    return signal;
+}
+
+void processCancel(int signal)
+{
+    if(signal == SIGINT)
+    {
+        processSignal(true);
+    }
+}
+
diff --git a/plugins/samples/linux/IotivityandZigbeeServer.h b/plugins/samples/linux/IotivityandZigbeeServer.h
new file mode 100644 (file)
index 0000000..3c264d4
--- /dev/null
@@ -0,0 +1,28 @@
+//******************************************************************
+//
+// Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "plugininterface.h"
+#include <octypes.h>
+#include <stdbool.h>
+
+OCStackResult SetPlatformInfo();
+OCStackResult SetDeviceInfo();
+bool processSignal(bool set);
+void processCancel(int signal);
index 0b1f0ae..27179c2 100644 (file)
@@ -30,9 +30,10 @@ pi_dir = os.path.join(src_dir, 'plugins')
 # Build flags
 ######################################################################
 samples_env.PrependUnique(CPPPATH = [
-               '../../../../logger/include',
-               '../../../../stack/include',
-               '../../../../../../extlibs/cjson',
+               '../../../resource/oc_logger/include',
+               '../../../resource/csdk/logger/include',
+               '../../../resource/csdk/stack/include',
+               '../../../extlibs/cjson',
                 os.path.join(pi_dir, 'include')
                ])
 
@@ -63,9 +64,12 @@ samples_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
 ######################################################################
 # Source files and Targets
 ######################################################################
-iotivityandzigbee         = samples_env.Program('iotivityandzigbee', ['IotivityandZigbee.c'])
+iotivityandzigbeeserver = samples_env.Program('iotivityandzigbeeserver',
+        ['IotivityandZigbeeServer.c'])
+iotivityandzigbeeclient = samples_env.Program('iotivityandzigbeeclient',
+        ['IotivityandZigbeeClient.c'])
 
-list_of_samples = [iotivityandzigbee]
+list_of_samples = [iotivityandzigbeeserver, iotivityandzigbeeclient]
 
 Alias("samples", list_of_samples)
 
index 58292a9..9896c02 100644 (file)
@@ -33,7 +33,7 @@
 //                allow another instance to occur within the same program space
 //                and will even return a soft success when we try (ie.
 //                OC_STACK_OK).
-OCStackResult PIStartPlugin(PIPluginBase * plugin)
+OCStackResult PIStartPlugin(PIPluginType pluginType, PIPluginBase** plugin)
 {
     return OC_STACK_NOTIMPL;
 }
index 57925ef..05c6e90 100644 (file)
@@ -56,7 +56,7 @@ std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(5);
 TEST(PITests, StartPluginTest)
 {
     itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
-    EXPECT_EQ(OC_STACK_NOTIMPL, PIStartPlugin(NULL));
+    EXPECT_EQ(OC_STACK_NOTIMPL, PIStartPlugin(PLUGIN_UNKNOWN, NULL));
 }
 
 // Plugin Interface API PIStopPlugin()