This was previously submitted as review # 330 but ran into issues during rebase. Abandoning 330 and creating this one. Review comments in 330 have been addressed.
Change-Id: I3f39718975421a9e8e22e953623a81b9c7aac623
// Internal function to create OCEntityHandlerRequest at the server from a received coap pdu
OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequestLoc,
OCMethod method, unsigned char * resBuf, unsigned char * bufReqPayload,
- unsigned char * queryBuf);
+ unsigned char * queryBuf, unsigned char *newResUriBuf);
// Internal function to retrieve Uri and Query from received coap pdu
OCStackResult ParseCoAPPdu(coap_pdu_t * pdu, unsigned char * uriBuf,
unsigned char rcvdUri[MAX_URI_LENGTH] = { 0 };
unsigned char rcvdQuery[MAX_QUERY_LENGTH] = { 0 };
unsigned char bufRes[MAX_RESPONSE_LENGTH] = { 0 };
+ unsigned char newResourceUri[MAX_RESPONSE_LENGTH] = { 0 };
uint8_t * rcvObserveOption = NULL;
unsigned char * bufReqPayload = NULL;
uint32_t observeOption = OC_RESOURCE_NO_OBSERVE;
// fill OCEntityHandlerRequest structure
result = FormOCEntityHandlerRequest(&entityHandlerRequest, ocMethod,
- bufRes, bufReqPayload, rcvdQuery);
+ bufRes, bufReqPayload, rcvdQuery, newResourceUri);
VERIFY_SUCCESS(result, OC_STACK_OK);
// fill OCObserveReq
}
else
{
- result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
- 0, NULL, 0, NULL,
- request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
- request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+ if (responseResult == OC_STACK_RESOURCE_CREATED)
+ {
+ result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
+ strlen((char *)newResourceUri), newResourceUri, 0, NULL,
+ request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
+ request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+ }
+ else
+ {
+ result = FormOptionList(&optList, &mediaType, &maxAge, 0, NULL, NULL,
+ 0, NULL, 0, NULL,
+ request->entityHandlerRequest->sendVendorSpecificHeaderOptions,
+ request->entityHandlerRequest->numSendVendorSpecificHeaderOptions);
+ }
}
VERIFY_SUCCESS(result, OC_STACK_OK);
ret = COAP_RESPONSE_200;
break;
+ case OC_STACK_RESOURCE_CREATED:
+ ret = COAP_RESPONSE_201;
+ break;
+
case OC_STACK_RESOURCE_DELETED:
ret = COAP_RESPONSE_202;
break;
ret = OC_STACK_OK;
break;
+ case COAP_RESPONSE_201 :
+ ret = OC_STACK_RESOURCE_CREATED;
+ break;
+
case COAP_RESPONSE_202 :
ret = OC_STACK_RESOURCE_DELETED;
break;
// Form the OCEntityHandlerRequest struct
OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequestLoc,
OCMethod method, unsigned char * resBuf, unsigned char * bufReqPayload,
- unsigned char * queryBuf)
+ unsigned char * queryBuf, unsigned char *newResUriBuf)
{
if (entityHandlerRequestLoc)
{
entityHandlerRequestLoc->resJSONPayloadLen = MAX_RESPONSE_LENGTH;
entityHandlerRequestLoc->obsInfo = NULL;
+ entityHandlerRequestLoc->newResourceUri = newResUriBuf;
return OC_STACK_OK;
}
* Declares Stack Results & Errors
*/
typedef enum {
+ /* Success status code - START HERE */
OC_STACK_OK = 0,
+ OC_STACK_RESOURCE_CREATED,
+ OC_STACK_RESOURCE_DELETED,
+ /* Success status code - END HERE */
+ /* Error status code - START HERE */
OC_STACK_INVALID_URI,
OC_STACK_INVALID_QUERY,
OC_STACK_INVALID_IP,
OC_STACK_OBSERVER_NOT_FOUND,
OC_STACK_OBSERVER_NOT_ADDED,
OC_STACK_OBSERVER_NOT_REMOVED,
- OC_STACK_RESOURCE_DELETED,
#ifdef WITH_PRESENCE
OC_STACK_PRESENCE_STOPPED,
OC_STACK_PRESENCE_DO_NOT_HANDLE,
OC_STACK_INVALID_OPTION,
OC_STACK_MALFORMED_RESPONSE, /* the remote reply contained malformed data */
OC_STACK_ERROR
+ /* Error status code - END HERE */
} OCStackResult;
/**
// An array of the vendor specific header options the entity handler wishes to use in response
uint8_t numSendVendorSpecificHeaderOptions;
OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
+ // URI of new resource that entity handler might create
+ unsigned char *newResourceUri;
+
}OCEntityHandlerRequest;
/**
typedef enum {
OC_EH_OK = 0,
OC_EH_ERROR,
+ OC_EH_RESOURCE_CREATED,
OC_EH_RESOURCE_DELETED,
OC_EH_FORBIDDEN
} OCEntityHandlerResult;
switch (result) {
case OC_STACK_OK:
return "OC_STACK_OK";
+ case OC_STACK_RESOURCE_CREATED:
+ return "OC_STACK_RESOURCE_CREATED";
+ case OC_STACK_RESOURCE_DELETED:
+ return "OC_STACK_RESOURCE_DELETED";
case OC_STACK_INVALID_URI:
return "OC_STACK_INVALID_URI";
case OC_STACK_INVALID_QUERY:
return "OC_STACK_SLOW_RESOURCE";
case OC_STACK_NO_OBSERVERS:
return "OC_STACK_NO_OBSERVERS";
- case OC_STACK_RESOURCE_DELETED:
- return "OC_STACK_RESOURCE_DELETED";
#ifdef WITH_PRESENCE
case OC_STACK_PRESENCE_DO_NOT_HANDLE:
return "OC_STACK_PRESENCE_DO_NOT_HANDLE";
}
}
-void ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
+OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest)
{
+ OCEntityHandlerResult ehResult = OC_EH_OK;
const char respPLPost_newLed[] = "{\"href\":\"\",\"rep\":{\"createduri\":\"\"}}";
char *respPLPost_led;
cJSON *json;
// Create new LED instance
char newLedUri[15] = "/a/led/";
sprintf (newLedUri + strlen(newLedUri), "%d", gCurrLedInstance);
- printf ("\n New resource URI: %s\n", newLedUri);
+ OC_LOG_V (INFO, TAG, "New resource URI: %s", newLedUri);
json = cJSON_Parse((char *)respPLPost_newLed);
cJSON_GetObjectItem(json,"href")->valuestring = resourceUri;
gLedInstance[gCurrLedInstance].power = 0;
gCurrLedInstance++;
respPLPost_led = cJSON_Print(json);
+ strncpy ((char *)ehRequest->newResourceUri, newLedUri, MAX_URI_LENGTH);
+ ehResult = OC_EH_RESOURCE_CREATED;
}
json = cJSON_Parse((char *)respPLPost_newLed);
OC_LOG_V (INFO, TAG, "Response buffer: %d bytes is too small",
ehRequest->resJSONPayloadLen);
}
+ return ehResult;
}
OCEntityHandlerResult ProcessDeleteRequest (OCEntityHandlerRequest *ehRequest)
else if (OC_REST_POST == entityHandlerRequest->method)
{
OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
- ProcessPostRequest (entityHandlerRequest);
+ ehResult = ProcessPostRequest (entityHandlerRequest);
}
else if (OC_REST_DELETE == entityHandlerRequest->method)
{
observer = GetObserverUsingToken (token);
if(observer)
{
- FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, NULL, NULL);
+ FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes,
+ NULL, NULL, NULL);
ehRequest.obsInfo = &observationInfo;
ehRequest.obsInfo->action = OC_OBSERVE_DEREGISTER;
ehRequest.obsInfo->obsId = observer->observeId;
{
if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
{
- FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes, NULL, NULL);
+ FormOCEntityHandlerRequest(&ehRequest, OC_REST_CANCEL_OBSERVE, bufRes,
+ NULL, NULL, NULL);
ehRequest.obsInfo = &observationInfo;
ehRequest.obsInfo->action = OC_OBSERVE_DEREGISTER;
ehRequest.obsInfo->obsId = observer->observeId;
// Invoke the entity handler for the client to process
// the query according to the new representation
FormOCEntityHandlerRequest(&entityHandlerReq, OC_REST_GET, bufRes,
- NULL, resourceObserver->query);
+ NULL, resourceObserver->query, NULL);
entityHandlerReq.resource = (OCResourceHandle)resPtr;
// Even if entity handler for a resource is not successful
case OC_EH_FORBIDDEN:
result = OC_STACK_RESOURCE_ERROR;
break;
+ case OC_EH_RESOURCE_CREATED:
+ result = OC_STACK_RESOURCE_CREATED;
+ break;
case OC_EH_RESOURCE_DELETED:
result = OC_STACK_NO_RESOURCE;
break;
}
}
- if (result == OC_STACK_OK)
+ if (result == OC_STACK_OK || OC_STACK_RESOURCE_CREATED)
{
ehRequest->resJSONPayloadLen = ehRequest->resJSONPayloadLen -
strlen((char*)ehRequest->resJSONPayload);