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 aa40dd5..729b410 100644 (file)
@@ -78,6 +78,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.
  *
  * @param pinCB implementation of input PIN callback.
index 0f750a0..dcc729e 100644 (file)
@@ -481,13 +481,18 @@ 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.
      * This callback will be invoked when random PIN based OTM is done.
index 3646605..189f07c 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;
     }