*/
bool checkIfQueryForPowerPassed(char * query)
{
- if (query && strcmp(query, "power<") == 0)
+ if (query && strncmp(query, "power<", strlen("power<")) == 0)
{
char * pointerToOperator = strstr(query, "<");
if (pointerToOperator)
{
int powerRequested = atoi(pointerToOperator + 1);
-
if (Light.power > powerRequested)
{
OC_LOG_V(INFO, TAG, "Current power: %d. Requested: <%d", Light.power
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
+/*
+ * Application should validate and process these as desired.
*/
-
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;
+ OC_LOG_V(INFO, TAG, PCF("Received query %s"), entityHandlerRequest->query);
+ OC_LOG(INFO, TAG, PCF("Not processing query"));
+ return OC_EH_OK;
}
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_ERROR;
+ OCEntityHandlerResult ehResult = OC_EH_OK;
OCEntityHandlerResponse response;
char payload[MAX_RESPONSE_LENGTH] = {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)
{
if (flag & OC_REQUEST_FLAG)
{
OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
- // Entity handler to check the validity of resourceType and resource interface
- if( ehResult == OC_EH_OK )
+
+ if (entityHandlerRequest->resource == NULL)
{
- 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;
- }
+ 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,
- "Invalid ResourceInterface Type & Name received from client for method: %d ",
+ OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
entityHandlerRequest->method);
+ ehResult = OC_EH_ERROR;
}
- // If the result isn't an error or forbidden, send response
+ // If the result isn't an error or forbidden, send response
if (!((ehResult == OC_EH_ERROR) || (ehResult == OC_EH_FORBIDDEN)))
{
// Format the response. Note this requires some info about the request
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");
{
OC_LOG (INFO, TAG, "Flag includes OC_REQUEST_FLAG");
- // 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)
+ if (OC_REST_GET == entityHandlerRequest->method)
{
- 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;
- }
+ 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,
- "Invalid ResourceInterface Type & Name received from client for method: %d ",
+ OC_LOG_V (INFO, TAG, "Received unsupported method %d from client",
entityHandlerRequest->method);
+ ehResult = OC_EH_ERROR;
}
// 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(),
- resourceInterface,
+ "oc.mi.def",
presenceNotificationUris.at(i).c_str(),
OCNOPEntityHandlerCb,
OC_DISCOVERABLE|OC_OBSERVABLE);
lightResource->state = false;
lightResource->power= 0;
OCStackResult res = OCCreateResource(&(lightResource->handle),
- resourceTypeName,
- resourceInterface,
+ "core.light",
+ "oc.mi.def",
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
//URI of the resource
std::string resourceURI = m_lightUri;
//resource type name. In this case, it is light
- std::string resourceTypeName = resourceTypeLight;
+ std::string resourceTypeName = "core.light";
// resource interface.
- std::string resourceInterface = resourceInterfaceDefault;
+ std::string resourceInterface = DEFAULT_INTERFACE;
// OCResourceProperty is defined ocstack.h
uint8_t resourceProperty;
// URI of the resource
std::string resourceURI = "/a/light1";
// resource type name. In this case, it is light
- std::string resourceTypeName = resourceTypeLight;
+ std::string resourceTypeName = "core.light";
// resource interface.
- std::string resourceInterface = resourceInterfaceDefault;
+ std::string resourceInterface = DEFAULT_INTERFACE;
// OCResourceProperty is defined ocstack.h
uint8_t resourceProperty;
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;
+ QueryParamsMap queries = request->getQueryParameters();
- cout << "\t\t\tquery params: \n";
-
- for(auto it : queryParamsMap)
+ if (!queries.empty())
{
- 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;
- }
+ std::cout << "\nQuery processing upto entityHandler" << std::endl;
}
- if(typeName.compare(resourceTypeLight) == 0 &&
- interfaceName.compare(resourceInterfaceDefault) == 0)
- {
- ehResult = OC_EH_OK;
- }
- else
+ for (auto it : queries)
{
- cout<< "\t\t Invalid ResourceInterface Type & Name received from Client"<<endl;
+ std::cout << "Query key: " << it.first << " value : " << it.second
+ << std:: endl;
}
// If the request type is GET
- if(requestType == "GET" && ehResult == OC_EH_OK)
+ if(requestType == "GET")
{
cout << "\t\t\trequestType : GET\n";
if(isSlowResponse) // Slow response case
}
}
}
- else if(requestType == "PUT" && ehResult == OC_EH_OK)
+ else if(requestType == "PUT")
{
cout << "\t\t\trequestType : PUT\n";
OCRepresentation rep = request->getResourceRepresentation();
ehResult = OC_EH_OK;
}
}
- else if(requestType == "POST" && ehResult == OC_EH_OK)
+ else if(requestType == "POST")
{
cout << "\t\t\trequestType : POST\n";
ehResult = OC_EH_OK;
}
}
- else if(requestType == "DELETE" && ehResult == OC_EH_OK)
+ else if(requestType == "DELETE")
{
- // DELETE request operations
+ cout << "Delete request received" << endl;
}
}