QR Code Random Pin CB 14/212714/1
authorOleksii Beketov <ol.beketov@samsung.com>
Tue, 6 Aug 2019 10:49:33 +0000 (13:49 +0300)
committerSudipto <sudipto.bal@samsung.com>
Mon, 26 Aug 2019 06:57:11 +0000 (12:27 +0530)
Added an API to set preconfigured PIN code

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/553/commits/d5ebdbfdb3287aea754fc6a637ba771d6485004f
(cherry-picked from d5ebdbfdb3287aea754fc6a637ba771d6485004f)

Change-Id: I0d1ccdf352ddabe9bb57c59a15c9a5ac8eb6f189
Signed-off-by: Oleksii Beketov <ol.beketov@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
resource/csdk/security/include/pinoxmcommon.h
resource/csdk/security/provisioning/sample/sampleserver_randompin.cpp
resource/csdk/security/src/oxmpincommon.c

index aa40dd5c28fd4bd16713f9161e3e228ca6c30e63..729b410f63e79f4310c649886c4f72be30bb19af 100644 (file)
@@ -77,6 +77,19 @@ typedef void (*ClosePinDisplayCallback)(void);
  */
 void SetGeneratePinCB(GeneratePinCallback pinCB);
 
+/**
+ * Function to set preconfigured PIN value.
+ *
+ * @param[in] pin    PIN data
+ * @param[in] pinLen byte length of PIN
+ */
+OCStackResult SetPin(const char * pin, size_t pinLen);
+
+/**
+ * Function to unset preconfigured PIN.
+ */
+OCStackResult UnSetPin();
+
 /**
  * Function to setting input PIN callback from user.
  *
index 0f750a0836e491bdb58c2c4ba7dc874c07a14977..dcc729eab9290d8e012ebb662254058ac45edb0a 100644 (file)
@@ -481,12 +481,17 @@ int main()
         return 0;
     }
 
-   /**
+    /**
      * If server supported random pin based ownership transfer,
      * callback of print PIN should be registered before runing server.
      */
     SetGeneratePinCB(GeneratePinCB);
 
+    /**
+     * Set preconfigured PIN code
+     */
+    //SetPin("78563412", 8);
+
     /**
      * If ther server supports random pin based OTM,
      * the callback to close PIN display can be registered.
index 3646605d877717e4f95352b94d2a6ab69b2db48b..189f07c39aa99b9951403d721e315cff10c5bd50 100644 (file)
@@ -41,6 +41,7 @@
 #define NUMBER_OF_ALPHABET (26)
 
 static GeneratePinCallback gGenPinCallback = NULL;
+static GeneratePinCallback gSetPinCallback = NULL;
 static InputPinCallback gInputPinCallback = NULL;
 static ClosePinDisplayCallback gClosePinDispalyCallback = NULL;
 
@@ -49,12 +50,14 @@ typedef struct PinOxmData {
     size_t pinSize;
     OicSecPinType_t pinType;
     OicUuid_t newDevice;
+    int pinPreset;
 }PinOxmData_t;
 
 static PinOxmData_t g_PinOxmData = {
-        .pinData={0},
+        .pinData = {0},
         .pinSize = OXM_RANDOM_PIN_DEFAULT_SIZE,
         .pinType = (OicSecPinType_t)(OXM_RANDOM_PIN_DEFAULT_PIN_TYPE),
+        .pinPreset = 0
     };
 
 /**
@@ -192,6 +195,27 @@ static char GenerateRandomPinElement(OicSecPinType_t pinType)
     return allowedCharacters[OCGetRandomRange(0, curIndex)];
 }
 
+OCStackResult SetPin(const char * pin, size_t pinLen)
+{
+    if(NULL == pin || OXM_PRECONFIG_PIN_MAX_SIZE < pinLen)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    memcpy(g_PinOxmData.pinData, pin, pinLen);
+    g_PinOxmData.pinSize = pinLen;
+    g_PinOxmData.pinData[pinLen] = '\0';
+    g_PinOxmData.pinPreset = 1;
+
+    return OC_STACK_OK;
+}
+
+OCStackResult UnSetPin()
+{
+    g_PinOxmData.pinPreset = 0;
+    return OC_STACK_OK;
+}
+
 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize)
 {
     if(!pinBuffer)
@@ -211,23 +235,26 @@ OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize)
         return OC_STACK_ERROR;
     }
 
-    for(size_t i = 0; i < g_PinOxmData.pinSize; i++)
+    if (!g_PinOxmData.pinPreset)
     {
-        pinBuffer[i] = GenerateRandomPinElement(g_PinOxmData.pinType);
-        g_PinOxmData.pinData[i] = pinBuffer[i];
-    }
+        for(size_t i = 0; i < g_PinOxmData.pinSize; i++)
+        {
+            pinBuffer[i] = GenerateRandomPinElement(g_PinOxmData.pinType);
+            g_PinOxmData.pinData[i] = pinBuffer[i];
+        }
 
-    pinBuffer[g_PinOxmData.pinSize] = '\0';
-    g_PinOxmData.pinData[g_PinOxmData.pinSize] = '\0';
+        pinBuffer[g_PinOxmData.pinSize] = '\0';
+        g_PinOxmData.pinData[g_PinOxmData.pinSize] = '\0';
+    }
 
     if(gGenPinCallback)
     {
-        gGenPinCallback(pinBuffer, g_PinOxmData.pinSize);
+        gGenPinCallback(g_PinOxmData.pinData, g_PinOxmData.pinSize);
     }
     else
     {
         OIC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
-        OIC_LOG(ERROR, TAG, "Callback for genrate PIN should be registered to use PIN based OxM.");
+        OIC_LOG(ERROR, TAG, "Callback for generate PIN should be registered to use PIN based OxM.");
         return OC_STACK_ERROR;
     }