[IOT-2036] Fix mediator sample app
authorDmitriy Zhuravlev <d.zhuravlev@samsung.com>
Fri, 5 May 2017 15:45:12 +0000 (18:45 +0300)
committerRandeep Singh <randeep.s@samsung.com>
Thu, 11 May 2017 11:27:43 +0000 (11:27 +0000)
In 1.2.1 secure port was common for all secure resources.
Update sample to use OCF 1.0 endpoint information.

Change-Id: I105f8097c69b15a89b7b799817e2887b575f2770
Signed-off-by: Dmitriy Zhuravlev <d.zhuravlev@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19667
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
service/easy-setup/sampleapp/mediator/linux/richsdk_sample/mediator.cpp [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index 976ec97..d559106
@@ -48,7 +48,7 @@ typedef void (*Runner)();
 Runner g_currentRun;
 
 int processUserInput(int min = std::numeric_limits<int>::min(),
-        int max = std::numeric_limits<int>::max())
+                     int max = std::numeric_limits<int>::max())
 {
     assert(min <= max);
 
@@ -68,14 +68,14 @@ int processUserInput(int min = std::numeric_limits<int>::min(),
     throw std::runtime_error("Invalid Input, please try again");
 }
 
-void printConfiguration(const EnrolleeConfconf)
+void printConfiguration(const EnrolleeConf &conf)
 {
     cout << "===========================================" << endl;
     cout << "\tProvStatus : " << conf.getProvStatus() << endl;
     cout << "\tLastErrCode : " << conf.getLastErrCode() << endl;
     cout << "\tDevice Name : " << conf.getDeviceName() << endl;
 
-    for(auto it : conf.getWiFiModes())
+    for (auto it : conf.getWiFiModes())
     {
         cout << "\tSupported WiFi modes : " << it << endl;
     }
@@ -85,7 +85,7 @@ void printConfiguration(const EnrolleeConf& conf)
     cout << "===========================================" << endl;
 }
 
-void printStatus(const EnrolleeStatusstatus)
+void printStatus(const EnrolleeStatus &status)
 {
     cout << "===========================================" << endl;
     cout << "\tProvStatus : " << status.getProvStatus() << endl;
@@ -93,17 +93,18 @@ void printStatus(const EnrolleeStatus& status)
     cout << "===========================================" << endl;
 }
 
-ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
+ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus>
+        secProvisioningStatus)
 {
     cout << "provisionSecurityStatusCallback IN" << endl;
     cout << "ESResult : " << secProvisioningStatus->getESResult() << std::endl;
     cout << "Device ID : " << secProvisioningStatus->getDeviceUUID() << std::endl;
 
-    if(secProvisioningStatus->getESResult() == ES_SECURE_RESOURCE_IS_DISCOVERED)
+    if (secProvisioningStatus->getESResult() == ES_SECURE_RESOURCE_IS_DISCOVERED)
     {
 #ifdef __WITH_DTLS__
         cout << "Owned Status : " << secProvisioningStatus->isOwnedDevice() << std::endl;
-        if(secProvisioningStatus->isOwnedDevice())
+        if (secProvisioningStatus->isOwnedDevice())
         {
             cout << "Owner ID : " << secProvisioningStatus->getOwnerID() << std::endl;
         }
@@ -122,9 +123,10 @@ ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvi
 #endif
 #endif
     }
-    else if(secProvisioningStatus->getESResult() == ES_OK)
+    else if (secProvisioningStatus->getESResult() == ES_OK)
     {
         cout << "provisionSecurity is success." << std::endl;
+        curResource = nullptr;
     }
     else
     {
@@ -136,7 +138,7 @@ ESOwnershipTransferData provisionSecurityStatusCallback(std::shared_ptr<SecProvi
 
 void provisionSecurity()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
@@ -155,22 +157,22 @@ void provisionSecurity()
 
 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
 {
-    if(getEnrolleeStatus->getESResult() != ES_OK)
+    if (getEnrolleeStatus->getESResult() != ES_OK)
     {
-      cout << "getStatus is failed." << endl;
-      return;
+        cout << "getStatus is failed." << endl;
+        return;
     }
     else
     {
-      cout << "getStatus is success." << endl;
-      printStatus(getEnrolleeStatus->getEnrolleeStatus());
+        cout << "getStatus is success." << endl;
+        printStatus(getEnrolleeStatus->getEnrolleeStatus());
     }
 }
 
 
 void getStatus()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
@@ -189,21 +191,21 @@ void getStatus()
 
 void getConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
 {
-    if(getConfigurationStatus->getESResult() != ES_OK)
+    if (getConfigurationStatus->getESResult() != ES_OK)
     {
-      cout << "GetConfigurationStatus is failed." << endl;
-      return;
+        cout << "GetConfigurationStatus is failed." << endl;
+        return;
     }
     else
     {
-      cout << "GetConfigurationStatus is success." << endl;
-      printConfiguration(getConfigurationStatus->getEnrolleeConf());
+        cout << "GetConfigurationStatus is success." << endl;
+        printConfiguration(getConfigurationStatus->getEnrolleeConf());
     }
 }
 
 void getConfiguration()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
@@ -222,20 +224,20 @@ void getConfiguration()
 
 void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
 {
-    if(provStatus->getESResult() != ES_OK)
+    if (provStatus->getESResult() != ES_OK)
     {
-      cout << "Device Provisioning is failed." << endl;
-      return;
+        cout << "Device Provisioning is failed." << endl;
+        return;
     }
     else
     {
-      cout << "Device Provisioning is success." << endl;
+        cout << "Device Provisioning is success." << endl;
     }
 }
 
 void provisionDeviceProperty()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
@@ -257,20 +259,20 @@ void provisionDeviceProperty()
 
 void connectRequestStatusCallback(std::shared_ptr< ConnectRequestStatus > requestStatus)
 {
-    if(requestStatus->getESResult() != ES_OK)
+    if (requestStatus->getESResult() != ES_OK)
     {
-      cout << "Request to connection is failed." << endl;
-      return;
+        cout << "Request to connection is failed." << endl;
+        return;
     }
     else
     {
-      cout << "Request to connection is success." << endl;
+        cout << "Request to connection is success." << endl;
     }
 }
 
 void requestToConnect()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
@@ -314,14 +316,15 @@ void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatu
 
 void provisionCloudProperty()
 {
-    if(!remoteEnrollee)
+    if (!remoteEnrollee)
     {
         std::cout << "RemoteEnrollee is null, retry Discovery EnrolleeResource." << endl;
         return;
     }
 
     CloudProp cloudProp;
-    cloudProp.setCloudPropWithAccessToken("accessToken", OAUTH_TOKENTYPE_BEARER, "authProvider", "ciServer");
+    cloudProp.setCloudPropWithAccessToken("accessToken", OAUTH_TOKENTYPE_BEARER, "authProvider",
+                                          "ciServer");
     cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
     cloudProp.setCredID(1);
 
@@ -337,18 +340,17 @@ void provisionCloudProperty()
 }
 
 // Callback to found resources
-void foundResource(std::shared_ptr<OC::OCResource> resource)
+void foundUnsecuredResource(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) == OC_RSRVD_ES_RES_TYPE_EASYSETUP)
+        if (resource && !curResource  &&
+            resource->getResourceTypes().at(0) == OC_RSRVD_ES_RES_TYPE_EASYSETUP)
         {
-            std::cout<<"DISCOVERED Resource:"<<std::endl;
+            std::cout << "DISCOVERED Resource:" << std::endl;
             // Get the resource URI
             resourceURI = resource->uri();
             std::cout << "\tURI of the resource: " << resourceURI << std::endl;
@@ -359,22 +361,22 @@ void foundResource(std::shared_ptr<OC::OCResource> resource)
 
             // Get the resource types
             std::cout << "\tList of resource types: " << std::endl;
-            for(auto &resourceTypes : resource->getResourceTypes())
+            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())
+            for (auto &resourceInterfaces : resource->getResourceInterfaces())
             {
                 std::cout << "\t\t" << resourceInterfaces << std::endl;
             }
 
-            if(curResource == nullptr)
+            if (curResource == nullptr)
             {
                 remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
-                if(!remoteEnrollee)
+                if (!remoteEnrollee)
                 {
                     std::cout << "RemoteEnrollee object is failed for some reasons!" << std::endl;
                 }
@@ -387,43 +389,124 @@ void foundResource(std::shared_ptr<OC::OCResource> resource)
             }
         }
     }
-    catch(std::exception& e)
+    catch (std::exception &e)
     {
-        std::cerr << "Exception in foundResource: "<< e.what() << std::endl;
+        std::cerr << "Exception in foundResource: " << e.what() << std::endl;
     }
 }
 
-void discoveryEnrolleeResource()
+// Callback to found resources
+void foundSecuredResource(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) == OC_RSRVD_ES_RES_TYPE_EASYSETUP)
+        {
+            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 Resource Endpoint Infomation
+            std::cout << "\tList of resource endpoints: " << std::endl;
+            for (auto &resourceEndpoints : resource->getAllHosts())
+            {
+                std::cout << "\t\t" << resourceEndpoints << 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 = nullptr;
+
+                for (auto &resourceEndpoints : resource->getAllHosts())
+                {
+                    if (std::string::npos != resourceEndpoints.find("coaps"))
+                    {
+                        std::string newHost = resourceEndpoints;
+                        // Change Resource host if another host exists
+                        std::cout << "\tChange host of resource endpoints" << std::endl;
+                        std::cout << "\t\t" << "Current host is "
+                                  << resource->setHost(newHost) << std::endl;
+                        remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
+                        break;
+                    }
+                }
+
+                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;
+    }
+}
+
+void discoveryEnrolleeResource(void (*f)(std::shared_ptr<OC::OCResource> resource))
 {
     try
     {
         std::ostringstream requestURI;
         requestURI << OC_RSRVD_WELL_KNOWN_URI << "?rt=" << OC_RSRVD_ES_RES_TYPE_EASYSETUP;
-        OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, &foundResource);
-        std::cout<< "Finding Resource... " <<std::endl;
+        OCPlatform::findResource("", requestURI.str(), CT_DEFAULT, f);
+        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();
+        std::cout << "Exception in discoveryEnrolleeResource: " << e.what();
     }
 }
 
 void DisplayMenu()
 {
-    constexpr int DISCOVERY_ENROLLEE = 1;
+    constexpr int DISCOVER_UNSECURED_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 REQUEST_TO_CONNECT = 6;
-    constexpr int PROVISION_CLOUD_PROPERTY = 7;
+    constexpr int DISCOVER_SECURED_ENROLLEE = 3;
+    constexpr int GET_STATUS = 4;
+    constexpr int GET_CONFIGURATION = 5;
+    constexpr int PROVISION_DEVICE_PROPERTY = 6;
+    constexpr int REQUEST_TO_CONNECT = 7;
+    constexpr int PROVISION_CLOUD_PROPERTY = 8;
 
     std::cout << "========================================================\n";
-    std::cout << DISCOVERY_ENROLLEE << ". Discovery Enrollee Resource \n";
+    std::cout << DISCOVER_UNSECURED_ENROLLEE << ". Discover Enrollee Resource \n";
     std::cout << PROVISION_SECURITY << ". Provision Security to Enrollee  \n";
+    std::cout << DISCOVER_SECURED_ENROLLEE << ". Discover Secured Enrollee Resource \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";
@@ -431,16 +514,19 @@ void DisplayMenu()
     std::cout << PROVISION_CLOUD_PROPERTY << ". Provision Cloud Property  \n";
     std::cout << "========================================================\n";
 
-    int selection = processUserInput(DISCOVERY_ENROLLEE, PROVISION_CLOUD_PROPERTY);
+    int selection = processUserInput(DISCOVER_UNSECURED_ENROLLEE, PROVISION_CLOUD_PROPERTY);
 
     switch (selection)
     {
-        case DISCOVERY_ENROLLEE:
-            discoveryEnrolleeResource();
+        case DISCOVER_UNSECURED_ENROLLEE:
+            discoveryEnrolleeResource(&foundUnsecuredResource);
             break;
         case PROVISION_SECURITY:
             provisionSecurity();
             break;
+        case DISCOVER_SECURED_ENROLLEE:
+            discoveryEnrolleeResource(&foundSecuredResource);
+            break;
         case GET_STATUS:
             getStatus();
             break;
@@ -461,7 +547,7 @@ void DisplayMenu()
     };
 }
 
-static FILEclient_open(const char *path, const char *mode)
+static FILE *client_open(const char *path, const char *mode)
 {
     if (0 == strcmp(path, OC_SECURITY_DB_DAT_FILE_NAME))
     {
@@ -495,7 +581,8 @@ int main()
             return -1;
         }
 #endif
-    }catch (...)
+    }
+    catch (...)
     {
         std::cout << "Exception in main: " << std::endl;
     }