const char *supportUrl = "mySupportUrl";
const char *version = "myVersion";
+// Entity handler should check for resourceTypeName and ResourceInterface in order to GET
+// the existence of a known resource
+const char *resourceTypeName = "core.light";
+const char *resourceInterface = "oc.mi.def";
+
OCDeviceInfo deviceInfo;
//This function takes the request as an input and returns the response
return true;
}
+/* This method check the validity of resourceTypeName and resource interfaces
+ * Entity Handler has to parse the query string in order to process it
+ */
+
+OCEntityHandlerResult ValidateQueryParams (OCEntityHandlerRequest *entityHandlerRequest)
+{
+ bool resourceList = true;
+ uint8_t resourceIndex = 0;
+ OCEntityHandlerResult ehResult = OC_EH_ERROR;
+
+ // Validate pointer
+ if (!entityHandlerRequest)
+ {
+ OC_LOG (ERROR, TAG, "Invalid request pointer");
+ return ehResult;
+ }
+ //Added check for resource type & interface in server entity handle
+
+ while(resourceList)
+ {
+ const char* typeName = OCGetResourceTypeName(entityHandlerRequest->resource,
+ resourceIndex);
+ const char* interfaceName = OCGetResourceInterfaceName(entityHandlerRequest->resource,
+ resourceIndex);
+ if(typeName && interfaceName)
+ {
+ if(strcmp(typeName,resourceTypeName) == 0 &&
+ strcmp(interfaceName,resourceInterface) == 0)
+ {
+ ehResult = OC_EH_OK;
+ break;
+ }
+ resourceIndex++;
+ }
+ else
+ {
+ resourceList = false;
+ }
+ }
+ return ehResult;
+}
+
OCEntityHandlerResult ProcessGetRequest (OCEntityHandlerRequest *ehRequest,
char *payload, uint16_t maxPayloadSize)
{
OCEntityHandlerResult ehResult;
+ char *getResp = constructJsonResponse(ehRequest);
bool queryPassed = checkIfQueryForPowerPassed(ehRequest->query);
{
OC_LOG_V (INFO, TAG, "Inside device default entity handler - flags: 0x%x, uri: %s", flag, uri);
- OCEntityHandlerResult ehResult = OC_EH_OK;
+ OCEntityHandlerResult ehResult = OC_EH_ERROR;
OCEntityHandlerResponse response;
char payload[MAX_RESPONSE_LENGTH] = {0};
OC_LOG (ERROR, TAG, "Invalid request pointer");
return OC_EH_ERROR;
}
-
// Initialize certain response fields
response.numSendVendorSpecificHeaderOptions = 0;
memset(response.sendVendorSpecificHeaderOptions, 0,
sizeof response.sendVendorSpecificHeaderOptions);
memset(response.resourceUri, 0, sizeof response.resourceUri);
+ // Entity handler to check the validity of resourceTypeName and resource interfaces
+ // It is Entity handler's responsibility to keep track of the list of resources prior to call
+ // Requested method
+ ehResult = ValidateQueryParams(entityHandlerRequest);
+
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->resource == NULL)
+ // Entity handler to check the validity of resourceType and resource interface
+ if( ehResult == OC_EH_OK )
{
- OC_LOG (INFO, TAG, "Received request from client to a non-existing resource");
- ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest,
- payload, sizeof(payload) - 1);
- }
- else if (OC_REST_GET == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
- ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
- }
- else if (OC_REST_PUT == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
- ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
- }
- else if (OC_REST_DELETE == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
- ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ if (entityHandlerRequest->resource == NULL)
+ {
+ OC_LOG (INFO, TAG, "Received request from client to a non-existing resource");
+ ehResult = ProcessNonExistingResourceRequest(entityHandlerRequest,
+ payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_GET == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
+ ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_PUT == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
+ ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_DELETE == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
+ ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else
+ {
+ OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+ entityHandlerRequest->method);
+ ehResult = OC_EH_ERROR;
+ }
}
else
{
- OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
- entityHandlerRequest->method);
- ehResult = OC_EH_ERROR;
+ OC_LOG_V (INFO, TAG,
+ "Invalid ResourceInterface Type & Name received from client for method: %d ",
+ entityHandlerRequest->method);
}
-
// If the result isn't an error or forbidden, send response
if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
{
0, sizeof response.sendVendorSpecificHeaderOptions);
memset(response.resourceUri, 0, sizeof response.resourceUri);
+ // Entity handler to check the validity of resourceTypeName and resource interfaces
+ // It is Entity handler's responsibility to keep track of the list of resources prior to call
+ // Requested method
+
+ ehResult = ValidateQueryParams(entityHandlerRequest);
+
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 (OC_REST_GET == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
- ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
- }
- else if (OC_REST_PUT == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
- ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
- }
- else if (OC_REST_POST == entityHandlerRequest->method)
- {
- OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
- ehResult = ProcessPostRequest (entityHandlerRequest,
- &response, payload, sizeof(payload) - 1);
- }
- else if (OC_REST_DELETE == entityHandlerRequest->method)
+
+ // Entity handler to check the validity of resourceType and resource interface
+ // Entity handler to check the validity of resourceType and resource interface
+ if(ehResult == OC_EH_OK)
{
- OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
- ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ if (OC_REST_GET == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_GET from client");
+ ehResult = ProcessGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_PUT == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_PUT from client");
+ ehResult = ProcessPutRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_POST == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_POST from client");
+ ehResult = ProcessPostRequest (entityHandlerRequest, &response, payload, sizeof(payload) - 1);
+ }
+ else if (OC_REST_DELETE == entityHandlerRequest->method)
+ {
+ OC_LOG (INFO, TAG, "Received OC_REST_DELETE from client");
+ ehResult = ProcessDeleteRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ }
+ else
+ {
+ OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
+ entityHandlerRequest->method);
+ ehResult = OC_EH_ERROR;
+ }
}
else
{
- OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
- entityHandlerRequest->method);
+ OC_LOG_V (INFO, TAG,
+ "Invalid ResourceInterface Type & Name received from client for method: %d ",
+ entityHandlerRequest->method);
}
-
// If the result isn't an error or forbidden, send response
if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
{
sleep(1);
res = OCCreateResource(&presenceNotificationHandles[i],
presenceNotificationResources.at(i).c_str(),
- "oc.mi.def",
+ resourceInterface,
presenceNotificationUris.at(i).c_str(),
OCNOPEntityHandlerCb,
OC_DISCOVERABLE|OC_OBSERVABLE);
lightResource->state = false;
lightResource->power= 0;
OCStackResult res = OCCreateResource(&(lightResource->handle),
- "core.light",
- "oc.mi.def",
+ resourceTypeName,
+ resourceInterface,
uri,
OCEntityHandlerCb,
OC_DISCOVERABLE|OC_OBSERVABLE);
/// Specifies whether Entity handler is going to do slow response or not
bool isSlowResponse = false;
+// Entity handler should check for resourceTypeName and ResourceInterface in order to GET
+// the existence of a known resource
+const std::string resourceTypeLight = "core.light";
+const std::string resourceInterfaceDefault = DEFAULT_INTERFACE;
+
// Forward declaring the entityHandler
/// This class represents a single resource named 'lightResource'. This resource has
/// This function internally calls registerResource API.
void createResource()
{
- std::string resourceURI = m_lightUri; //URI of the resource
- std::string resourceTypeName = "core.light"; //resource type name. In this case, it is light
- std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+ //URI of the resource
+ std::string resourceURI = m_lightUri;
+ //resource type name. In this case, it is light
+ std::string resourceTypeName = resourceTypeLight;
+ // resource interface.
+ std::string resourceInterface = resourceInterfaceDefault;
// OCResourceProperty is defined ocstack.h
uint8_t resourceProperty;
OCStackResult createResource1()
{
- std::string resourceURI = "/a/light1"; // URI of the resource
- std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light
- std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+ // URI of the resource
+ std::string resourceURI = "/a/light1";
+ // resource type name. In this case, it is light
+ std::string resourceTypeName = resourceTypeLight;
+ // resource interface.
+ std::string resourceInterface = resourceInterfaceDefault;
// OCResourceProperty is defined ocstack.h
uint8_t resourceProperty;
if(requestFlag & RequestHandlerFlag::InitFlag)
{
cout << "\t\trequestFlag : Init\n";
-
// entity handler to perform resource initialization operations
}
if(requestFlag & RequestHandlerFlag::RequestFlag)
pResponse->setRequestHandle(request->getRequestHandle());
pResponse->setResourceHandle(request->getResourceHandle());
+ // Check for query params (if any)
+ QueryParamsMap queryParamsMap = request->getQueryParameters();
+
+ // Entity handler to check the validity of resourceTypeName and resource interfaces
+ // It is Entity handler's responsibility to keep track of the list of resources prior to call
+ // Requested method
+
+ std::string interfaceName;
+ std::string typeName;
+
+ cout << "\t\t\tquery params: \n";
+
+ for(auto it : queryParamsMap)
+ {
+ cout << "\t\t\t\t" << it.first << ":" << it.second << endl;
+ std::string firstQuery = it.first;
+ if(firstQuery.find_first_of("if") == 0)
+ {
+ interfaceName = it.second;
+ }
+ else if(firstQuery.find_first_of("rt") == 0 )
+ {
+ typeName = it.second;
+ }
+ }
+ if(typeName.compare(resourceTypeLight) == 0 &&
+ interfaceName.compare(resourceInterfaceDefault) == 0)
+ {
+ ehResult = OC_EH_OK;
+ }
+ else
+ {
+ cout<< "\t\t Invalid ResourceInterface Type & Name received from Client"<<endl;
+ }
+
// If the request type is GET
- if(requestType == "GET")
+ if(requestType == "GET" && ehResult == OC_EH_OK)
{
cout << "\t\t\trequestType : GET\n";
if(isSlowResponse) // Slow response case
}
}
}
- else if(requestType == "PUT")
+ else if(requestType == "PUT" && ehResult == OC_EH_OK)
{
cout << "\t\t\trequestType : PUT\n";
OCRepresentation rep = request->getResourceRepresentation();
ehResult = OC_EH_OK;
}
}
- else if(requestType == "POST")
+ else if(requestType == "POST" && ehResult == OC_EH_OK)
{
cout << "\t\t\trequestType : POST\n";
ehResult = OC_EH_OK;
}
}
- else if(requestType == "DELETE")
+ else if(requestType == "DELETE" && ehResult == OC_EH_OK)
{
// DELETE request operations
}
return 0;
}
-