Merge branch 'resource-container'
[platform/upstream/iotivity.git] / resource / csdk / security / src / amaclresource.c
index e34b6bd..de7e990 100644 (file)
@@ -36,7 +36,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define TAG  PCF("SRM-AMACL")
+#define TAG  "SRM-AMACL"
 
 OicSecAmacl_t *gAmacl = NULL;
 static OCResourceHandle gAmaclHandle = NULL;
@@ -48,7 +48,7 @@ void DeleteAmaclList(OicSecAmacl_t* amacl)
         OicSecAmacl_t *amaclTmp1 = NULL, *amaclTmp2 = NULL;
         LL_FOREACH_SAFE(amacl, amaclTmp1, amaclTmp2)
         {
-            int i = 0;
+            unsigned int i = 0;
 
             LL_DELETE(amacl, amaclTmp1);
 
@@ -103,7 +103,7 @@ char * BinToAmaclJSON(const OicSecAmacl_t * amacl)
             cJSON_AddItemToObject(jsonAmacl, OIC_JSON_RESOURCES_NAME, jsonRsrcArray =
                     cJSON_CreateArray());
             VERIFY_NON_NULL(TAG, jsonRsrcArray, ERROR);
-            for (int i = 0; i < amacl->resourcesLen; i++)
+            for (unsigned int i = 0; i < amacl->resourcesLen; i++)
             {
                 cJSON_AddItemToArray(jsonRsrcArray, cJSON_CreateString(amacl->resources[i]));
             }
@@ -113,7 +113,7 @@ char * BinToAmaclJSON(const OicSecAmacl_t * amacl)
             cJSON_AddItemToObject(jsonAmacl, OIC_JSON_AMSS_NAME, jsonAmsArray =
                     cJSON_CreateArray());
             VERIFY_NON_NULL(TAG, jsonAmsArray, ERROR);
-            for (int i = 0; i < amacl->amssLen; i++)
+            for (unsigned int i = 0; i < amacl->amssLen; i++)
             {
                 outLen = 0;
 
@@ -129,7 +129,7 @@ char * BinToAmaclJSON(const OicSecAmacl_t * amacl)
             cJSON_AddItemToObject(jsonAmacl, OIC_JSON_OWNERS_NAME, jsonOwnrArray =
                     cJSON_CreateArray());
             VERIFY_NON_NULL(TAG, jsonOwnrArray, ERROR);
-            for (int i = 0; i < amacl->ownersLen; i++)
+            for (unsigned int i = 0; i < amacl->ownersLen; i++)
             {
                 outLen = 0;
 
@@ -176,7 +176,7 @@ OicSecAmacl_t * JSONToAmaclBin(const char * jsonStr)
     VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
 
     jsonAmaclArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
-    VERIFY_NON_NULL(TAG, jsonAmaclArray, ERROR);
+    VERIFY_NON_NULL(TAG, jsonAmaclArray, INFO);
 
     if (cJSON_Array == jsonAmaclArray->type)
     {
@@ -211,7 +211,7 @@ OicSecAmacl_t * JSONToAmaclBin(const char * jsonStr)
             amacl->resources = (char**)OICCalloc(amacl->resourcesLen, sizeof(char*));
             VERIFY_NON_NULL(TAG, (amacl->resources), ERROR);
 
-            int idxx = 0;
+            size_t idxx = 0;
             do
             {
                 cJSON *jsonRsrc = cJSON_GetArrayItem(jsonObj, idxx);
@@ -259,7 +259,7 @@ static OCEntityHandlerResult HandleAmaclGetRequest (const OCEntityHandlerRequest
 
     OICFree(jsonStr);
 
-    OC_LOG_V (INFO, TAG, PCF("%s RetVal %d"), __func__ , ehRet);
+    OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
     return ehRet;
 }
 
@@ -268,7 +268,7 @@ static OCEntityHandlerResult HandleAmaclPostRequest (const OCEntityHandlerReques
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
 
     // Convert JSON Amacl data into binary. This will also validate the Amacl data received.
-    OicSecAmacl_t* newAmacl = JSONToAmaclBin((char *)(ehRequest->reqJSONPayload));
+    OicSecAmacl_t* newAmacl = JSONToAmaclBin(((OCSecurityPayload*)ehRequest->payload)->securityData);
 
     if (newAmacl)
     {
@@ -294,7 +294,7 @@ static OCEntityHandlerResult HandleAmaclPostRequest (const OCEntityHandlerReques
     // Send payload to request originator
     SendSRMResponse(ehRequest, ehRet, NULL);
 
-    OC_LOG_V (INFO, TAG, PCF("%s RetVal %d"), __func__ , ehRet);
+    OIC_LOG_V (DEBUG, TAG, "%s RetVal %d", __func__ , ehRet);
     return ehRet;
 }
 
@@ -306,6 +306,7 @@ OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
                                           OCEntityHandlerRequest * ehRequest,
                                           void* callbackParameter)
 {
+    (void) callbackParameter;
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
 
     if (!ehRequest)
@@ -315,7 +316,7 @@ OCEntityHandlerResult AmaclEntityHandler (OCEntityHandlerFlag flag,
 
     if (flag & OC_REQUEST_FLAG)
     {
-        OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG"));
+        OIC_LOG (DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
         switch (ehRequest->method)
         {
             case OC_REST_GET:
@@ -352,7 +353,7 @@ OCStackResult CreateAmaclResource()
 
     if (OC_STACK_OK != ret)
     {
-        OC_LOG (FATAL, TAG, PCF("Unable to instantiate Amacl resource"));
+        OIC_LOG (FATAL, TAG, "Unable to instantiate Amacl resource");
         DeInitAmaclResource();
     }
     return ret;
@@ -400,3 +401,28 @@ void DeInitAmaclResource()
     DeleteAmaclList(gAmacl);
     gAmacl = NULL;
 }
+
+
+OCStackResult AmaclGetAmsDeviceId(const char *resource, OicUuid_t *amsDeviceId)
+{
+    OicSecAmacl_t *amacl = NULL;
+
+    VERIFY_NON_NULL(TAG, resource, ERROR);
+    VERIFY_NON_NULL(TAG, amsDeviceId, ERROR);
+
+    LL_FOREACH(gAmacl, amacl)
+    {
+        for(size_t i = 0; i < amacl->resourcesLen; i++)
+        {
+            if (strncmp((amacl->resources[i]), resource, strlen(amacl->resources[i])) == 0)
+            {
+                //Returning the ID of the first AMS service for the resource
+                memcpy(amsDeviceId, &amacl->amss[0], sizeof(*amsDeviceId));
+                return OC_STACK_OK;
+            }
+        }
+    }
+
+exit:
+    return OC_STACK_ERROR;
+}