Resolved Klocwork warnings for C++ Stack & Samples
authorShilpa Sodani <shilpa.a.sodani@intel.com>
Wed, 14 Jan 2015 23:56:47 +0000 (15:56 -0800)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Fri, 23 Jan 2015 21:49:25 +0000 (21:49 +0000)
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 <shilpa.a.sodani@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/141
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
Reviewed-by: Sudarshan Prasad <sudarshan.prasad@intel.com>
resource/examples/groupclient.cpp
resource/examples/groupserver.cpp
resource/examples/simpleserver.cpp
resource/src/InProcClientWrapper.cpp

index 11afb80faa1816ce1f63000a0fafaead84b5fb49..34f325fd3593292d2a218f4328f5d1682ed20c1b 100644 (file)
 
 #include "OCPlatform.h"
 #include "OCApi.h"
+#include "logger.h"
 
 #include <functional>
 #include <pthread.h>
+#include <mutex>
+#include <condition_variable>
 #include <iostream>
 
 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<std::mutex> 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)
index 4cebd44b17a2da58abd28e1808ad6330e62180f8..bcf5eca7d7a8e6d4ec712a8cbc6b121e9fafa111 100644 (file)
@@ -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;
             }
 
         }
index a4146c603c5921fb807bc77f146605d64c5c1311..2411711152529555868f3f485df035009e76398f 100644 (file)
@@ -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<std::mutex> lock(blocker);
+        std::cout <<"Waiting" << std::endl;
         cv.wait(lock);
     }
     catch(OCException e)
index 79f88a4fe0c5252152fd437f3f24ccea1d384e7f..2f7f0b06d6bc8ec52a03edb5199baf25dbe7597c 100644 (file)
@@ -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;
     }