Merge tizen_5.0 codes into tizen_4.0
[platform/upstream/iotivity.git] / resource / csdk / security / src / doxmresource.c
index 999c016..4fbd45c 100644 (file)
@@ -48,6 +48,7 @@
 #include "srmutility.h"
 #include "pinoxmcommon.h"
 #include "oxmverifycommon.h"
+#include "octhread.h"
 
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
 #include "pkix_interface.h"
@@ -91,7 +92,10 @@ typedef enum ConfirmState{
 }ConfirmState_t;
 
 static OicSecDoxm_t        *gDoxm = NULL;
+static oc_mutex            g_mutexDoxm = NULL;
+static bool                g_isDoxmNull = false;
 static OCResourceHandle    gDoxmHandle = NULL;
+static InformOxmSelectedCallback_t g_InformOxmSelectedCallback = NULL;
 
 static OicSecOxm_t gOicSecDoxmJustWorks = OIC_JUST_WORKS;
 static OicSecDoxm_t gDefaultDoxm =
@@ -160,6 +164,12 @@ void DeleteDoxmBinData(OicSecDoxm_t* doxm)
         //Clean doxm itself
         OICFree(doxm);
     }
+
+    if(g_mutexDoxm)
+    {
+        oc_mutex_free(g_mutexDoxm);
+        g_mutexDoxm = NULL;
+    }
 }
 
 OCStackResult DoxmToCBORPayload(const OicSecDoxm_t *doxm, uint8_t **payload, size_t *size,
@@ -1072,6 +1082,20 @@ static bool ValidateOxmsel(const OicSecOxm_t *supportedMethods,
     return isValidOxmsel;
 }
 
+void SetInformOxmSelCB(InformOxmSelectedCallback_t informOxmSelCB)
+{
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+    g_InformOxmSelectedCallback = informOxmSelCB;
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+
+void UnsetInformOxmSelCB()
+{
+    OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
+    g_InformOxmSelectedCallback = NULL;
+    OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
+}
+
 static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRequest)
 {
     OIC_LOG (DEBUG, TAG, "Doxm EntityHandle  processing POST request");
@@ -1231,6 +1255,10 @@ static OCEntityHandlerResult HandleDoxmPostRequest(OCEntityHandlerRequest * ehRe
                     ehRet = OC_EH_NOT_ACCEPTABLE;
                     goto exit;
                 }
+                if (g_InformOxmSelectedCallback)
+                {
+                    g_InformOxmSelectedCallback(newDoxm->oxmSel);
+                }
 
 #if defined (__WITH_TLS__) || defined(__WITH_DTLS__)
                 if (memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
@@ -1692,6 +1720,14 @@ OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
         return ehRet;
     }
 
+    oc_mutex_lock(g_mutexDoxm);
+
+    if(g_isDoxmNull)
+    {
+        oc_mutex_unlock(g_mutexDoxm);
+        return OC_EH_SERVICE_UNAVAILABLE;
+    }
+
     if (flag & OC_REQUEST_FLAG)
     {
         OIC_LOG(DEBUG, TAG, "Flag includes OC_REQUEST_FLAG");
@@ -1717,6 +1753,8 @@ OCEntityHandlerResult DoxmEntityHandler(OCEntityHandlerFlag flag,
                                OC_EH_OK : OC_EH_ERROR;
                 break;
         }
+
+        oc_mutex_unlock(g_mutexDoxm);
     }
 
     return ehRet;
@@ -1913,6 +1951,15 @@ OCStackResult InitDoxmResource()
 {
     OCStackResult ret = OC_STACK_ERROR;
 
+    if (!g_mutexDoxm)
+    {
+        g_mutexDoxm = oc_mutex_new();
+            if(!g_mutexDoxm)
+            {
+                return OC_STACK_ERROR;
+            }
+    }
+
     gConfirmState = CONFIRM_STATE_READY;
     gConfirmMsgId = 0;
 
@@ -1940,6 +1987,10 @@ OCStackResult InitDoxmResource()
         gDoxm = GetDoxmDefault();
     }
 
+     oc_mutex_lock(g_mutexDoxm);
+     g_isDoxmNull = false;
+     oc_mutex_unlock(g_mutexDoxm);
+
     //In case of the server is shut down unintentionally, we should initialize the owner
     if(gDoxm && (false == gDoxm->owned))
     {
@@ -1976,19 +2027,23 @@ OCStackResult InitDoxmResource()
 
 OCStackResult DeInitDoxmResource()
 {
+    oc_mutex_lock(g_mutexDoxm);
     OCStackResult ret = OCDeleteResource(gDoxmHandle);
     if (gDoxm  != &gDefaultDoxm)
     {
         DeleteDoxmBinData(gDoxm);
     }
     gDoxm = NULL;
+    g_isDoxmNull = true;
 
     if (OC_STACK_OK == ret)
     {
+        oc_mutex_unlock(g_mutexDoxm);
         return OC_STACK_OK;
     }
     else
     {
+        oc_mutex_unlock(g_mutexDoxm);
         return OC_STACK_ERROR;
     }
 }