From 16aaed92f74fedf975a1631800200b8ffc526f7d Mon Sep 17 00:00:00 2001 From: Sachin Agrawal Date: Mon, 5 Jan 2015 15:33:29 -0800 Subject: [PATCH] Fixed seg fault in simpleclient sample app (Bug IOT-130) Arduino server sample uses light representation which is different from what simpleclient expects. Updated simpleclient to catch exception in this scenario. Change-Id: I3d5d2e2f7785a9345655bc347662efb7fd0927a2 Signed-off-by: Sachin Agrawal --- resource/examples/simpleclient.cpp | 227 +++++++++++++++++++++---------------- 1 file changed, 132 insertions(+), 95 deletions(-) diff --git a/resource/examples/simpleclient.cpp b/resource/examples/simpleclient.cpp index 85c0dc1..619f01d 100644 --- a/resource/examples/simpleclient.cpp +++ b/resource/examples/simpleclient.cpp @@ -58,112 +58,135 @@ int observe_count() void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, const int& eCode, const int& sequenceNumber) { - if(eCode == OC_STACK_OK) + try { - std::cout << "OBSERVE RESULT:"< 30) - { - std::cout<<"Cancelling Observe..."<cancelObserve(); + if(observe_count() > 30) + { + std::cout<<"Cancelling Observe..."<cancelObserve(); - std::cout << "Cancel result: "<< result <("createduri") << std::endl; - } - else + if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) { - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + std::cout << "POST request was successful" << std::endl; - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; - } + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); + + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } - if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) - std::cout << endl << "Observe is used." << endl << endl; - else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll) - std::cout << endl << "ObserveAll is used." << endl << endl; + if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) + std::cout << endl << "Observe is used." << endl << endl; + else if (OBSERVE_TYPE_TO_USE == ObserveType::ObserveAll) + std::cout << endl << "ObserveAll is used." << endl << endl; - curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve); + curResource->observe(OBSERVE_TYPE_TO_USE, QueryParamsMap(), &onObserve); + } + else + { + std::cout << "onPost2 Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPost2 Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPost2" << std::endl; } + } void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) { - if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) + try { - std::cout << "POST request was successful" << std::endl; - - if(rep.hasAttribute("createduri")) + if(eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED) { - std::cout << "\tUri of the created resource: " - << rep.getValue("createduri") << std::endl; - } - else - { - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + std::cout << "POST request was successful" << std::endl; - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; - } + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - OCRepresentation rep2; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } - std::cout << "Posting light representation..."<post(rep2, QueryParamsMap(), &onPost2); + rep2.setValue("state", mylight.m_state); + rep2.setValue("power", mylight.m_power); + + curResource->post(rep2, QueryParamsMap(), &onPost2); + } + else + { + std::cout << "onPost Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPost Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPost" << std::endl; } } @@ -190,24 +213,31 @@ void postLightRepresentation(std::shared_ptr resource) // callback handler on PUT request void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) { - if(eCode == OC_STACK_OK) + try { - std::cout << "PUT request was successful" << std::endl; + if(eCode == OC_STACK_OK) + { + std::cout << "PUT request was successful" << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; - postLightRepresentation(curResource); + postLightRepresentation(curResource); + } + else + { + std::cout << "onPut Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onPut Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onPut" << std::endl; } } @@ -234,25 +264,32 @@ void putLightRepresentation(std::shared_ptr resource) // Callback handler on GET request void onGet(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) { - if(eCode == OC_STACK_OK) + try { - std::cout << "GET request was successful" << std::endl; - std::cout << "Resource URI: " << rep.getUri() << std::endl; + if(eCode == OC_STACK_OK) + { + std::cout << "GET request was successful" << std::endl; + std::cout << "Resource URI: " << rep.getUri() << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; - putLightRepresentation(curResource); + putLightRepresentation(curResource); + } + else + { + std::cout << "onGET Response error: " << eCode << std::endl; + std::exit(-1); + } } - else + catch(std::exception& e) { - std::cout << "onGET Response error: " << eCode << std::endl; - std::exit(-1); + std::cout << "Exception: " << e.what() << " in onGet" << std::endl; } } -- 2.7.4