1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 * *****************************************************************/
23 #include "pinoxmcommon.h"
25 #define TAG "PIN_OXM_COMMON"
27 static GeneratePinCallback gGenPinCallback = NULL;
28 static InputPinCallback gInputPinCallback = NULL;
30 void SetInputPinCB(InputPinCallback pinCB)
34 OC_LOG(ERROR, TAG, "Failed to set callback for input pin.");
38 gInputPinCallback = pinCB;
41 void SetGeneratePinCB(GeneratePinCallback pinCB)
45 OC_LOG(ERROR, TAG, "Failed to set callback for generate pin.");
49 gGenPinCallback = pinCB;
52 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize)
56 OC_LOG(ERROR, TAG, "PIN buffer is NULL");
57 return OC_STACK_INVALID_PARAM;
59 if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
61 OC_LOG(ERROR, TAG, "PIN buffer size is too small");
62 return OC_STACK_INVALID_PARAM;
64 for(size_t i = 0; i < OXM_RANDOM_PIN_SIZE; i++)
66 pinBuffer[i] = OCGetRandomRange((uint32_t)'0', (uint32_t)'9');
68 pinBuffer[OXM_RANDOM_PIN_SIZE] = '\0';
72 gGenPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE);
76 OC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
77 OC_LOG(ERROR, TAG, "Callback for genrate PIN should be registered to use PIN based OxM.");
78 return OC_STACK_ERROR;
85 OCStackResult InputPin(char* pinBuffer, size_t bufferSize)
89 OC_LOG(ERROR, TAG, "PIN buffer is NULL");
90 return OC_STACK_INVALID_PARAM;
92 if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
94 OC_LOG(ERROR, TAG, "PIN buffer size is too small");
95 return OC_STACK_INVALID_PARAM;
100 gInputPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE + 1);
104 OC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
105 OC_LOG(ERROR, TAG, "Callback for input PIN should be registered to use PIN based OxM.");
106 return OC_STACK_ERROR;