replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / sampleserver_randompin.cpp
index 06a5512..0f750a0 100644 (file)
 ///////////////////////////////////////////////////////////////////////
 //NOTE :  This sample server is generated based on ocserverbasicops.cpp
 ///////////////////////////////////////////////////////////////////////
+#include "iotivity_config.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
-#ifdef HAVE_PTHREAD_H
-#include <pthread.h>
+#ifdef HAVE_TIME_H
+#include <time.h>
 #endif
 #include <signal.h>
 #include "ocstack.h"
@@ -141,6 +142,43 @@ const char *getResult(OCStackResult result) {
     }
 }
 
+#ifdef MULTIPLE_OWNER
+
+#include <assert.h>
+#include <thread>
+#include <chrono>
+
+static bool volatile g_LoopFlag;
+static std::thread* oc_process_thread;
+
+static void oc_process_loop()
+{
+    while(g_LoopFlag)
+    {
+        if (OCProcess() != OC_STACK_OK)
+        {
+            OIC_LOG(ERROR, TAG, "OCStack process error");
+            g_LoopFlag = false;
+            break;
+        }
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    }
+}
+
+static void StartOCProcessThread()
+{
+    g_LoopFlag = true;
+    oc_process_thread = new std::thread(oc_process_loop);
+}
+
+static void StopOCProcessThread()
+{
+    assert(oc_process_thread->joinable() == true);
+    g_LoopFlag = false;
+    oc_process_thread->join();
+}
+#endif //MULTIPLE_OWNER
+
 OCRepPayload* getPayload(const char* uri, int64_t power, bool state)
 {
     OCRepPayload* payload = OCRepPayloadCreate();
@@ -276,7 +314,7 @@ OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest,
                 gLedInstance[gCurrLedInstance].state = 0;
                 gLedInstance[gCurrLedInstance].power = 0;
                 gCurrLedInstance++;
-                strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH);
+                strncpy ((char *)response->resourceUri, newLedUri, sizeof(response->resourceUri));
                 ehResult = OC_EH_RESOURCE_CREATED;
             }
         }
@@ -422,14 +460,19 @@ void GeneratePinCB(char* pin, size_t pinSize)
     OIC_LOG(INFO, TAG, "============================");
 }
 
-int main()
+void ClosePinDisplayCB(void)
 {
-    struct timespec timeout;
+    OIC_LOG(INFO, TAG, "============================");
+    OIC_LOG(INFO, TAG, "    PIN DISPLAY CLOSED.");
+    OIC_LOG(INFO, TAG, "============================");
+}
 
+int main()
+{
     OIC_LOG(DEBUG, TAG, "OCServer is starting...");
 
     // Initialize Persistent Storage for SVR database
-    OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink};
+    OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink, NULL, NULL};
     OCRegisterPersistentStorageHandler(&ps);
 
     if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK)
@@ -442,19 +485,60 @@ int main()
      * If server supported random pin based ownership transfer,
      * callback of print PIN should be registered before runing server.
      */
-    SetGeneratePinCB(&GeneratePinCB);
+    SetGeneratePinCB(GeneratePinCB);
+
+    /**
+     * 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.
+     */
+    SetClosePinDisplayCB(ClosePinDisplayCB);
+
+    /**
+     * Random PIN generation policy can be changed through SetRandomPinPolicy() API.
+     * first param : byte length of random PIN ( 4 <= first param <= 32)
+     * second param : PIN type (This is bitmask)
+     */
+    if(OC_STACK_OK != SetRandomPinPolicy(8, NUM_PIN))
+    {
+        OIC_LOG(ERROR, TAG, "Failed to setting PIN policy");
+        return 0;
+    }
 
     /*
      * Declare and create the example resource: LED
      */
     createLEDResource(gResourceUri, &LED, false, 0);
 
-    timeout.tv_sec  = 0;
-    timeout.tv_nsec = 100000000L;
-
     // Break from loop with Ctrl-C
     OIC_LOG(INFO, TAG, "Entering ocserver main loop...");
     signal(SIGINT, handleSigInt);
+
+#ifdef MULTIPLE_OWNER
+    StartOCProcessThread();
+
+    while(!gQuitFlag)
+    {
+        printf("Press 'G' to generate random PIN...\n");
+        printf("Press 'E' to exit...\n");
+        char in = getchar();
+        if('G' == in || 'g' == in)
+        {
+            char ranPin[OXM_RANDOM_PIN_MAX_SIZE + 1] = {0};
+            GeneratePin(ranPin, sizeof(ranPin));
+        }
+        if('E' == in || 'e' == in)
+        {
+            break;
+        }
+    }
+
+    StopOCProcessThread();
+#else
+    struct timespec timeout;
+    timeout.tv_sec  = 0;
+    timeout.tv_nsec = 100000000L;
+
     while (!gQuitFlag)
     {
         if (OCProcess() != OC_STACK_OK)
@@ -464,6 +548,7 @@ int main()
         }
         nanosleep(&timeout, NULL);
     }
+#endif //MULTIPLE_OWNER
 
     OIC_LOG(INFO, TAG, "Exiting ocserver main loop...");