#include <pthread.h>
#include "ocstack.h"
#include "logger.h"
-#include "cJSON.h"
#include "ocserverbasicops.h"
+#include "ocpayload.h"
//string length of "/a/led/" + std::numeric_limits<int>::digits10 + '\0'"
// 7 + 9 + 1 = 17
char *gResourceUri= (char *)"/a/led";
-//This function takes the request as an input and returns the response
-//in JSON format.
-char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
+OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
{
- cJSON *json = cJSON_CreateObject();
+ OCRepPayload* payload = OCRepPayloadCreate();
+ if(!payload)
+ {
+ OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload"));
+ return nullptr;
+ }
+
+ OCRepPayloadSetUri(payload, uri);
+ OCRepPayloadSetPropBool(payload, "state", state);
+ OCRepPayloadSetPropInt(payload, "power", power);
+
+ return payload;
+}
- if(!json)
+//This function takes the request as an input and returns the response
+OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
+{
+ if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
{
- OC_LOG (ERROR, TAG, "json object not created properly");
- return NULL;
+ OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
+ return nullptr;
}
- cJSON *format;
- char *jsonResponse;
+ OCRepPayload* input = reinterpret_cast<OCRepPayload*>(ehRequest->payload);
+
LEDResource *currLEDResource = &LED;
if (ehRequest->resource == gLedInstance[0].handle)
{
currLEDResource = &gLedInstance[0];
- gResourceUri = (char *) "a/led/0";
+ gResourceUri = (char *) "/a/led/0";
}
else if (ehRequest->resource == gLedInstance[1].handle)
{
currLEDResource = &gLedInstance[1];
- gResourceUri = (char *) "a/led/1";
+ gResourceUri = (char *) "/a/led/1";
}
if(OC_REST_PUT == ehRequest->method)
{
- cJSON *putJson = cJSON_Parse((char *)ehRequest->reqJSONPayload);
-
- if(!putJson)
+ // Get pointer to query
+ int64_t pow;
+ if(OCRepPayloadGetPropInt(input, "power", &pow))
{
- OC_LOG (ERROR, TAG, "putJson object not created properly");
- cJSON_Delete(json);
- return NULL;
+ currLEDResource->power =pow;
}
- 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);
- format = cJSON_CreateObject();
- if(!format)
- {
- OC_LOG (ERROR, TAG, "format object not created properly");
- cJSON_Delete(json);
- return NULL;
+ bool state;
+ if(OCRepPayloadGetPropBool(input, "state", &state))
+ {
+ currLEDResource->state = state;
+ }
}
- cJSON_AddItemToObject(json, "rep", format);
- cJSON_AddStringToObject(format, "state", (char *) (currLEDResource->state ? "on":"off"));
- cJSON_AddNumberToObject(format, "power", currLEDResource->power);
-
- jsonResponse = cJSON_Print(json);
- cJSON_Delete(json);
- return jsonResponse;
+ return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
}
-OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest, char *payload,
- uint16_t maxPayloadSize)
+OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
+ OCRepPayload **payload)
{
OCEntityHandlerResult ehResult;
- char *getResp = constructJsonResponse(ehRequest);
+ OCRepPayload *getResp = constructResponse(ehRequest);
if(getResp)
{
- if (maxPayloadSize > strlen ((char *)getResp))
- {
- strncpy(payload, getResp, strlen((char *)getResp));
- ehResult = OC_EH_OK;
- }
- else
- {
- OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
- maxPayloadSize);
- ehResult = OC_EH_ERROR;
- }
-
- free(getResp);
- }
+ *payload = getResp;
+ ehResult = OC_EH_OK;
+ }
else
{
ehResult = OC_EH_ERROR;
return ehResult;
}
-OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest, char *payload,
- uint16_t maxPayloadSize)
+OCEntityHandlerResult ProcessPutRequest (OCEntityHandlerRequest *ehRequest,
+ OCRepPayload **payload)
{
OCEntityHandlerResult ehResult;
- char *putResp = constructJsonResponse(ehRequest);
+ OCRepPayload *putResp = constructResponse(ehRequest);
if(putResp)
{
- if (maxPayloadSize > strlen ((char *)putResp))
- {
- strncpy(payload, putResp, strlen((char *)putResp));
- ehResult = OC_EH_OK;
- }
- else
- {
- OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
- maxPayloadSize);
- ehResult = OC_EH_ERROR;
- }
-
- free(putResp);
+ *payload = putResp;
+ ehResult = OC_EH_OK;
}
else
{
return ehResult;
}
-OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, char *payload,
- uint16_t maxPayloadSize)
+OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
+ OCEntityHandlerResponse *response, OCRepPayload **payload)
{
- char *respPLPost_led = NULL;
- cJSON *json;
- cJSON *format;
- OCEntityHandlerResult ehResult;
+ OCRepPayload *respPLPost_led = nullptr;
+ OCEntityHandlerResult ehResult = OC_EH_OK;
/*
* The entity handler determines how to process a POST request.
char newLedUri[URI_MAXSIZE ];
snprintf(newLedUri, URI_MAXSIZE, "/a/led/%d", gCurrLedInstance);
- json = cJSON_CreateObject();
- if(!json)
- {
- return OC_EH_ERROR;
- }
-
- cJSON_AddStringToObject(json,"href",gResourceUri);
- format = cJSON_CreateObject();
-
- if(!format)
- {
- return OC_EH_ERROR;
- }
-
- cJSON_AddItemToObject(json, "rep", format);
- cJSON_AddStringToObject(format, "createduri", (char *) newLedUri);
+ respPLPost_led = OCRepPayloadCreate();
+ OCRepPayloadSetUri(respPLPost_led, gResourceUri);
+ OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri);
if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0))
{
gLedInstance[gCurrLedInstance].state = 0;
gLedInstance[gCurrLedInstance].power = 0;
gCurrLedInstance++;
- respPLPost_led = cJSON_Print(json);
+ strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
+ ehResult = OC_EH_RESOURCE_CREATED;
}
-
- cJSON_Delete(json);
}
else
{
- respPLPost_led = constructJsonResponse(ehRequest);
+ respPLPost_led = constructResponse(ehRequest);
}
}
else
{
if (i == 0)
{
- respPLPost_led = constructJsonResponse(ehRequest);
+ respPLPost_led = constructResponse(ehRequest);
break;
}
else if (i == 1)
{
- respPLPost_led = constructJsonResponse(ehRequest);
+ respPLPost_led = constructResponse(ehRequest);
}
}
}
}
- if ((respPLPost_led != NULL) && (maxPayloadSize > strlen ((char *)respPLPost_led)))
+ if ((respPLPost_led != NULL))
{
- strncpy(payload, respPLPost_led, strlen((char *)respPLPost_led));
+ *payload = respPLPost_led;
ehResult = OC_EH_OK;
}
else
{
- OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
- maxPayloadSize);
+ OC_LOG_V (INFO, TAG, "Payload was NULL");
ehResult = OC_EH_ERROR;
}
- free(respPLPost_led);
-
return ehResult;
}
OCEntityHandlerResult
OCEntityHandlerCb (OCEntityHandlerFlag flag,
- OCEntityHandlerRequest *entityHandlerRequest)
+ OCEntityHandlerRequest *entityHandlerRequest,void* callbackParam)
{
OC_LOG_V (INFO, TAG, "Inside entity handler - flags: 0x%x", flag);
OCEntityHandlerResult ehResult = OC_EH_ERROR;
OCEntityHandlerResponse response;
- char payload[MAX_RESPONSE_LENGTH] = {0};
+
+ // Validate pointer
+ if (!entityHandlerRequest)
+ {
+ OC_LOG (ERROR, TAG, "Invalid request pointer");
+ return OC_EH_ERROR;
+ }
+
+ OCRepPayload* payload = nullptr;
if (flag & OC_REQUEST_FLAG)
{
if (OC_REST_GET == entityHandlerRequest->method)
{
OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
- ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ ehResult = ProcessGetRequest (entityHandlerRequest, &payload);
}
else if (OC_REST_PUT == entityHandlerRequest->method)
{
OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
- ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ ehResult = ProcessPutRequest (entityHandlerRequest, &payload);
}
else if (OC_REST_POST == entityHandlerRequest->method)
{
OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
- ehResult = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ ehResult = ProcessPostRequest (entityHandlerRequest, &response, &payload);
}
else
{
entityHandlerRequest->method);
}
- if (ehResult == OC_EH_OK)
+ if (ehResult == OC_EH_OK && ehResult != OC_EH_FORBIDDEN)
{
// Format the response. Note this requires some info about the request
response.requestHandle = entityHandlerRequest->requestHandle;
response.resourceHandle = entityHandlerRequest->resource;
response.ehResult = ehResult;
- response.payload = payload;
- response.payloadSize = strlen(payload);
+ response.payload = reinterpret_cast<OCPayload*>(payload);
response.numSendVendorSpecificHeaderOptions = 0;
memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
memset(response.resourceUri, 0, sizeof(response.resourceUri));
}
}
}
+
+ OCPayloadDestroy(response.payload);
return ehResult;
}
OC_RSRVD_INTERFACE_DEFAULT,
uri,
OCEntityHandlerCb,
+ NULL,
OC_DISCOVERABLE|OC_OBSERVABLE);
OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));