Merge all changes of easy setup in master branch to 1.2-rel branch
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / mediator / linux / richsdk_sample / mediator_cpp.cpp
index 135715e..144685d 100755 (executable)
@@ -42,8 +42,6 @@ static std::shared_ptr<OC::OCResource> curResource = nullptr;
 static std::mutex g_discoverymtx;
 static std::condition_variable g_cond;
 
-#define PROV_RESOURCE_TYPE "ocf.wk.prov"
-
 typedef void (*Runner)();
 
 Runner g_currentRun;
@@ -69,27 +67,95 @@ int processUserInput(int min = std::numeric_limits<int>::min(),
 void printConfiguration(EnrolleeConf conf)
 {
     cout << "===========================================" << endl;
-    DeviceConfig devConfig = conf.getDevConf();
-    WiFiConfig wifiConfig = conf.getWiFiConf();
-
-    cout << "\tDeviceConfig.name : " << devConfig.name << endl;
-    cout << "\tDeviceConfig.language : " << devConfig.language << endl;
-    cout << "\tDeviceConfig.country : " << devConfig.country << endl;
+    cout << "\tDevice Name : " << conf.getDeviceName() << endl;
+    cout << "\tModel Number : " << conf.getModelNumber() << endl;
 
-    for(auto mode = wifiConfig.modes.begin(); mode != wifiConfig.modes.end(); ++mode)
+    for(auto it : conf.getWiFiModes())
     {
-        cout << "\tnetInfo.modes : " << static_cast<int>(*mode) << endl;
+        cout << "\tSupported WiFi modes : " << it << endl;
     }
-    cout << "\tnetInfo.freq : " << static_cast<int>(wifiConfig.freq) << endl;
+
+    cout << "\tSupported WiFi freq : " << static_cast<int>(conf.getWiFiFreq()) << endl;
+    cout << "\tCloud accessibility: " << conf.isCloudAccessible() << endl;
+    cout << "===========================================" << endl;
+}
+
+void printStatus(EnrolleeStatus status)
+{
+    cout << "===========================================" << endl;
+    cout << "\tProvStatus : " << status.getProvStatus() << endl;
+    cout << "\tLastErrCode : " << status.getLastErrCode() << endl;
     cout << "===========================================" << endl;
 }
 
+void provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
+{
+    if(secProvisioningStatus->getESResult() != ES_OK)
+    {
+      cout << "provisionSecurity is failed." << endl;
+      return;
+    }
+    else
+    {
+      cout << "provisionSecurity is success." << endl;
+      cout << "uuid : " << secProvisioningStatus->getDeviceUUID()<< endl;
+    }
+}
+
 void provisionSecurity()
 {
-    // TODO
+    if(!remoteEnrollee)
+    {
+        std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
+        return;
+    }
+
+    try
+    {
+        remoteEnrollee->provisionSecurity(provisionSecurityStatusCallback);
+    }
+    catch (OCException &e)
+    {
+        std::cout << "Exception during provisionSecurity call" << e.reason();
+        return;
+    }
 }
 
-void GetConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
+void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
+{
+    if(getEnrolleeStatus->getESResult() != ES_OK)
+    {
+      cout << "getStatus is failed." << endl;
+      return;
+    }
+    else
+    {
+      cout << "getStatus is success." << endl;
+      printStatus(getEnrolleeStatus->getEnrolleeStatus());
+    }
+}
+
+
+void getStatus()
+{
+    if(!remoteEnrollee)
+    {
+        std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
+        return;
+    }
+
+    try
+    {
+        remoteEnrollee->getStatus(getStatusCallback);
+    }
+    catch (OCException &e)
+    {
+        std::cout << "Exception during getConfiguration call" << e.reason();
+        return;
+    }
+}
+
+void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
 {
     if(getConfigurationStatus->getESResult() != ES_OK)
     {
@@ -106,11 +172,14 @@ void GetConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfi
 void getConfiguration()
 {
     if(!remoteEnrollee)
+    {
+        std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
+    }
 
     try
     {
-        remoteEnrollee->getConfiguration(GetConfigurationCallback);
+        remoteEnrollee->getConfiguration(getConfigurationCallback);
     }
     catch (OCException &e)
     {
@@ -135,19 +204,18 @@ void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningSta
 void provisionDeviceProperty()
 {
     if(!remoteEnrollee)
+    {
+        std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
+    }
 
-    DeviceProp deviceProp;
-    deviceProp.WIFI.ssid = "Iotivity_SSID";
-    deviceProp.WIFI.pwd = "Iotivity_PWD";
-    deviceProp.WIFI.authtype = WPA2_PSK;
-    deviceProp.WIFI.enctype = TKIP_AES;
-    deviceProp.Device.language = "korean";
-    deviceProp.Device.country = "Korea";
+    DeviceProp devProp;
+    devProp.setWiFiProp("Iotivity_SSID", "Iotivity_PWD", WPA2_PSK, TKIP_AES);
+    devProp.setDevConfProp("korean", "Korea", "Location");
 
     try
     {
-        remoteEnrollee->provisionDeviceProperties(deviceProp, deviceProvisioningStatusCallback);
+        remoteEnrollee->provisionDeviceProperties(devProp, deviceProvisioningStatusCallback);
     }
     catch (OCException &e)
     {
@@ -158,32 +226,38 @@ void provisionDeviceProperty()
 
 void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
 {
-    switch (provStatus->getESCloudState())
+    switch (provStatus->getESResult())
     {
-        case ES_CLOUD_PROVISIONING_ERROR:
-            cout << "Cloud Provisioning is failed." << endl;
-            break;
-        case ES_CLOUD_PROVISIONING_SUCCESS:
+        case ES_OK:
             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:
+        case ES_SECURE_RESOURCE_DISCOVERY_FAILURE:
             cout << "Enrollee is not found in a given network." << endl;
             break;
+        case ES_ACL_PROVISIONING_FAILURE:
+            cout << "ACL provisioning is failed." << endl;
+            break;
+        case ES_CERT_PROVISIONING_FAILURE:
+            cout << "CERT provisioning is failed." << endl;
+            break;
+        default:
+            cout << "Cloud Provisioning is failed." << endl;
+            break;
     }
 }
 
 void provisionCloudProperty()
 {
     if(!remoteEnrollee)
+    {
+        std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
+    }
 
     CloudProp cloudProp;
-    cloudProp.authCode = "authCode";
-    cloudProp.authProvider = "authProvider";
-    cloudProp.ciServer = "ciServer";
+    cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
+    cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
+    cloudProp.setCredID(1);
 
     try
     {
@@ -196,41 +270,6 @@ void provisionCloudProperty()
     }
 }
 
-void DisplayMenu()
-{
-    constexpr int PROVISION_SECURITY = 1;
-    constexpr int GET_CONFIGURATION = 2;
-    constexpr int PROVISION_DEVICE_PROPERTY = 3;
-    constexpr int PROVISION_CLOUD_PROPERTY = 4;
-
-    std::cout << "========================================================\n";
-    std::cout << PROVISION_SECURITY << ". Provision Security to 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(PROVISION_SECURITY, PROVISION_CLOUD_PROPERTY);
-
-    switch (selection)
-    {
-        case PROVISION_SECURITY:
-            provisionSecurity();
-            break;
-        case GET_CONFIGURATION:
-            getConfiguration();
-            break;
-        case PROVISION_DEVICE_PROPERTY:
-            provisionDeviceProperty();
-            break;
-        case PROVISION_CLOUD_PROPERTY:
-            provisionCloudProperty();
-            break;
-        default:
-            break;
-    };
-}
-
 // Callback to found resources
 void foundResource(std::shared_ptr<OC::OCResource> resource)
 {
@@ -241,7 +280,7 @@ void foundResource(std::shared_ptr<OC::OCResource> resource)
         // Do some operations with resource object.
         if(resource &&
            !curResource &&
-           resource->getResourceTypes().at(0) == PROV_RESOURCE_TYPE)
+           resource->getResourceTypes().at(0) == OC_RSRVD_ES_RES_TYPE_PROV)
         {
             std::cout<<"DISCOVERED Resource:"<<std::endl;
             // Get the resource URI
@@ -288,6 +327,69 @@ void foundResource(std::shared_ptr<OC::OCResource> resource)
     }
 }
 
+void discoveryEnrolleeResource()
+{
+       try
+       {
+           std::ostringstream requestURI;
+        requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << OC_RSRVD_ES_RES_TYPE_PROV;
+        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(5));
+       }
+       catch (OCException& e)
+       {
+               std::cout << "Exception in discoveryEnrolleeResource: "<<e.what();
+       }
+}
+
+void DisplayMenu()
+{
+       constexpr int DISCOVERY_ENROLLEE = 1;
+    constexpr int PROVISION_SECURITY = 2;
+    constexpr int GET_STATUS = 3;
+    constexpr int GET_CONFIGURATION = 4;
+    constexpr int PROVISION_DEVICE_PROPERTY = 5;
+    constexpr int PROVISION_CLOUD_PROPERTY = 6;
+
+    std::cout << "========================================================\n";
+    std::cout << DISCOVERY_ENROLLEE << ". Discovery Enrollee Resource \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(DISCOVERY_ENROLLEE, PROVISION_CLOUD_PROPERTY);
+
+    switch (selection)
+    {
+        case DISCOVERY_ENROLLEE:
+            discoveryEnrolleeResource();
+            break;
+        case PROVISION_SECURITY:
+            provisionSecurity();
+            break;
+        case GET_STATUS:
+            getStatus();
+            break;
+        case GET_CONFIGURATION:
+            getConfiguration();
+            break;
+        case PROVISION_DEVICE_PROPERTY:
+            provisionDeviceProperty();
+            break;
+        case PROVISION_CLOUD_PROPERTY:
+            provisionCloudProperty();
+            break;
+        default:
+            break;
+    };
+}
+
 static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
 {
     (void)UNUSED_PARAM;
@@ -296,39 +398,29 @@ 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
     {
-        OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos, &ps
+        OC::ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::HighQos, &ps
     };
 
     OCPlatform::Configure(config);
 
-#ifdef __WITH_DTLS__
-    //Initializing the provisioning client stack using the db path provided by the application.
-    OCStackResult result = OCSecure::provisionInit("");
-
-    if (result != OC_STACK_OK)
-    {
-        return -1;
-    }
-#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));
+#ifdef __WITH_DTLS__
+        //Initializing the provisioning client stack using the db path provided by the application.
+        OCStackResult result = OCSecure::provisionInit("");
 
+        if (result != OC_STACK_OK)
+        {
+            return -1;
+        }
+#endif
     }catch(OCException& e)
     {
-        oclog() << "Exception in main: "<<e.what();
+        std::cout << "Exception in main: "<<e.what();
     }
 
     while (true)