Asynchronous User Confirm
[platform/upstream/iotivity.git] / resource / csdk / security / src / oxmverifycommon.c
index b8470e9..8f5f946 100644 (file)
@@ -25,6 +25,7 @@
 #include "srmresourcestrings.h"
 #include "cainterface.h"
 #include "oxmverifycommon.h"
+#include "octhread.h"
 
 #define TAG "OIC_VERIFY_COMMON"
 
@@ -32,7 +33,9 @@ static VerifyOptionBitmask_t gVerifyOption = (DISPLAY_NUM | USER_CONFIRM);
 
 static DisplayNumContext_t gDisplayNumContext = { .callback = NULL, .context = NULL };
 static UserConfirmContext_t gUserConfirmContext = { .callback = NULL, .context = NULL };
+static UserAsyncConfirmContext_t gUserAsyncConfirmContext = { .callback = NULL, .context = NULL, .userConfirm = false, .userConfirmResult = false };
 static InputStateContext_t gInputStateContext = { .callback = NULL, .context = NULL };
+extern oc_cond g_condWait;
 
 void SetDisplayNumCB(void * ptr, DisplayNumCallback displayNumCB)
 {
@@ -149,6 +152,62 @@ OCStackResult VerifyOwnershipTransfer(uint8_t mutualVerifNum [MUTUAL_VERIF_NUM_L
     return OC_STACK_OK;
 }
 
+OCStackResult VerifyUserConfirm()
+{
+    OIC_LOG(DEBUG, TAG, "IN VerifyUserConfirm");
+    OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
+    if (gVerifyOption)
+    {
+        if (!gUserAsyncConfirmContext.callback)
+        {
+            OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
+            return OC_STACK_ERROR;
+        }
+        OIC_LOG(DEBUG, TAG, "calling userAsyncConfirmCallback");
+        gUserAsyncConfirmContext.callback(&gUserAsyncConfirmContext);
+    }
+    OIC_LOG(DEBUG, TAG, "OUT VerifyUserConfirm");
+    return OC_STACK_OK;
+}
+
+void GetAsyncVerifyUserResult(bool * result, bool * confirm)
+{
+    *result = gUserAsyncConfirmContext.userConfirmResult;
+    *confirm = gUserAsyncConfirmContext.userConfirm;
+    return;
+}
+
+void SetAsyncUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
+{
+    OIC_LOG(DEBUG, TAG, "IN SetAsyncUserConfirmCB");
+    if (NULL == userConfirmCB)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
+        return;
+    }
+    gUserAsyncConfirmContext.callback = userConfirmCB;
+    gUserAsyncConfirmContext.context = ptr;
+    OIC_LOG(DEBUG, TAG, "OUT SetAsyncUserConfirmCB");
+}
+
+void* UnsetAsyncUserConfirmCB()
+{
+    OIC_LOG(DEBUG, TAG, "IN UnsetAsyncUserConfirmCB");
+    void *prevctx = gUserAsyncConfirmContext.context;
+    gUserAsyncConfirmContext.callback = NULL;
+    gUserAsyncConfirmContext.context = NULL;
+    OIC_LOG(DEBUG, TAG, "OUT UnsetAsyncUserConfirmCB");
+    return prevctx;
+}
+
+OCStackResult SendUserConfirm(bool confirmed)
+{
+    gUserAsyncConfirmContext.userConfirmResult = confirmed;
+    gUserAsyncConfirmContext.userConfirm = true;
+    oc_cond_signal(g_condWait);
+    return OC_STACK_OK;
+}
+
 OCStackResult NotifyInputState(void)
 {
     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);