Fixed C and C++ sample apps.
authorMandeep Shetty <mandeep.shetty@intel.com>
Fri, 8 May 2015 19:17:16 +0000 (12:17 -0700)
committerErich Keane <erich.keane@intel.com>
Tue, 12 May 2015 17:31:00 +0000 (17:31 +0000)
Fixed the GET fail on C++ samples.
Added logging that informs users that query processing is not
implemented and is the responsibility if the entity handler.

Change-Id: If73ee023aa739584c34d1a4ffe4656a4902f6cf3
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/943
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Mark Tung <mark.y.tung@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp
resource/examples/simpleserver.cpp

index f133925..078b0d1 100644 (file)
@@ -155,14 +155,13 @@ char* constructJsonResponse (OCEntityHandlerRequest *ehRequest)
  */
 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
@@ -174,53 +173,20 @@ bool checkIfQueryForPowerPassed(char * query)
     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);
 
@@ -510,7 +476,7 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
 {
     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};
 
@@ -526,10 +492,6 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag flag,
             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)
     {
@@ -538,44 +500,35 @@ OCDeviceEntityHandlerCb (OCEntityHandlerFlag 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
@@ -643,12 +596,6 @@ OCEntityHandlerCb (OCEntityHandlerFlag flag,
             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");
@@ -657,42 +604,31 @@ OCEntityHandlerCb (OCEntityHandlerFlag 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)))
@@ -869,7 +805,7 @@ void *presenceNotificationGenerator(void *param)
             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);
@@ -1040,8 +976,8 @@ int createLightResource (char *uri, LightResource *lightResource)
     lightResource->state = false;
     lightResource->power= 0;
     OCStackResult res = OCCreateResource(&(lightResource->handle),
-            resourceTypeName,
-            resourceInterface,
+            "core.light",
+            "oc.mi.def",
             uri,
             OCEntityHandlerCb,
             OC_DISCOVERABLE|OC_OBSERVABLE);
index 4b04f3a..8d5f9cb 100644 (file)
@@ -53,11 +53,6 @@ bool isSecure = false;
 /// 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
@@ -98,9 +93,9 @@ public:
         //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;
@@ -130,9 +125,9 @@ public:
         // 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;
@@ -280,42 +275,20 @@ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
             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
@@ -340,7 +313,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
                     }
                 }
             }
-            else if(requestType == "PUT" && ehResult == OC_EH_OK)
+            else if(requestType == "PUT")
             {
                 cout << "\t\t\trequestType : PUT\n";
                 OCRepresentation rep = request->getResourceRepresentation();
@@ -356,7 +329,7 @@ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
                     ehResult = OC_EH_OK;
                 }
             }
-            else if(requestType == "POST" && ehResult == OC_EH_OK)
+            else if(requestType == "POST")
             {
                 cout << "\t\t\trequestType : POST\n";
 
@@ -381,9 +354,9 @@ OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
                     ehResult = OC_EH_OK;
                 }
             }
-            else if(requestType == "DELETE" && ehResult == OC_EH_OK)
+            else if(requestType == "DELETE")
             {
-                // DELETE request operations
+                cout << "Delete request received" << endl;
             }
         }