From f3b534aa0ff25d308bb974af66f4f732432fe5b5 Mon Sep 17 00:00:00 2001 From: Shilpa Sodani Date: Wed, 14 Jan 2015 15:56:47 -0800 Subject: [PATCH] Resolved Klocwork warnings for C++ Stack & Samples Fixed comment, space issue and added default case to switch in groupclient.cpp Made code compliance to code guideline Resolve Infinite loop warning and Memory leak warning. Also replaced multiple if-else with switch to match code standards. Change-Id: I125ab9d20fe4c8352f261afa7a538a7487aa7880 Signed-off-by: Shilpa Sodani Reviewed-on: https://gerrit.iotivity.org/gerrit/141 Tested-by: jenkins-iotivity Reviewed-by: Erich Keane Reviewed-by: Sudarshan Prasad --- resource/examples/groupclient.cpp | 124 +++++++++++++++++++---------------- resource/examples/groupserver.cpp | 22 +++++-- resource/examples/simpleserver.cpp | 3 + resource/src/InProcClientWrapper.cpp | 3 + 4 files changed, 90 insertions(+), 62 deletions(-) diff --git a/resource/examples/groupclient.cpp b/resource/examples/groupclient.cpp index 11afb80..34f325f 100644 --- a/resource/examples/groupclient.cpp +++ b/resource/examples/groupclient.cpp @@ -20,9 +20,12 @@ #include "OCPlatform.h" #include "OCApi.h" +#include "logger.h" #include #include +#include +#include #include using namespace std; @@ -30,9 +33,10 @@ using namespace OC; namespace PH = std::placeholders; OCResourceHandle resourceHandle; - shared_ptr< OCResource > g_resource; vector< string > lights; +std::mutex blocker; +std::condition_variable cv; bool isReady = false; @@ -84,6 +88,7 @@ void onGet(const HeaderOptions& opt, const OCRepresentation &rep, const int eCod } isReady = true; + cv.notify_one(); } void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) @@ -121,6 +126,31 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con } } +string buildActionSetDesc() +{ + string actionsetDesc = ""; + //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap: + //10.251.44.228:49858/a/light|power=10|"; + actionsetDesc = "allbulboff"; + actionsetDesc.append("*"); + for (auto iter = lights.begin(); iter != lights.end(); ++iter) + { + actionsetDesc.append("uri=").append((*iter)); + actionsetDesc.append("|"); + actionsetDesc.append("power="); + actionsetDesc.append("off"); + if ((iter + 1) != lights.end()) + { + actionsetDesc.append("*"); + } + } + return actionsetDesc; +} + +bool isResourceReady() +{ + return isReady; +} int main() { PlatformConfig config @@ -135,94 +165,76 @@ int main() string resourceTypeName = "a.collection"; OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=a.collection", &foundResource); - isReady = false; + //Non-intensive block util foundResource callback is called by OCPlatform + //and onGet gets resource. + //isResourceReady takes care of spurious wake-up + + std::unique_lock lock(blocker); + cv.wait(lock, isResourceReady); while (isRun) { usleep(100); - while (isReady) - { - int n; + int selectedMenu; - cout << endl - << "1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET 3 :: GET ACTIONSET\n"; - cout << "4 :: DELETE ACTIONSET 5 :: Quit\n"; + cout << endl + << "0 :: Quit 1 :: CREATE ACTIONSET 2 :: EXECUTE ACTION SET \n"; + cout << "3 :: GET ACTIONSET 4 :: DELETE ACTIONSET \n" << endl; - cin >> n; - if (n == 1) - { - string actionsetDesc; - //"movieTime*uri=coap://10.251.44.228:49858/a/light|power=10*uri=coap://10.251.44.228:49858/a/light|power=10|"; + cin >> selectedMenu; + + OCRepresentation rep; + string actionsetDesc; + switch(selectedMenu) + { + case 0: + isRun = false; + break; + case 1: + actionsetDesc = buildActionSetDesc(); - actionsetDesc = "allbulboff"; - actionsetDesc.append("*"); - for (auto iter = lights.begin(); iter != lights.end(); ++iter) + if(!actionsetDesc.empty()) { - actionsetDesc.append("uri=").append((*iter)); - actionsetDesc.append("|"); - actionsetDesc.append("power="); - actionsetDesc.append("off"); - if ((iter + 1) != lights.end()) - { - actionsetDesc.append("*"); - } + cout << "ActionSet :: " << actionsetDesc << endl; + rep.setValue("ActionSet", actionsetDesc); } - cout << "ActionSet :: " << actionsetDesc << endl; - - OCRepresentation rep; - rep.setValue("ActionSet", actionsetDesc); - if (g_resource) { g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), &onPut); } - } - else if (n == 2) - { - OCRepresentation rep; - + break; + case 2: rep.setValue("DoAction", std::string("allbulboff")); - if (g_resource) { g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), - &onPost); + &onPost); } - } - else if (n == 3) - { - OCRepresentation rep; - + break; + case 3: rep.setValue("GetActionSet", std::string("allbulboff")); - if (g_resource) { g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), - &onPost); + &onPost); } - } - else if (n == 4) - { - OCRepresentation rep; - + break; + case 4: rep.setValue("DelActionSet", std::string("allbulboff")); - if (g_resource) { g_resource->put("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(), - &onPut); + &onPut); } - } - else if (n == 5) - { - isRun = false; break; - } + default: + cout << "Incorrect option" << endl; + break; - fflush(stdin); } + fflush(stdin); } } catch (OCException& e) diff --git a/resource/examples/groupserver.cpp b/resource/examples/groupserver.cpp index 4cebd44..bcf5eca 100644 --- a/resource/examples/groupserver.cpp +++ b/resource/examples/groupserver.cpp @@ -106,16 +106,26 @@ int main() OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE); int selectedMenu; - while (true) + bool isRun = true; + while (isRun) { + cout << endl + << "0 :: Quit 1 :: UNREGISTER RESOURCES\n" << endl; + std::cin >> selectedMenu; - if (selectedMenu == 1) + switch(selectedMenu) { - for (unsigned int i = 0; i < resourceHandleVector.size(); ++i) - { - OCPlatform::unregisterResource(resourceHandleVector.at(i)); - } + case 0: + isRun = false; + break; + case 1: + std::cout << "Unregistering resources" << std::endl; + for (unsigned int i = 0; i < resourceHandleVector.size(); ++i) + { + OCPlatform::unregisterResource(resourceHandleVector.at(i)); + } + break; } } diff --git a/resource/examples/simpleserver.cpp b/resource/examples/simpleserver.cpp index a4146c6..2411711 100644 --- a/resource/examples/simpleserver.cpp +++ b/resource/examples/simpleserver.cpp @@ -523,9 +523,11 @@ int main(int argc, char* argv[]) // Invoke createResource function of class light. myLight.createResource(); + std::cout << "Created resource." << std::endl; myLight.addType(std::string("core.brightlight")); myLight.addInterface(std::string("oc.mi.ll")); + std::cout << "Added Interface and Type" << std::endl; // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we @@ -534,6 +536,7 @@ int main(int argc, char* argv[]) std::mutex blocker; std::condition_variable cv; std::unique_lock lock(blocker); + std::cout <<"Waiting" << std::endl; cv.wait(lock); } catch(OCException e) diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 79f88a4..2f7f0b0 100644 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -219,6 +219,7 @@ namespace OC delete context; result = OC_STACK_ERROR; } + return result; } @@ -273,8 +274,10 @@ namespace OC } else { + delete context; result = OC_STACK_ERROR; } + return result; } -- 2.7.4