[IOT-2714][ES] Adding Error Codes as per OCF 1.3.
authorSenthil Kumar G S <senthil.gs@samsung.com>
Fri, 13 Oct 2017 09:29:45 +0000 (14:59 +0530)
committerSenthil Kumar G S <senthil.gs@samsung.com>
Fri, 13 Oct 2017 09:30:13 +0000 (15:00 +0530)
i) Added error codes to represent invalid auth type and enc type.
ii) Added logic to send OC_EH_BAD_REQUEST(400) without payload
if POST request comes to "/WiFiConfResURI" or "/EasySetupResURI"
with invalid auth type or enc type.

Also updated the comments for existing error codes.

Change-Id: I246af9a777f1aba1f18474ded74e38c4a5ed9b2c
Signed-off-by: Senthil Kumar G S <senthil.gs@samsung.com>
service/easy-setup/enrollee/inc/easysetup.h
service/easy-setup/enrollee/src/resourcehandler.c
service/easy-setup/inc/escommon.h
service/easy-setup/mediator/richsdk/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/mediator/enums/ESErrorCode.java

index 99b926fffa8645e7331af060f6e61687efa7f1e3..b87d15c0858772866678d11b7ac01f80504e10f8 100644 (file)
@@ -69,7 +69,6 @@ typedef void (*ESReadUserdataCb)(OCRepPayload* payload, char* resourceType, void
  */
 ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks);
 
-
 /**
  * This function Sets Device Information.
  *
@@ -80,7 +79,6 @@ ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvision
  */
 ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty);
 
-
 /**
  * This function Sets Enrollee's State.
  *
@@ -91,7 +89,6 @@ ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty);
  */
 ESResult ESSetState(ESEnrolleeState esState);
 
-
 /**
  * This function Sets Enrollee's Error Code.
  *
index ac763deb74c7460cc156a358ab883ed41c4b7ede..01ab9b583baf8b732f31824d2237c406b53e1602 100644 (file)
@@ -53,11 +53,13 @@ OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandle
 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
-void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
+OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
 void updateEasySetupConnectProperty(OCRepPayload* input);
-void updateWiFiConfResource(OCRepPayload* input);
+OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input);
 void updateCoapCloudConfResource(OCRepPayload* input);
 void updateDevConfResource(OCRepPayload* input);
+bool isAuthTypeSupported(WIFI_AUTHTYPE authType);
+bool isEncTypeSupported(WIFI_ENCTYPE encType);
 const char *getResult(OCStackResult result);
 
 ESConnectRequestCB gConnectRequestEvtCb = NULL;
@@ -339,18 +341,20 @@ OCStackResult initDevConfResource(bool isSecured)
 
 }
 
-void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
+OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest,
+    OCRepPayload* input)
 {
     OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
 
+    OCEntityHandlerResult ehResult = OC_EH_OK;
     if (ehRequest->query)
     {
         if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
         {
+            bool hasError = false;
             // When Provisioning resource has a POST with BatchInterface
             // Parsing POST request on Batch Interface cosidering same format as GET using batch.
             OCRepPayload *children = input;
-
             while(children)
             {
                 char* uri = children->uri;
@@ -387,7 +391,13 @@ void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* in
                 }
                 else if (0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
                 {
-                    updateWiFiConfResource(repPayload);
+                    if (updateWiFiConfResource(repPayload) != OC_EH_OK)
+                    {
+                        // Possibility of failure exist only when updating WiFiConf resource.
+                        // So error code is checked only for this function.
+                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WiFiConf resource.");
+                        hasError = true;
+                    }
                 }
                 else if (0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
                 {
@@ -401,12 +411,18 @@ void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* in
                 children = children->next;
                 OCRepPayloadDestroy(repPayload);
              }
+
+             if (hasError)
+             {
+                ehResult = OC_EH_BAD_REQ;
+             }
         }
         else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
         {
             OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
             updateEasySetupConnectProperty(input);
         }
+    return ehResult;
     }
 
     OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
@@ -461,70 +477,90 @@ void updateEasySetupConnectProperty(OCRepPayload* input)
     }
 }
 
-void updateWiFiConfResource(OCRepPayload* input)
+OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
 {
+    OCEntityHandlerResult ehResult = OC_EH_ERROR;
     ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
-
     if (wiFiData == NULL)
     {
         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
-        return;
+        return ehResult;
     }
 
+    char* ssid = NULL;
+    char* cred = NULL;
+    char *authType = NULL;
+    char *encType = NULL;
     memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
     memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
     wiFiData->authtype = NONE_AUTH;
     wiFiData->enctype = NONE_AUTH;
     wiFiData->userdata = NULL;
 
-    char* ssid = NULL;
-    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
-    {
-        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
-        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
-        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
-                g_ESWiFiConfResource.ssid);
-    }
-
-    char* cred = NULL;
-    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
-    {
-        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
-        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
-        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
-                g_ESWiFiConfResource.cred);
-    }
-
     bool validAuthType = false;
-    char *authType = NULL;
     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
     {
         WIFI_AUTHTYPE tmp;
         validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
-        if (validAuthType == true)
+        if (validAuthType && isAuthTypeSupported(tmp))
         {
-            g_ESWiFiConfResource.authType = tmp;
-            wiFiData->authtype = g_ESWiFiConfResource.authType;
+            wiFiData->authtype = tmp;
             OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
-                    g_ESWiFiConfResource.authType);
+                    wiFiData->authtype);
+        }
+        else
+        {
+            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
+            ehResult = OC_EH_BAD_REQ;
+            goto EXIT;
         }
     }
 
     bool validEncType = false;
-    char *encType = NULL;
     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
     {
         WIFI_ENCTYPE tmp;
         validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
-        if (validEncType == true)
+        if (validEncType && isEncTypeSupported(tmp))
         {
-            g_ESWiFiConfResource.encType = tmp;
-            wiFiData->enctype = g_ESWiFiConfResource.encType;
+            wiFiData->enctype = tmp;
             OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
-                    g_ESWiFiConfResource.encType);
+                    wiFiData->enctype);
+        }
+        else
+        {
+            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
+            ehResult = OC_EH_BAD_REQ;
+            goto EXIT;
         }
     }
 
+    if (validAuthType)
+    {
+        g_ESWiFiConfResource.authType = wiFiData->authtype;
+    }
+
+    if (validEncType)
+    {
+        g_ESWiFiConfResource.encType = wiFiData->enctype;
+    }
+
+    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
+    {
+        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
+        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
+        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
+                g_ESWiFiConfResource.ssid);
+    }
+
+    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
+    {
+        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
+        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
+        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
+                g_ESWiFiConfResource.cred);
+    }
+
     if (gReadUserdataCb)
     {
         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
@@ -550,11 +586,15 @@ void updateWiFiConfResource(OCRepPayload* input)
         OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
     }
 
+    ehResult = OC_EH_OK;
+
+EXIT:
     OICFree(encType);
     OICFree(authType);
     OICFree(cred);
     OICFree(ssid);
     OICFree(wiFiData);
+    return ehResult;
 }
 
 void updateCoapCloudConfResource(OCRepPayload* input)
@@ -660,7 +700,7 @@ void updateDevConfResource(OCRepPayload* input)
 
     // If a writable property in oic.r.devconf is added later,
     // a condition for calling a resistered callback should be implemented also.
-    if( devConfData->userdata != NULL )
+    if (devConfData->userdata != NULL)
     {
         OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
 
@@ -1495,7 +1535,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
         return ehResult;
     }
-    if(!isValidESResourceHandle(ehRequest->resource))
+    if (!isValidESResourceHandle(ehRequest->resource))
     {
         OIC_LOG(ERROR, ES_RH_TAG, "Request does not have a valid Easy Setup Resource handle");
         return ehResult;
@@ -1511,7 +1551,7 @@ OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRep
 
     char *iface_name = NULL;
     GetInterfaceNameFromQuery(ehRequest->query, &iface_name);
-    if(!iface_name)
+    if (!iface_name)
     {
         iface_name = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
     }
@@ -1579,7 +1619,11 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRe
         }
         else
         {
-            updateEasySetupResource(ehRequest, input);
+            if (updateEasySetupResource(ehRequest, input) != OC_EH_OK)
+            {
+                OIC_LOG(ERROR, ES_RH_TAG, "Failed to update EasySetup resource.");
+                return OC_EH_BAD_REQ;
+            }
         }
     }
     else if (ehRequest->resource == g_ESWiFiConfResource.handle)
@@ -1591,7 +1635,11 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRe
         }
         else
         {
-            updateWiFiConfResource(input);
+            if (updateWiFiConfResource(input) != OC_EH_OK)
+            {
+                OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WifiConf resource.");
+                return OC_EH_BAD_REQ;
+            }
         }
     }
     else if (ehRequest->resource == g_ESCoapCloudConfResource.handle)
@@ -1866,6 +1914,34 @@ OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
     return OC_EH_OK;
 }
 
+bool isAuthTypeSupported(WIFI_AUTHTYPE authType)
+{
+    bool supported = false;
+    for (uint8_t i = 0; i < g_ESWiFiConfResource.numSupportedAuthType; ++i)
+    {
+        if (g_ESWiFiConfResource.supportedAuthType[i] == authType)
+        {
+            supported = true;
+            break;
+        }
+    }
+    return supported;
+}
+
+bool isEncTypeSupported(WIFI_ENCTYPE encType)
+{
+    bool supported = false;
+    for (uint8_t i = 0; i < g_ESWiFiConfResource.numSupportedEncType; ++i)
+    {
+        if (g_ESWiFiConfResource.supportedEncType[i] == encType)
+        {
+            supported = true;
+            break;
+        }
+    }
+    return supported;
+}
+
 const char *getResult(OCStackResult result)
 {
     switch (result)
index a9930f27679c7274fd5e697b03634a44fd3b1bb6..8a52d835e4d73ef7d8b258b39756ce351d00e972 100644 (file)
@@ -353,78 +353,98 @@ typedef enum
 typedef enum
 {
     /**
-     * Init Error Code
+     * Init Error Code.
      */
     ES_ERRCODE_NO_ERROR = 0,
 
     /**
-     * Error Code that given WiFi's SSID is not found
+     * WiFi's SSID is not found.
      */
     ES_ERRCODE_SSID_NOT_FOUND,
 
     /**
-     * Error Code that given WiFi's Password is wrong
+     * WiFi's Password is wrong.
      */
     ES_ERRCODE_PW_WRONG,
 
     /**
-     * Error Code that IP address is not allocated
+     * IP address is not allocated.
      */
     ES_ERRCODE_IP_NOT_ALLOCATED,
 
     /**
-     * Error Code that there is no Internet connection
+     * There is no Internet connection.
      */
     ES_ERRCODE_NO_INTERNETCONNECTION,
 
     /**
-     * Error Code that Timeout occured
+     * Timeout occured.
      */
     ES_ERRCODE_TIMEOUT,
 
     /**
-     * Error Code that cloud server is not reachable due to wrong URL of cloud server, for example.
+     * Auth type is not supported by the Enrollee.
+     */
+    ES_ERRCODE_AUTH_TYPE_NOT_SUPPORTED,
+
+    /**
+     * Enc type is not supported by the Enrollee.
+     */
+    ES_ERRCODE_ENC_TYPE_NOT_SUPPORTED,
+
+    /**
+     * Auth type is not supported by the Enroller.
+     */
+    ES_ERRCODE_AUTH_TYPE_INVALID,
+
+    /**
+     * Enc type is not supported by the Enroller.
+     */
+    ES_ERRCODE_ENC_TYPE_INVALID,
+
+    /**
+     * Cloud server is not reachable due to wrong URL of cloud server, for example.
      */
     ES_ERRCODE_FAILED_TO_ACCESS_CLOUD_SERVER,
 
     /**
-     * Error Code that no response is arrived from cloud server
+     * No response from cloud server.
      */
     ES_ERRCODE_NO_RESPONSE_FROM_CLOUD_SERVER,
 
     /**
-     * Error Code that a delivered authcode is not valid.
+     * Delivered authcode is not valid.
      */
     ES_ERRCODE_INVALID_AUTHCODE,
 
     /**
-     * Error Code that a given access token is not valid due to its expiration, for example.
+     * Access token is not valid due to its expiration, for example.
      */
     ES_ERRCODE_INVALID_ACCESSTOKEN,
 
     /**
-     * Error Code that a refresh of expired access token is failed due to some reasons.
+     * Refresh of expired access token is failed due to some reasons.
      */
     ES_ERRCODE_FAILED_TO_REFRESH_ACCESSTOKEN,
 
     /**
-     * Error Code that a target device is not discovered in cloud server
+     * Target device is not discovered in cloud server.
      */
     ES_ERRCODE_FAILED_TO_FIND_REGISTERED_DEVICE_IN_CLOUD,
 
     /**
-     * Error Code that a target user does not exist in cloud server.
+     * Target user does not exist in cloud server.
      */
     ES_ERRCODE_FAILED_TO_FIND_REGISTERED_USER_IN_CLOUD,
 
     /**
-     * Error Code that an enrollee can not connect to a target WiFi AP because the AP resides in
+     * Enrollee can not connect to a target WiFi AP because the AP resides in
      * an unsupported WiFi frequency.
      */
     ES_ERRCODE_UNSUPPORTED_WIFI_FREQUENCY,
 
     /**
-     * Error Code that Unknown error occured
+     * Unknown error occured.
      */
     ES_ERRCODE_UNKNOWN = 255
 } ESErrorCode;
index 264410de073a60df9b73d02632651476dc74766f..8b540637b8dc5960aad602fb4136aa6c5cfc99b2 100755 (executable)
@@ -26,78 +26,98 @@ package org.iotivity.service.easysetup.mediator.enums;
 public enum ESErrorCode {
 
     /**
-     * Init Error Code
+     * Init Error Code.
      */
     ES_ERRCODE_NO_ERROR(0),
 
     /**
-     * Error Code that given WiFi's SSID is not found
+     * WiFi's SSID is not found.
      */
     ES_ERRCODE_SSID_NOT_FOUND(1),
 
     /**
-     * Error Code that given WiFi's Password is wrong
+     * WiFi's Password is wrong.
      */
     ES_ERRCODE_PW_WRONG(2),
 
     /**
-     * Error Code that IP address is not allocated
+     * IP address is not allocated.
      */
     ES_ERRCODE_IP_NOT_ALLOCATED(3),
 
     /**
-     * Error Code that there is no Internet connection
+     * There is no Internet connection.
      */
     ES_ERRCODE_NO_INTERNETCONNECTION(4),
 
     /**
-     * Error Code that Timeout occured
+     * Timeout occured.
      */
     ES_ERRCODE_TIMEOUT(5),
 
     /**
-     * Error Code that cloud server is not reachable due to wrong URL of cloud server, for example.
+     * Auth type is not supported by the Enrollee.
      */
-    ES_ERRCODE_FAILED_TO_ACCESS_CLOUD_SERVER(6),
+    ES_ERRCODE_AUTH_TYPE_NOT_SUPPORTED(6),
 
     /**
-     * Error Code that no response is arrived from cloud server
+     * Enc type is not supported by the Enrollee.
      */
-    ES_ERRCODE_NO_RESPONSE_FROM_CLOUD_SERVER(7),
+    ES_ERRCODE_ENC_TYPE_NOT_SUPPORTED(7),
 
     /**
-     * Error Code that a delivered authcode is not valid.
+     * Auth type is not supported by the Enroller.
      */
-    ES_ERRCODE_INVALID_AUTHCODE(8),
+    ES_ERRCODE_AUTH_TYPE_INVALID(8),
 
     /**
-     * Error Code that a given access token is not valid due to its expiration, for example.
+     * Enc type is not supported by the Enroller.
      */
-    ES_ERRCODE_INVALID_ACCESSTOKEN(9),
+    ES_ERRCODE_ENC_TYPE_INVALID(9),
 
     /**
-     * Error Code that a refresh of expired access token is failed due to some reasons.
+     * Cloud server is not reachable due to wrong URL of cloud server, for example.
      */
-    ES_ERRCODE_FAILED_TO_REFRESH_ACCESSTOKEN(10),
+    ES_ERRCODE_FAILED_TO_ACCESS_CLOUD_SERVER(10),
 
     /**
-     * Error Code that a target device is not discovered in cloud server
+     * No response from cloud server.
      */
-    ES_ERRCODE_FAILED_TO_FIND_REGISTERED_DEVICE_IN_CLOUD(11),
+    ES_ERRCODE_NO_RESPONSE_FROM_CLOUD_SERVER(11),
 
     /**
-     * Error Code that a target user does not exist in cloud server.
+     * Delivered authcode is not valid.
      */
-    ES_ERRCODE_FAILED_TO_FIND_REGISTERED_USER_IN_CLOUD(12),
+    ES_ERRCODE_INVALID_AUTHCODE(12),
 
     /**
-     * Error Code that an enrollee can not connect to a target WiFi AP because the AP resides in
+     * Access token is not valid due to its expiration, for example.
+     */
+    ES_ERRCODE_INVALID_ACCESSTOKEN(13),
+
+    /**
+     * Refresh of expired access token is failed due to some reasons.
+     */
+    ES_ERRCODE_FAILED_TO_REFRESH_ACCESSTOKEN(14),
+
+    /**
+     * Target device is not discovered in cloud server.
+     */
+    ES_ERRCODE_FAILED_TO_FIND_REGISTERED_DEVICE_IN_CLOUD(15),
+
+    /**
+     * Target user does not exist in cloud server.
+     */
+    ES_ERRCODE_FAILED_TO_FIND_REGISTERED_USER_IN_CLOUD(16),
+
+    /**
+     * Enrollee can not connect to a target WiFi AP because the AP resides in
      * an unsupported WiFi frequency.
      */
-    ES_ERRCODE_UNSUPPORTED_WIFI_FREQUENCY(13),
+    ES_ERRCODE_UNSUPPORTED_WIFI_FREQUENCY(17),
 
     /**
-     * Error Code that Unknown error occured
+     * Unknown error occured.
      */
     ES_ERRCODE_UNKNOWN(255);