1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "securevirtualresourcetypes.h"
25 #include "srmresourcestrings.h"
26 #include "cainterface.h"
27 #include "oxmverifycommon.h"
30 #define TAG "OIC_VERIFY_COMMON"
32 static VerifyOptionBitmask_t gVerifyOption = (DISPLAY_NUM | USER_CONFIRM);
34 static DisplayNumContext_t gDisplayNumContext = { .callback = NULL, .context = NULL };
35 static UserConfirmContext_t gUserConfirmContext = { .callback = NULL, .context = NULL };
36 static UserAsyncConfirmContext_t gUserAsyncConfirmContext = { .callback = NULL, .context = NULL, .userConfirm = false, .userConfirmResult = false };
37 static InputStateContext_t gInputStateContext = { .callback = NULL, .context = NULL };
38 extern oc_cond g_condWait;
40 void SetDisplayNumCB(void * ptr, DisplayNumCallback displayNumCB)
42 OIC_LOG(DEBUG, TAG, "IN SetDisplayNumCB");
43 if (NULL == displayNumCB)
45 OIC_LOG(ERROR, TAG, "Failed to set callback for displaying mutualVerifNum");
48 gDisplayNumContext.callback = displayNumCB;
49 gDisplayNumContext.context = ptr;
50 OIC_LOG(DEBUG, TAG, "OUT SetDisplayNumCB");
53 void* UnsetDisplayNumCB()
55 OIC_LOG(DEBUG, TAG, "IN UnsetDisplayNumCB");
56 void *prevctx = gDisplayNumContext.context;
57 gDisplayNumContext.callback = NULL;
58 gDisplayNumContext.context= NULL;
59 OIC_LOG(DEBUG, TAG, "OUT UnsetDisplayNumCB");
63 void SetUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
65 OIC_LOG(DEBUG, TAG, "IN SetUserConfirmCB");
66 if (NULL == userConfirmCB)
68 OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
71 gUserConfirmContext.callback = userConfirmCB;
72 gUserConfirmContext.context = ptr;
73 OIC_LOG(DEBUG, TAG, "OUT SetUserConfirmCB");
76 void* UnsetUserConfirmCB()
78 OIC_LOG(DEBUG, TAG, "IN UnsetUserConfirmCB");
79 void *prevctx = gUserConfirmContext.context;
80 gUserConfirmContext.callback = NULL;
81 gUserConfirmContext.context = NULL;
82 OIC_LOG(DEBUG, TAG, "OUT UnsetUserConfirmCB");
86 void SetInputStateCB(void * ptr, InputStateCallback inputStateCB)
88 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
89 if (NULL == inputStateCB)
91 OIC_LOG(ERROR, TAG, "Failed to set callback to notify user input state");
94 gInputStateContext.callback = inputStateCB;
95 gInputStateContext.context = ptr;
96 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
99 void* UnsetInputStateCB()
101 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
102 void *prevctx = gInputStateContext.context;
103 gInputStateContext.callback = NULL;
104 gInputStateContext.context = NULL;
105 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
109 void SetVerifyOption(VerifyOptionBitmask_t verifyOption)
111 OIC_LOG(DEBUG, TAG, "IN SetVerifyOption");
112 gVerifyOption = verifyOption;
113 OIC_LOG_V(DEBUG, TAG, "VerifyOption set to %d", (int) gVerifyOption);
114 OIC_LOG(DEBUG, TAG, "OUT SetVerifyOption");
117 OCStackResult VerifyOwnershipTransfer(uint8_t mutualVerifNum [MUTUAL_VERIF_NUM_LEN],
118 VerifyOptionBitmask_t verifyOption)
120 OIC_LOG(DEBUG, TAG, "IN VerifyOwnershipTransfer");
121 OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
122 verifyOption = (VerifyOptionBitmask_t)(verifyOption & gVerifyOption);
123 if (verifyOption & DISPLAY_NUM)
125 if (!gDisplayNumContext.callback)
127 OIC_LOG(ERROR, TAG, "Callback for displaying verification PIN not registered");
128 return OC_STACK_ERROR;
130 OIC_LOG(DEBUG, TAG, "calling displayNumCallback");
131 if (OC_STACK_OK != gDisplayNumContext.callback(gDisplayNumContext.context, mutualVerifNum))
133 OIC_LOG(ERROR, TAG, "Failed to display verification PIN");
134 return OC_STACK_ERROR;
137 if (verifyOption & USER_CONFIRM)
139 if (!gUserConfirmContext.callback)
141 OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
142 return OC_STACK_ERROR;
144 OIC_LOG(DEBUG, TAG, "calling userConfirmCallback");
145 if (OC_STACK_OK != gUserConfirmContext.callback(gUserConfirmContext.context))
147 OIC_LOG(ERROR, TAG, "Failed to get user confirmation");
148 return OC_STACK_USER_DENIED_REQ;
151 OIC_LOG(DEBUG, TAG, "OUT VerifyOwnershipTransfer");
155 OCStackResult VerifyUserConfirm()
157 OIC_LOG(DEBUG, TAG, "IN VerifyUserConfirm");
158 OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
161 if (!gUserAsyncConfirmContext.callback)
163 OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
164 return OC_STACK_ERROR;
166 OIC_LOG(DEBUG, TAG, "calling userAsyncConfirmCallback");
167 gUserAsyncConfirmContext.callback(&gUserAsyncConfirmContext);
169 OIC_LOG(DEBUG, TAG, "OUT VerifyUserConfirm");
173 void GetAsyncVerifyUserResult(bool * result, bool * confirm)
175 *result = gUserAsyncConfirmContext.userConfirmResult;
176 *confirm = gUserAsyncConfirmContext.userConfirm;
180 void SetAsyncUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
182 OIC_LOG(DEBUG, TAG, "IN SetAsyncUserConfirmCB");
183 if (NULL == userConfirmCB)
185 OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
188 gUserAsyncConfirmContext.callback = userConfirmCB;
189 gUserAsyncConfirmContext.context = ptr;
190 OIC_LOG(DEBUG, TAG, "OUT SetAsyncUserConfirmCB");
193 void* UnsetAsyncUserConfirmCB()
195 OIC_LOG(DEBUG, TAG, "IN UnsetAsyncUserConfirmCB");
196 void *prevctx = gUserAsyncConfirmContext.context;
197 gUserAsyncConfirmContext.callback = NULL;
198 gUserAsyncConfirmContext.context = NULL;
199 OIC_LOG(DEBUG, TAG, "OUT UnsetAsyncUserConfirmCB");
203 OCStackResult SendUserConfirm(bool confirmed)
205 gUserAsyncConfirmContext.userConfirmResult = confirmed;
206 gUserAsyncConfirmContext.userConfirm = true;
207 oc_cond_signal(g_condWait);
211 OCStackResult NotifyInputState(void)
213 OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
215 if (!gInputStateContext.callback)
217 OIC_LOG(INFO, TAG, "Callback for notifying user input state not registered");
220 OIC_LOG(INFO, TAG, "calling inputStateCallback");
221 if (OC_STACK_OK != gInputStateContext.callback(gInputStateContext.context))
223 OIC_LOG(ERROR, TAG, "Failed to notify user input state");
224 return OC_STACK_ERROR;
226 OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);