From 3c30b5222b0142cf897e8600c74314b7993a4bc3 Mon Sep 17 00:00:00 2001 From: Joseph Morrow Date: Fri, 18 Sep 2015 20:24:55 -0700 Subject: [PATCH] Adding an IoTivity/Zigbee sample server 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 Signed-off-by: Joseph Morrow Reviewed-on: https://gerrit.iotivity.org/gerrit/2122 Reviewed-by: Joseph Morrow Reviewed-on: https://gerrit.iotivity.org/gerrit/2767 Tested-by: jenkins-iotivity Reviewed-by: Patrick Lankswert --- plugins/include/plugininterface.h | 5 +- ...tivityandZigbee.c => IotivityandZigbeeClient.c} | 2 +- ...tivityandZigbee.h => IotivityandZigbeeClient.h} | 2 +- plugins/samples/linux/IotivityandZigbeeServer.c | 172 +++++++++++++++++++++ plugins/samples/linux/IotivityandZigbeeServer.h | 28 ++++ plugins/samples/linux/SConscript | 14 +- plugins/src/plugininterface.c | 2 +- plugins/unittests/plugininterfacetest.cpp | 2 +- 8 files changed, 216 insertions(+), 11 deletions(-) rename plugins/samples/linux/{IotivityandZigbee.c => IotivityandZigbeeClient.c} (91%) rename plugins/samples/linux/{IotivityandZigbee.h => IotivityandZigbeeClient.h} (80%) create mode 100644 plugins/samples/linux/IotivityandZigbeeServer.c create mode 100644 plugins/samples/linux/IotivityandZigbeeServer.h diff --git a/plugins/include/plugininterface.h b/plugins/include/plugininterface.h index 53bddd4..501f07b 100644 --- a/plugins/include/plugininterface.h +++ b/plugins/include/plugininterface.h @@ -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); /** * diff --git a/plugins/samples/linux/IotivityandZigbee.c b/plugins/samples/linux/IotivityandZigbeeClient.c similarity index 91% rename from plugins/samples/linux/IotivityandZigbee.c rename to plugins/samples/linux/IotivityandZigbeeClient.c index f21526f..06a085e 100644 --- a/plugins/samples/linux/IotivityandZigbee.c +++ b/plugins/samples/linux/IotivityandZigbeeClient.c @@ -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[]) { diff --git a/plugins/samples/linux/IotivityandZigbee.h b/plugins/samples/linux/IotivityandZigbeeClient.h similarity index 80% rename from plugins/samples/linux/IotivityandZigbee.h rename to plugins/samples/linux/IotivityandZigbeeClient.h index 53898ac..f8f3e9d 100644 --- a/plugins/samples/linux/IotivityandZigbee.h +++ b/plugins/samples/linux/IotivityandZigbeeClient.h @@ -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 index 0000000..3b66e64 --- /dev/null +++ b/plugins/samples/linux/IotivityandZigbeeServer.c @@ -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 +#include +#include + +#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 index 0000000..3c264d4 --- /dev/null +++ b/plugins/samples/linux/IotivityandZigbeeServer.h @@ -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 +#include + +OCStackResult SetPlatformInfo(); +OCStackResult SetDeviceInfo(); +bool processSignal(bool set); +void processCancel(int signal); diff --git a/plugins/samples/linux/SConscript b/plugins/samples/linux/SConscript index 0b1f0ae..27179c2 100644 --- a/plugins/samples/linux/SConscript +++ b/plugins/samples/linux/SConscript @@ -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) diff --git a/plugins/src/plugininterface.c b/plugins/src/plugininterface.c index 58292a9..9896c02 100644 --- a/plugins/src/plugininterface.c +++ b/plugins/src/plugininterface.c @@ -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; } diff --git a/plugins/unittests/plugininterfacetest.cpp b/plugins/unittests/plugininterfacetest.cpp index 57925ef..05c6e90 100644 --- a/plugins/unittests/plugininterfacetest.cpp +++ b/plugins/unittests/plugininterfacetest.cpp @@ -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() -- 2.7.4