replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / stack / samples / linux / secure / occlientdirectpairing.cpp
index fd7d8ee..74dd822 100644 (file)
 //\r
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
 \r
+#include "iotivity_config.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <string.h>\r
 #include <signal.h>\r
+#ifdef HAVE_UNISTD_H\r
 #include <unistd.h>\r
+#endif\r
+#ifdef HAVE_PTHREAD_H\r
 #include <pthread.h>\r
+#endif\r
 #include <iostream>\r
 #include <sstream>\r
+#if defined(HAVE_WINDOWS_H)\r
+#include <windows.h>\r
+/** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */\r
+#ifdef ERROR\r
+#undef ERROR\r
+#endif\r
+#endif // defined(HAVE_WINDOWS_H)\r
 #include "ocstack.h"\r
 #include "logger.h"\r
 #include "ocpayload.h"\r
@@ -49,8 +61,8 @@ static char DISCOVERY_QUERY[] = "%s/oic/res";
 //Secure Virtual Resource database for Iotivity Client application\r
 //It contains Client's Identity and the PSK credentials\r
 //of other devices which the client trusts\r
-static char CRED_FILE[] = "oic_svr_db_client_directpairing.dat";
-
+static char CRED_FILE[] = "oic_svr_db_client_directpairing.dat";\r
+\r
 static const OCDPDev_t *discoveredDevs = NULL;\r
 static const OCDPDev_t *pairedDevs = NULL;\r
 \r
@@ -270,10 +282,11 @@ OCStackApplicationResult discoveryReqCB(void*, OCDoHandle,
 }\r
 \r
 // This is a function called back when direct-pairing status is changed\r
-void pairingReqCB(OCDPDev_t* peer, OCStackResult result)\r
+void pairingReqCB(void *ctx, OCDPDev_t* peer, OCStackResult result)\r
 {\r
     OIC_LOG(INFO, TAG, "Callback Context for Direct-Pairing establishment\n");\r
 \r
+    (void) ctx;\r
     if (OC_STACK_OK == result)\r
     {\r
         OIC_LOG_V(INFO, TAG,\r
@@ -322,8 +335,8 @@ int DeviceDiscovery()
     /* Start a discovery query*/\r
     OIC_LOG_V(INFO, TAG, "Resource Discovery : %s\n", queryUri);\r
 \r
-    ret = OCDoResource(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,\r
-                       OC_LOW_QOS, &cbData, NULL, 0);\r
+    ret = OCDoRequest(NULL, OC_REST_DISCOVER, queryUri, 0, 0, CT_DEFAULT,\r
+                      OC_LOW_QOS, &cbData, NULL, 0);\r
     if (ret != OC_STACK_OK)\r
     {\r
         OIC_LOG(ERROR, TAG, "OCStack resource error");\r
@@ -359,7 +372,7 @@ OCStackResult DoDirectPairing(OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber)
 \r
     // start direct pairing\r
     OIC_LOG(INFO, TAG, "   Start Direct Pairing..");\r
-    if(OC_STACK_OK != OCDoDirectPairing(peer, pmSel, pinNumber, pairingReqCB))\r
+    if(OC_STACK_OK != OCDoDirectPairing(NULL, peer, pmSel, pinNumber, pairingReqCB))\r
     {\r
         OIC_LOG(ERROR, TAG, "OCDoDirectPairing API error");\r
         return OC_STACK_ERROR;\r
@@ -387,8 +400,8 @@ OCStackResult SendGetRequest(OCDPDev_t* peer)
     cbData.cd = NULL;\r
 \r
     OIC_LOG(INFO, TAG, "Request to /a/light ");\r
-    ret = OCDoResource(&handle, OC_REST_GET, szQueryUri,\r
-               &endpoint, NULL, peer->connType, OC_LOW_QOS, &cbData, NULL, 0);\r
+    ret = OCDoRequest(&handle, OC_REST_GET, szQueryUri,\r
+                      &endpoint, NULL, peer->connType, OC_LOW_QOS, &cbData, NULL, 0);\r
     if (ret != OC_STACK_OK)\r
     {\r
         OIC_LOG_V(ERROR, TAG, "OCDoResource returns error %d with method %d", ret, OC_REST_GET);\r
@@ -405,19 +418,19 @@ FILE* client_fopen(const char *path, const char *mode)
 \r
 void *CLInterface(void *data)\r
 {\r
-    printf(RED_BEGIN"#Ready to operation ('h' for help)#\n"COLOR_END);\r
+    printf(RED_BEGIN "#Ready to operation ('h' for help)#\n" COLOR_END);\r
 \r
     (void)data;\r
     OCStackResult ret;\r
-    char query[MAX_LINE] = {0,};\r
-    const char prompt[] = BOLD_BEGIN"IoTivity-DP#"COLOR_END" ";\r
+    char query[MAX_LINE] = {0};\r
+    const char prompt[] = BOLD_BEGIN "IoTivity-DP#" COLOR_END" ";\r
     const char* helpmsg[6] = {\r
-            GREEN_BEGIN"# h  (or help) : show help message"COLOR_END,\r
-            GREEN_BEGIN"# dd (DP device discovery) : discover Direct-Pairing devices"COLOR_END,\r
-            GREEN_BEGIN"# dp (start Direct-Pairing) : negotiate DP method & start Direct-Pairing"COLOR_END,\r
-            GREEN_BEGIN"# sd (send data) : send data to device"COLOR_END,\r
-            GREEN_BEGIN"# ll (list all device) : list all discovered/paired devices"COLOR_END,\r
-            GREEN_BEGIN"# q  (quit) : quit test"COLOR_END,\r
+            GREEN_BEGIN "# h  (or help) : show help message" COLOR_END,\r
+            GREEN_BEGIN "# dd (DP device discovery) : discover Direct-Pairing devices" COLOR_END,\r
+            GREEN_BEGIN "# dp (start Direct-Pairing) : negotiate DP method & start Direct-Pairing" COLOR_END,\r
+            GREEN_BEGIN "# sd (send data) : send data to device" COLOR_END,\r
+            GREEN_BEGIN "# ll (list all device) : list all discovered/paired devices" COLOR_END,\r
+            GREEN_BEGIN "# q  (quit) : quit test" COLOR_END,\r
         };\r
 \r
     for (size_t i=0; i<(sizeof(helpmsg)/sizeof(char*)); i++)\r
@@ -514,8 +527,8 @@ void *CLInterface(void *data)
                     OIC_LOG(ERROR, TAG, "Invalid PIN");\r
                     continue;\r
                 }\r
-                sscanf(input, "%8s", pinNumber);
-                printf("\n");
+                sscanf(input, "%8s", pinNumber);\r
+                printf("\n");\r
 \r
                 ret = DoDirectPairing(peer, pmSel, pinNumber);\r
                 if (OC_STACK_OK != ret)\r
@@ -625,7 +638,12 @@ int main(void)
             return 0;\r
         }\r
 \r
+#if defined(_WIN32)\r
+        Sleep(100);\r
+#else\r
         nanosleep(&timeout, NULL);\r
+#endif // defined(_WIN32)\r
+\r
     }\r
     OIC_LOG(INFO, TAG, "Exiting occlient main loop...");\r
 \r