Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / richsdk_sample / mediator_cpp.cpp
index 289c07f..c94bc05 100755 (executable)
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
 #include <iostream>
-#include<stdio.h>
+#include <condition_variable>
 
-#include "oic_string.h"
-#include "EasySetup.h"
-#include "ESRichCommon.h"
 #include "OCPlatform.h"
-#include "logger.h"
+#include "OCApi.h"
 #include "OCProvisioningManager.h"
 
+#include "EasySetup.h"
+#include "ESRichCommon.h"
+
 #define ES_SAMPLE_APP_TAG "ES_SAMPLE_APP_TAG"
 #define DECLARE_MENU(FUNC, ...) { #FUNC, FUNC }
 
 using namespace OC;
 using namespace OIC::Service;
 
-static EasySetup *easySetupIntance = nullptr;
 static std::shared_ptr<RemoteEnrollee> remoteEnrollee = nullptr;
+static std::shared_ptr<OC::OCResource> curResource = nullptr;
 
-static std::string ipaddress, ssid, pwd;
-char security;
+static std::mutex g_discoverymtx;
+static std::condition_variable g_cond;
+
+#define PROV_RESOURCE_TYPE "ocf.wk.prov"
 
 typedef void (*Runner)();
 
@@ -64,204 +66,279 @@ int processUserInput(int min = std::numeric_limits<int>::min(),
     throw std::runtime_error("Invalid Input, please try again");
 }
 
-void printPropertyData(PropertyData propData)
+void printConfiguration(EnrolleeConf conf)
 {
     cout << "===========================================" << endl;
-    DeviceConfig devConfig = propData.getDevConf();
-    NetworkInfo netInfo = propData.getNetInfo();
-    bool cloudable = propData.isCloudable();
-
-    cout << "\tDeviceConfig.id : " << devConfig.id << endl;
-    cout << "\tDeviceConfig.name : " << devConfig.name << endl;
-    cout << "\tDeviceConfig.language : " << devConfig.language << endl;
-    cout << "\tDeviceConfig.country : " << devConfig.country << endl;
+    cout << "\tDevice Name : " << conf.getDeviceName() << endl;
 
-    for(auto type = netInfo.types.begin(); type != netInfo.types.end(); ++type)
+    for(auto it : conf.getWiFiModes())
     {
-        cout << "\tnetInfo.types : " << static_cast<int>(*type) << endl;
+        cout << "\tSupported WiFi modes : " << it << endl;
     }
-    cout << "\tnetInfo.freq : " << static_cast<int>(netInfo.freq) << endl;
+
+    cout << "\tSupported WiFi freq : " << static_cast<int>(conf.getWiFiFreq()) << endl;
+    cout << "\tCloud accessibility: " << conf.isCloudAccessible() << endl;
     cout << "===========================================" << endl;
 }
 
-void RequestPropertyDataStatusCallback(std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus)
+void printStatus(EnrolleeStatus status)
 {
-    cout << "Enter RequestPropertyDataStatusCb." << endl;
+    cout << "===========================================" << endl;
+    cout << "\tProvStatus : " << status.getProvStatus() << endl;
+    cout << "\tLastErrCode : " << status.getLastErrCode() << endl;
+    cout << "===========================================" << endl;
+}
 
-    if(requestPropertyDataStatus->getESResult() != ES_OK)
+void provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
+{
+    if(secProvisioningStatus->getESResult() != ES_OK)
     {
-      cout << "requestPropertyDataStatus is failed." << endl;
+      cout << "provisionSecurity is failed." << endl;
       return;
     }
     else
     {
-      cout << "requestPropertyDataStatus is success." << endl;
-      printPropertyData(requestPropertyDataStatus->getPropertyData());
+      cout << "provisionSecurity is success." << endl;
+      cout << "uuid : " << secProvisioningStatus->getDeviceUUID()<< endl;
     }
 }
 
-void dataProvisioningStatusCallback(std::shared_ptr< DataProvisioningStatus > provStatus)
+void provisionSecurity()
 {
-    cout << "Enter dataProvisioningStatusCallback." << endl;
+    remoteEnrollee->provisionSecurity(provisionSecurityStatusCallback);
+}
 
-    if(provStatus->getESResult() != ES_OK)
+void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
+{
+    if(getEnrolleeStatus->getESResult() != ES_OK)
     {
-      cout << "dataProvisioningStatusCallback is failed." << endl;
+      cout << "getStatus is failed." << endl;
       return;
     }
     else
     {
-      cout << "dataProvisioningStatusCallback is success." << endl;
-      cout << "ESDataProvState : " << provStatus->getESDataProvState() << endl;
+      cout << "getStatus is success." << endl;
+      printStatus(getEnrolleeStatus->getEnrolleeStatus());
     }
 }
 
-void cloudProvisioningStatusCallback(std::shared_ptr< CloudProvisioningStatus > status)
-{
-    cout << "Enter cloudProvisioningStatusCallback." << endl;
-    cout << "CloudProvStatus : " <<  status->getESCloudState() << endl;
-}
 
-void createRemoteEnrollee()
+void getStatus()
 {
-    easySetupIntance = EasySetup::getInstance();
+    if(!remoteEnrollee)
+        return;
+
     try
     {
-        remoteEnrollee = easySetupIntance->createRemoteEnrollee();
+        remoteEnrollee->getStatus(getStatusCallback);
     }
     catch (OCException &e)
     {
-        std::cout << "Exception during createEnrolleeDevice call" << e.reason();
+        std::cout << "Exception during getConfiguration call" << e.reason();
         return;
     }
-    cout << "createRemoteEnrollee is success." << endl;
 }
 
-void initRemoteEnrollee()
+void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
 {
-    try
+    if(getConfigurationStatus->getESResult() != ES_OK)
     {
-        remoteEnrollee->initRemoteEnrollee();
+      cout << "GetConfigurationStatus is failed." << endl;
+      return;
     }
-    catch (OCException &e)
+    else
     {
-        std::cout << "Exception during initRemoteEnrollee call" << e.reason();
-        return;
+      cout << "GetConfigurationStatus is success." << endl;
+      printConfiguration(getConfigurationStatus->getEnrolleeConf());
     }
 }
 
-void requestPropertyData()
+void getConfiguration()
 {
+    if(!remoteEnrollee)
+        return;
+
     try
     {
-        remoteEnrollee->requestPropertyData(RequestPropertyDataStatusCallback);
+        remoteEnrollee->getConfiguration(getConfigurationCallback);
     }
     catch (OCException &e)
     {
-        std::cout << "Exception during requestPropertyData call" << e.reason();
+        std::cout << "Exception during getConfiguration call" << e.reason();
         return;
     }
 }
 
-void setDataProvInfo()
+void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
 {
-    DataProvInfo dataProvInfo;
-    dataProvInfo.WIFI.ssid = "Iotivity_2.4G";
-    dataProvInfo.WIFI.pwd = "1234567890";
-    dataProvInfo.WIFI.authtype = WPA2_PSK;
-    dataProvInfo.WIFI.enctype = TKIP_AES;
-    dataProvInfo.Device.language = "korean";
-    dataProvInfo.Device.country = "korea";
-
-    remoteEnrollee->setDataProvInfo(dataProvInfo);
+    if(provStatus->getESResult() != ES_OK)
+    {
+      cout << "Device Provisioning is failed." << endl;
+      return;
+    }
+    else
+    {
+      cout << "Device Provisioning is success." << endl;
+    }
 }
 
-void setCloudProvInfo()
+void provisionDeviceProperty()
 {
-    CloudProvInfo cloudProvInfo;
-    cloudProvInfo.authCode = "authCode";
-    cloudProvInfo.authProvider = "authProvider";
-    cloudProvInfo.ciServer = "ciServer";
+    if(!remoteEnrollee)
+        return;
 
-    remoteEnrollee->setCloudProvInfo(cloudProvInfo);
-}
+    DeviceProp devProp;
+    devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
+    devProp.setDevConfProp("korean", "Korea");
 
-void startDataProvisioning()
-{
     try
     {
-        remoteEnrollee->startDataProvisioning(dataProvisioningStatusCallback);
+        //remoteEnrollee->provisionDeviceProperties(deviceProp, deviceProvisioningStatusCallback);
+        remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCallback);
     }
     catch (OCException &e)
     {
-        std::cout << "Exception during startDataProvisioning call" << e.reason();
+        std::cout << "Exception during provisionDeviceProperties call" << e.reason();
         return;
     }
 }
 
-void startCloudProvisioning()
+void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
 {
+    switch (provStatus->getESCloudState())
+    {
+        case ES_CLOUD_PROVISIONING_ERROR:
+            cout << "Cloud Provisioning is failed." << endl;
+            break;
+        case ES_CLOUD_PROVISIONING_SUCCESS:
+            cout << "Cloud Provisioning is success." << endl;
+            break;
+        case ES_CLOUD_ENROLLEE_FOUND:
+            cout << "Enrollee is found in a given network." << endl;
+            break;
+        case ES_CLOUD_ENROLLEE_NOT_FOUND:
+            cout << "Enrollee is not found in a given network." << endl;
+            break;
+    }
+}
+
+void provisionCloudProperty()
+{
+    if(!remoteEnrollee)
+        return;
+
+    CloudProp cloudProp;
+    cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
+    cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
+
     try
     {
-        remoteEnrollee->startCloudProvisioning(cloudProvisioningStatusCallback);
+        remoteEnrollee->provisionCloudProperties(cloudProp, cloudProvisioningStatusCallback);
     }
     catch (OCException &e)
     {
-        std::cout << "Exception during startDataProvisioning call" << e.reason();
+        std::cout << "Exception during provisionCloudProperties call" << e.reason();
         return;
     }
 }
 
 void DisplayMenu()
 {
-    constexpr int CREATE_REMOTE_ENROLLEE = 1;
-    constexpr int EASY_SETUP_INIT = 2;
-    constexpr int REQUEST_PROPERTY_DATA = 3;
-    constexpr int SET_DATA_PROVISIONING_INFO = 4;
-    constexpr int START_DATA_PROVISIONING = 5;
-    constexpr int SET_CLOUD_PROVISIONING_INFO = 6;
-    constexpr int START_CLOUD_PROVISIONING = 7;
+    constexpr int PROVISION_SECURITY = 1;
+    constexpr int GET_STATUS = 2;
+    constexpr int GET_CONFIGURATION = 3;
+    constexpr int PROVISION_DEVICE_PROPERTY = 4;
+    constexpr int PROVISION_CLOUD_PROPERTY = 5;
 
     std::cout << "========================================================\n";
-    std::cout << CREATE_REMOTE_ENROLLEE << ". Create Remote Enrollee                    \n";
-    std::cout << EASY_SETUP_INIT << ". Easy Setup Init                    \n";
-    std::cout << REQUEST_PROPERTY_DATA << ". Request PropertyData              \n";
-    std::cout << SET_DATA_PROVISIONING_INFO << ". Set Data Provisioning Info              \n";
-    std::cout << START_DATA_PROVISIONING << ". Start Data Provisioning              \n";
-    std::cout << SET_CLOUD_PROVISIONING_INFO << ". Set Cloud Provisioning Info              \n";
-    std::cout << START_CLOUD_PROVISIONING << ". Start Cloud Provisioning              \n";
+    std::cout << PROVISION_SECURITY << ". Provision Security to Enrollee  \n";
+    std::cout << GET_STATUS << ". Get Status from Enrollee  \n";
+    std::cout << GET_CONFIGURATION << ". Get Configuration from Enrollee  \n";
+    std::cout << PROVISION_DEVICE_PROPERTY << ". Provision Device Property\n";
+    std::cout << PROVISION_CLOUD_PROPERTY << ". Provision Cloud Property  \n";
     std::cout << "========================================================\n";
 
-    int selection = processUserInput(CREATE_REMOTE_ENROLLEE, START_CLOUD_PROVISIONING);
+    int selection = processUserInput(PROVISION_SECURITY, PROVISION_CLOUD_PROPERTY);
 
     switch (selection)
     {
-        case CREATE_REMOTE_ENROLLEE:
-            createRemoteEnrollee();
-            break;
-        case EASY_SETUP_INIT:
-            initRemoteEnrollee();
+        case PROVISION_SECURITY:
+            provisionSecurity();
             break;
-        case REQUEST_PROPERTY_DATA:
-            requestPropertyData();
+        case GET_STATUS:
+            getStatus();
             break;
-        case SET_DATA_PROVISIONING_INFO:
-            setDataProvInfo();
+        case GET_CONFIGURATION:
+            getConfiguration();
             break;
-        case START_DATA_PROVISIONING:
-            startDataProvisioning();
+        case PROVISION_DEVICE_PROPERTY:
+            provisionDeviceProperty();
             break;
-        case SET_CLOUD_PROVISIONING_INFO:
-            setCloudProvInfo();
-            break;
-        case START_CLOUD_PROVISIONING:
-            startCloudProvisioning();
+        case PROVISION_CLOUD_PROPERTY:
+            provisionCloudProperty();
             break;
         default:
             break;
     };
 }
 
+// Callback to found resources
+void foundResource(std::shared_ptr<OC::OCResource> resource)
+{
+    std::string resourceURI;
+    std::string hostAddress;
+    try
+    {
+        // Do some operations with resource object.
+        if(resource &&
+           !curResource &&
+           resource->getResourceTypes().at(0) == PROV_RESOURCE_TYPE)
+        {
+            std::cout<<"DISCOVERED Resource:"<<std::endl;
+            // Get the resource URI
+            resourceURI = resource->uri();
+            std::cout << "\tURI of the resource: " << resourceURI << std::endl;
+
+            // Get the resource host address
+            hostAddress = resource->host();
+            std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
+
+            // Get the resource types
+            std::cout << "\tList of resource types: " << std::endl;
+            for(auto &resourceTypes : resource->getResourceTypes())
+            {
+                std::cout << "\t\t" << resourceTypes << std::endl;
+            }
+
+            // Get the resource interfaces
+            std::cout << "\tList of resource interfaces: " << std::endl;
+            for(auto &resourceInterfaces : resource->getResourceInterfaces())
+            {
+                std::cout << "\t\t" << resourceInterfaces << std::endl;
+            }
+
+            if(curResource == nullptr)
+            {
+                remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
+                if(!remoteEnrollee)
+                {
+                    std::cout << "RemoteEnrollee object is failed for some reasons!" << std::endl;
+                }
+                else
+                {
+                    curResource = resource;
+                    std::cout << "RemoteEnrollee object is successfully created!" << std::endl;
+                    g_cond.notify_all();
+                }
+            }
+        }
+    }
+    catch(std::exception& e)
+    {
+        std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
+    }
+}
+
 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
 {
     (void)UNUSED_PARAM;
@@ -270,6 +347,7 @@ static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
 
 int main()
 {
+    std::ostringstream requestURI;
     OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink };
 
     PlatformConfig config
@@ -289,6 +367,21 @@ int main()
     }
 #endif
 
+    try
+    {
+        requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << PROV_RESOURCE_TYPE;
+
+        OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource);
+        std::cout<< "Finding Resource... " <<std::endl;
+
+        std::unique_lock<std::mutex> lck(g_discoverymtx);
+        g_cond.wait_for(lck, std::chrono::seconds(4));
+
+    }catch(OCException& e)
+    {
+        oclog() << "Exception in main: "<<e.what();
+    }
+
     while (true)
     {
         try