From b99d2f18677ef7d54880a39294d9a3bae14efe54 Mon Sep 17 00:00:00 2001 From: Mandeep Shetty Date: Fri, 13 Feb 2015 14:38:27 -0800 Subject: [PATCH] Fix C/C++ samples interoperability This fixes IOT-130 Made the "state" of light resource consistent across samples to be true/false instead of "on"/"off". Fixed JSON payload parsing in occserver to fix seg fault on PUT request. Change-Id: I4983c7ec20a274d0ae24f013cd7b7ec95c5815a6 Signed-off-by: Mandeep Shetty Reviewed-on: https://gerrit.iotivity.org/gerrit/345 Tested-by: jenkins-iotivity Reviewed-by: Sashi Penta Reviewed-by: Sudarshan Prasad --- .../arduino/SimpleClientServer/ocserver/ocserver.cpp | 4 ++-- .../stack/samples/linux/SimpleClientServer/occlient.cpp | 2 +- .../stack/samples/linux/SimpleClientServer/ocserver.cpp | 17 ++++++++++++----- resource/examples/simpleclient.cpp | 1 - 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/resource/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp b/resource/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp index a4c93f9..1591210 100644 --- a/resource/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp +++ b/resource/csdk/stack/samples/arduino/SimpleClientServer/ocserver/ocserver.cpp @@ -57,8 +57,8 @@ typedef struct LIGHTRESOURCE{ static LightResource Light; -static char responsePayloadGet[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":\"on\",\"power\":10}}"; -static char responsePayloadPut[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":\"off\",\"power\":0}}"; +static char responsePayloadGet[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":true,\"power\":10}}"; +static char responsePayloadPut[] = "{\"href\":\"/a/light\",\"rep\":{\"state\":false,\"power\":0}}"; #ifdef ARDUINOWIFI // Arduino WiFi Shield diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp index 1d0a6a6..59f9e52 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp @@ -38,7 +38,7 @@ static const char * MULTICAST_RESOURCE_DISCOVERY_QUERY = "/oc/core"; //The following variable determines the interface (wifi, ethernet etc.) //to be used for sending unicast messages. Default set to WIFI. static OCConnectivityType OC_CONNTYPE = OC_WIFI; -static std::string putPayload = "{\"state\":\"on\",\"power\":5}"; +static std::string putPayload = "{\"oc\":[{\"rep\":{\"power\":15,\"state\":true}}]}"; static std::string coapServerIP = "255.255.255.255"; static std::string coapServerPort = "5683"; static std::string coapServerResource = "/a/light"; diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp index 7800f96..5d9e6d0 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp @@ -104,16 +104,23 @@ char* constructJsonResponse (OCEntityHandlerRequest *ehRequest) if(OC_REST_PUT == ehRequest->method) { + // Get cJSON pointer to query cJSON *putJson = cJSON_Parse((char *)ehRequest->reqJSONPayload); - currLightResource->state = ( !strcmp(cJSON_GetObjectItem(putJson,"state")->valuestring, - "on") ? true:false); - currLightResource->power = cJSON_GetObjectItem(putJson,"power")->valuedouble; + + // Get root of JSON payload, then the 1st resource. + cJSON* carrier = cJSON_GetObjectItem(putJson, "oc"); + carrier = cJSON_GetArrayItem(carrier, 0); + carrier = cJSON_GetObjectItem(carrier, "rep"); + + currLightResource->power = cJSON_GetObjectItem(carrier,"power")->valueint; + currLightResource->state = cJSON_GetObjectItem(carrier,"state")->valueint; + cJSON_Delete(putJson); } cJSON_AddStringToObject(json,"href",gResourceUri); cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); - cJSON_AddStringToObject(format, "state", (char *) (currLightResource->state ? "on":"off")); + cJSON_AddBoolToObject(format, "state", currLightResource->state); cJSON_AddNumberToObject(format, "power", currLightResource->power); jsonResponse = cJSON_Print(json); @@ -671,7 +678,7 @@ void *ChangeLightRepresentation (void *param) cJSON *format; cJSON_AddStringToObject(json,"href",gResourceUri); cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject()); - cJSON_AddStringToObject(format, "state", (char *) (Light.state ? "on":"off")); + cJSON_AddBoolToObject(format, "state", Light.state); cJSON_AddNumberToObject(format, "power", Light.power); char * obsResp = cJSON_Print(json); cJSON_Delete(json); diff --git a/resource/examples/simpleclient.cpp b/resource/examples/simpleclient.cpp index a76b223..bc6e996 100644 --- a/resource/examples/simpleclient.cpp +++ b/resource/examples/simpleclient.cpp @@ -69,7 +69,6 @@ void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep, { std::cout << "OBSERVE RESULT:"<