}
cJSON *format;
- char *jsonResponse;
+ cJSON *putJson = NULL;
+ char *jsonResponse = NULL;
LEDResource *currLEDResource = &LED;
if (ehRequest->resource == gLedInstance[0].handle)
if(OC_REST_PUT == ehRequest->method)
{
- cJSON *putJson = cJSON_Parse((char *)ehRequest->reqJSONPayload);
+ cJSON* jsonObj = NULL;
+ putJson = cJSON_Parse(ehRequest->reqJSONPayload);
+ if(putJson)
+ {
+ jsonObj = cJSON_GetObjectItem(putJson,"oic");
+ if (jsonObj)
+ {
+ jsonObj = cJSON_GetArrayItem(jsonObj, 0);
+ if (jsonObj)
+ {
+ jsonObj = cJSON_GetObjectItem(jsonObj, "rep");
+ }
+ }
+ }
+ if (NULL == jsonObj)
+ {
+ OC_LOG_V(ERROR, TAG, "Failed to parse JSON: %s", ehRequest->reqJSONPayload);
+ goto exit;
+ }
- if(!putJson)
+ cJSON* prop = cJSON_GetObjectItem(jsonObj,"power");
+ if (prop)
{
- OC_LOG (ERROR, TAG, "putJson object not created properly");
- cJSON_Delete(json);
- return NULL;
+ currLEDResource->power =prop->valueint;
+ }
+
+ prop = cJSON_GetObjectItem(jsonObj,"state");
+ if (prop)
+ {
+ currLEDResource->state = prop->valueint;
}
- currLEDResource->state = ( !strcmp(cJSON_GetObjectItem(putJson,"state")->valuestring ,
- "on") ? true:false);
- currLEDResource->power = cJSON_GetObjectItem(putJson,"power")->valuedouble;
- cJSON_Delete(putJson);
}
cJSON_AddStringToObject(json,"href",gResourceUri);
if(!format)
{
OC_LOG (ERROR, TAG, "format object not created properly");
- cJSON_Delete(json);
- return NULL;
+ goto exit;
}
cJSON_AddItemToObject(json, "rep", format);
cJSON_AddNumberToObject(format, "power", currLEDResource->power);
jsonResponse = cJSON_Print(json);
+
+exit:
+ cJSON_Delete(putJson);
cJSON_Delete(json);
return jsonResponse;
}