Skip to content

Commit

Permalink
Resolved Klocwork warnings for C++ Stack & Samples
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-on: https://gerrit.iotivity.org/gerrit/141
Tested-by: jenkins-iotivity <[email protected]>
Reviewed-by: Erich Keane <[email protected]>
Reviewed-by: Sudarshan Prasad <[email protected]>
  • Loading branch information
sasodani authored and Sudarshan Prasad committed Jan 23, 2015
1 parent 6baf3d3 commit f3b534a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 62 deletions.
124 changes: 68 additions & 56 deletions resource/examples/groupclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@

#include "OCPlatform.h"
#include "OCApi.h"
#include "logger.h"

#include <functional>
#include <pthread.h>
#include <mutex>
#include <condition_variable>
#include <iostream>

using namespace std;
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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 16 additions & 6 deletions resource/examples/groupserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Expand Down
3 changes: 3 additions & 0 deletions resource/examples/simpleserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions resource/src/InProcClientWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ namespace OC
delete context;
result = OC_STACK_ERROR;
}

return result;
}

Expand Down Expand Up @@ -273,8 +274,10 @@ namespace OC
}
else
{
delete context;
result = OC_STACK_ERROR;
}

return result;
}

Expand Down

0 comments on commit f3b534a

Please sign in to comment.