#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
// This variable determines instance number of the LED resource.
// Used by POST method to create a new instance of LED resource.
static int gCurrLedInstance = 0;
-#define SAMPLE_MAX_NUM_POST_INSTANCE 2
+#define SAMPLE_MAX_NUM_POST_INSTANCE 2
static LEDResource gLedInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
char *gResourceUri= (char *)"/a/led";
-static uint16_t OC_WELL_KNOWN_PORT = 5683;
+OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
+{
+ 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;
+}
//This function takes the request as an input and returns the response
-//in JSON format.
-char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
+OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
{
- cJSON *json = cJSON_CreateObject();
- cJSON *format;
- char *jsonResponse;
+ if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
+ {
+ OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
+ return nullptr;
+ }
+
+ 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);
- currLEDResource->state = ( !strcmp(cJSON_GetObjectItem(putJson,"state")->valuestring ,
- "on") ? true:false);
- currLEDResource->power = cJSON_GetObjectItem(putJson,"power")->valuedouble;
- cJSON_Delete(putJson);
- }
+ // Get pointer to query
+ int64_t pow;
+ if(OCRepPayloadGetPropInt(input, "power", &pow))
+ {
+ currLEDResource->power =pow;
+ }
- cJSON_AddStringToObject(json,"href",gResourceUri);
- cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
- cJSON_AddStringToObject(format, "state", (char *) (currLEDResource->state ? "on":"off"));
- cJSON_AddNumberToObject(format, "power", currLEDResource->power);
+ bool state;
+ if(OCRepPayloadGetPropBool(input, "state", &state))
+ {
+ currLEDResource->state = state;
+ }
+ }
- 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 (maxPayloadSize > strlen ((char *)getResp))
+ if(getResp)
{
- strncpy(payload, getResp, strlen((char *)getResp));
+ *payload = 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);
-
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 (maxPayloadSize > strlen ((char *)putResp))
+ if(putResp)
{
- strncpy(payload, putResp, strlen((char *)putResp));
+ *payload = 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);
-
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();
-
- cJSON_AddStringToObject(json,"href",gResourceUri);
- cJSON_AddItemToObject(json, "rep", format=cJSON_CreateObject());
- 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};
- if (flag & OC_INIT_FLAG)
+ // Validate pointer
+ if (!entityHandlerRequest)
{
- OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG");
- ehResult = OC_EH_OK;
+ OC_LOG (ERROR, TAG, "Invalid request pointer");
+ return OC_EH_ERROR;
}
+
+ OCRepPayload* payload = nullptr;
+
if (flag & OC_REQUEST_FLAG)
{
OC_LOG (INFO, TAG, "Flag includes 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 = (unsigned char *)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;
}
int main(int argc, char* argv[])
{
- uint8_t addr[20] = {0};
- uint8_t* paddr = NULL;
- uint16_t port = OC_WELL_KNOWN_PORT;
- uint8_t ifname[] = "eth0";
-
OC_LOG(DEBUG, TAG, "OCServer is starting...");
- /*Get Ip address on defined interface and initialize coap on it with random port number
- * this port number will be used as a source port in all coap communications*/
- if ( OCGetInterfaceAddress(ifname, sizeof(ifname), AF_INET, addr,
- sizeof(addr)) == ERR_SUCCESS)
- {
- OC_LOG_V(INFO, TAG, "Starting ocserver on address %s:%d",addr,port);
- paddr = addr;
- }
-
- if (OCInit((char *) paddr, port, OC_SERVER) != OC_STACK_OK)
+ if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
{
OC_LOG(ERROR, TAG, "OCStack init error");
return 0;
ledResource->power= resourcePower;
OCStackResult res = OCCreateResource(&(ledResource->handle),
"core.led",
- "oc.mi.def",
+ OC_RSRVD_INTERFACE_DEFAULT,
uri,
OCEntityHandlerCb,
+ NULL,
OC_DISCOVERABLE|OC_OBSERVABLE);
OC_LOG_V(INFO, TAG, "Created LED resource with result: %s", getResult(res));
return 0;
}
+