Patch 1-3: Implementation.
Patch 4-5: Review comments from Doug and Joey.
Patch 6: Joey -> Resolved 'defaultDeviceHandler' declaration
down to one instance.
Patch 7: Reverted Patch 6.
Patch 8: Removed the declaration from ocresource.c
Change-Id: Ic1a47f65ae6772ecf417fa5f7dcb384db029a4b7
OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER,
OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER,
OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER,
+ OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER,
OC_RESOURCE_NOT_SPECIFIED
} ResourceHandling;
extern "C" {
#endif // __cplusplus
+
+//-----------------------------------------------------------------------------
+// Global variables
+//-----------------------------------------------------------------------------
+extern OCEntityHandler defaultDeviceHandler;
+
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
OCStackResult OCStopPresence();
#endif
+
+
+/**
+ * Set default device entity handler
+ *
+ * @param entityHandler - entity handler function that is called by ocstack to handle requests for
+ * any undefined resources or default actions.
+ * if NULL is passed it removes the device default entity handler.
+ *
+ * @return
+ * OC_STACK_OK - no errors
+ * OC_STACK_ERROR - stack process error
+ */
+OCStackResult OCSetDefaultDeviceEntityHandler(OCEntityHandler entityHandler);
+
/**
* Create a resource.
*
if (clientStillObserving == false)
gLEDUnderObservation = 0;
}
+OCEntityHandlerResult
+OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
+ OCEntityHandlerRequest *entityHandlerRequest)
+{
+ const char* typeOfMessage;
+
+ OC_LOG_V (INFO, TAG, "Inside device default entity handler - flags: 0x%x", flag);
+ if (flag & OC_INIT_FLAG)
+ {
+ OC_LOG (INFO, TAG, "Flag includes OC_INIT_FLAG");
+ }
+ if (flag & OC_REQUEST_FLAG)
+ {
+ OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
+ if (entityHandlerRequest)
+ {
+ if (OC_REST_GET == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
+ ProcessGetRequest (entityHandlerRequest);
+ }
+ else if (OC_REST_PUT == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
+ ProcessPutRequest (entityHandlerRequest);
+ }
+ else
+ {
+ OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+ entityHandlerRequest->method);
+ }
+ }
+ }
+ if (flag & OC_OBSERVE_FLAG)
+ {
+ OC_LOG(INFO, TAG, "Flag includes OC_OBSERVE_FLAG");
+ if (entityHandlerRequest)
+ {
+ if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo->action)
+ {
+ OC_LOG (INFO, TAG, "Received OC_OBSERVE_REGISTER from client");
+ }
+ else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo->action)
+ {
+ OC_LOG (INFO, TAG, "Received OC_OBSERVE_DEREGISTER from client");
+ }
+ }
+ }
+
+ return OC_EH_OK;
+}
+
OCEntityHandlerResult
OCEntityHandlerCb (OCEntityHandlerFlag flag,
return 0;
}
#endif
+
+ OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandlerCb);
+
/*
* Declare and create the example resource: LED
*/
*resource = resourcePtr;
if (!resourcePtr)
{
+ if(defaultDeviceHandler)
+ {
+ *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
+ return OC_STACK_OK;
+ }
+
// Resource does not exist
+ // and default device handler does not exist
return OC_STACK_NO_RESOURCE;
}
return result;
}
+static OCStackResult
+HandleDefaultDeviceEntityHandler (OCRequest *request)
+{
+ OCStackResult result = OC_STACK_OK;
+ OCEntityHandlerRequest *ehRequest = request->entityHandlerRequest;
+
+ // At this point we know for sure that defaultDeviceHandler exists
+ defaultDeviceHandler(OC_REQUEST_FLAG, ehRequest);
+
+ ehRequest->resJSONPayloadLen = ehRequest->resJSONPayloadLen -
+ strlen((char*)ehRequest->resJSONPayload);
+ ehRequest->resJSONPayload += strlen((char*)ehRequest->resJSONPayload);
+
+ return result;
+}
static OCStackResult
HandleResourceWithEntityHandler (OCRequest *request,
break;
}
+ case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
+ {
+ ret = HandleDefaultDeviceEntityHandler(request);
+ break;
+ }
case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
{
OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
uint8_t PresenceTimeOutSize = 4;
uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
#endif
+
OCMode myStackMode;
+OCEntityHandler defaultDeviceHandler;
//-----------------------------------------------------------------------------
// Macros
break;
}
myStackMode = mode;
+
+ defaultDeviceHandler = NULL;
+
// Make call to OCCoAP layer
result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
if (result == OC_STACK_OK)
}
#endif
+
+OCStackResult OCSetDefaultDeviceEntityHandler(OCEntityHandler entityHandler)
+{
+ defaultDeviceHandler = entityHandler;
+
+ return OC_STACK_OK;
+}
+
/**
* Create a resource
*