Update a mediator sample application for linux platform
authorJihun Ha <jihun.ha@samsung.com>
Thu, 21 Jul 2016 06:12:10 +0000 (15:12 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 22 Jul 2016 07:45:01 +0000 (07:45 +0000)
Sample application of mediator for linux is updated along with
Mediator SDK update.
Additionally, logics for handling error code are updated.

Change-Id: I9bae277def11b19644ade50b322e3ba5d7495a3e
Signed-off-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9517
Reviewed-by: Hemant Mahsky <h.mahsky@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/easy-setup/mediator/richsdk/SConscript
service/easy-setup/mediator/richsdk/inc/ESRichCommon.h
service/easy-setup/mediator/richsdk/src/CloudResource.cpp
service/easy-setup/mediator/richsdk/src/EasySetup.cpp [changed mode: 0644->0755]
service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp
service/easy-setup/sampleapp/enrollee/linux/SConscript [changed mode: 0644->0755]
service/easy-setup/sampleapp/enrollee/linux/oic_svr_db_server.dat [new file with mode: 0755]
service/easy-setup/sampleapp/mediator/linux/richsdk_sample/SConscript [changed mode: 0644->0755]
service/easy-setup/sampleapp/mediator/linux/richsdk_sample/mediator_cpp.cpp
service/easy-setup/sampleapp/mediator/linux/richsdk_sample/oic_svr_db_client.dat [new file with mode: 0755]

index 3986cef..f3600cf 100644 (file)
@@ -135,8 +135,8 @@ print "Files path is %s" % env.get('es_src')
 ######################################################################
 # Build RichSDK Mediator Sample App
 ################################################ ######################
-#if target_os in ['linux']:
-#      SConscript('../../sampleapp/mediator/linux/richsdk_sample/SConscript')
+if target_os in ['linux']:
+       SConscript('../../sampleapp/mediator/linux/richsdk_sample/SConscript')
 
 ######################################################################
 #Build UnitTestcases for Mediator[RichSDK]
index 45c03f2..97f07a2 100755 (executable)
@@ -161,7 +161,7 @@ namespace OIC
 
         typedef struct
         {
-            vector<WIFI_MODE> types;
+            vector<WIFI_MODE> modes;
             WIFI_FREQ freq;
         } WiFiConfig;
 
index e95478c..24ebd63 100755 (executable)
@@ -73,7 +73,7 @@ namespace OIC
             OIC_LOG_V (DEBUG, ES_CLOUD_RES_TAG, "onCloudProvResponse : %s, eCode = %d",
                     rep.getUri().c_str(), eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
                 ESResult result  = ESResult::ES_ERROR;
 
old mode 100644 (file)
new mode 100755 (executable)
index 96808e6..489b0e2
 
 #include "EasySetup.h"
 
+#include "OCPlatform.h"
 #include "logger.h"
 #include "ESException.h"
 #include "RemoteEnrollee.h"
 
+using namespace OC;
+
 namespace OIC
 {
     namespace Service
@@ -48,6 +51,11 @@ namespace OIC
 
         std::shared_ptr<RemoteEnrollee> EasySetup::createRemoteEnrollee (std::shared_ptr< OC::OCResource > resource)
         {
+            if(resource->getResourceTypes().at(0) != OC_RSRVD_ES_RES_TYPE_PROV)
+            {
+                OIC_LOG_V (DEBUG, EASYSETUP_TAG, "createRemoteEnrollee : invalid reousrce");
+                return nullptr;
+            }
             return std::shared_ptr< RemoteEnrollee > (new RemoteEnrollee(resource));
         }
     }
index 21c5471..beae978 100755 (executable)
@@ -45,7 +45,7 @@ namespace OIC
                     rep.getUri().c_str(),
                     eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
                 OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
                         "checkProvInformationCb : Provisioning is failed ");
@@ -69,7 +69,7 @@ namespace OIC
             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %s, eCode = %d",
                     rep.getUri().c_str(), eCode);
 
-            if (eCode != 0)
+            if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED)
             {
                 ESResult result  = ESResult::ES_ERROR;
 
@@ -197,13 +197,13 @@ namespace OIC
                     if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE)
                                 && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ))
                     {
-                        std::vector<int> types = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
+                        std::vector<int> modes = prop->getValue<std::vector<int>>(OC_RSRVD_ES_SUPPORTEDWIFIMODE);
 
-                        for(auto type = types.begin(); type != types.end(); ++type)
+                        for(auto mode = modes.begin(); mode != modes.end(); ++mode)
                         {
                             OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d",
-                                *type);
-                            wifiConf.types.push_back(static_cast<WIFI_MODE>(*type));
+                                *mode);
+                            wifiConf.modes.push_back(static_cast<WIFI_MODE>(*mode));
                         }
 
                         wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
old mode 100644 (file)
new mode 100755 (executable)
index 6fb0f68..53d99c3
@@ -63,5 +63,8 @@ enrollee = enrollee_env.Program('enrollee', 'enrolleewifi.c')
 
 i_enrollee = enrollee_env.Install(env.get('BUILD_DIR'), enrollee)
 
-Alias('enrollee', i_enrollee)
+justworksdat = enrollee_env.Install(env.get('BUILD_DIR') + '/service/easy-setup/sampleapp/enrollee/linux/',
+                                    env.get('SRC_DIR') + '/service/easy-setup/sampleapp/enrollee/linux/oic_svr_db_server.dat')
+
+Alias('enrollee', [i_enrollee, justworksdat])
 env.AppendTarget('enrollee')
diff --git a/service/easy-setup/sampleapp/enrollee/linux/oic_svr_db_server.dat b/service/easy-setup/sampleapp/enrollee/linux/oic_svr_db_server.dat
new file mode 100755 (executable)
index 0000000..08c7368
Binary files /dev/null and b/service/easy-setup/sampleapp/enrollee/linux/oic_svr_db_server.dat differ
old mode 100644 (file)
new mode 100755 (executable)
index eb2ab43..0132a63
@@ -70,5 +70,9 @@ mediator = mediator_env.Program('mediator_rich', 'mediator_cpp.cpp')
 
 i_mediator = mediator_env.Install(env.get('BUILD_DIR'), mediator)
 
-Alias('mediator_rich', i_mediator)
+clientdat = mediator_env.Install(env.get('BUILD_DIR') + '/service/easy-setup/sampleapp/mediator/linux/richsdk_sample',
+                                    env.get('SRC_DIR') + '/service/easy-setup/sampleapp/mediator/linux/richsdk_sample/oic_svr_db_client.dat')
+
+
+Alias('mediator_rich', [i_mediator, clientdat])
 env.AppendTarget('mediator_rich')
index 810a8ab..135715e 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,203 +66,228 @@ 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();
+    DeviceConfig devConfig = conf.getDevConf();
+    WiFiConfig wifiConfig = conf.getWiFiConf();
 
-    cout << "\tDeviceConfig.id : " << devConfig.id << endl;
     cout << "\tDeviceConfig.name : " << devConfig.name << endl;
     cout << "\tDeviceConfig.language : " << devConfig.language << endl;
     cout << "\tDeviceConfig.country : " << devConfig.country << endl;
 
-    for(auto type = netInfo.types.begin(); type != netInfo.types.end(); ++type)
+    for(auto mode = wifiConfig.modes.begin(); mode != wifiConfig.modes.end(); ++mode)
     {
-        cout << "\tnetInfo.types : " << static_cast<int>(*type) << endl;
+        cout << "\tnetInfo.modes : " << static_cast<int>(*mode) << endl;
     }
-    cout << "\tnetInfo.freq : " << static_cast<int>(netInfo.freq) << endl;
+    cout << "\tnetInfo.freq : " << static_cast<int>(wifiConfig.freq) << endl;
     cout << "===========================================" << endl;
 }
 
-void RequestPropertyDataStatusCallback(std::shared_ptr< RequestPropertyDataStatus > requestPropertyDataStatus)
+void provisionSecurity()
 {
-    cout << "Enter RequestPropertyDataStatusCb." << endl;
-
-    if(requestPropertyDataStatus->getESResult() != ES_OK)
-    {
-      cout << "requestPropertyDataStatus is failed." << endl;
-      return;
-    }
-    else
-    {
-      cout << "requestPropertyDataStatus is success." << endl;
-      printPropertyData(requestPropertyDataStatus->getPropertyData());
-    }
+    // TODO
 }
 
-void dataProvisioningStatusCallback(std::shared_ptr< DataProvisioningStatus > provStatus)
+void GetConfigurationCallback(std::shared_ptr< GetConfigurationStatus > getConfigurationStatus)
 {
-    cout << "Enter dataProvisioningStatusCallback." << endl;
-
-    if(provStatus->getESResult() != ES_OK)
+    if(getConfigurationStatus->getESResult() != ES_OK)
     {
-      cout << "dataProvisioningStatusCallback is failed." << endl;
+      cout << "GetConfigurationStatus is failed." << endl;
       return;
     }
     else
     {
-      cout << "dataProvisioningStatusCallback is success." << endl;
-      cout << "ESDataProvState : " << provStatus->getESDataProvState() << endl;
+      cout << "GetConfigurationStatus is success." << endl;
+      printConfiguration(getConfigurationStatus->getEnrolleeConf());
     }
 }
 
-void cloudProvisioningStatusCallback(std::shared_ptr< CloudProvisioningStatus > status)
+void getConfiguration()
 {
-    cout << "Enter cloudProvisioningStatusCallback." << endl;
-    cout << "CloudProvStatus : " <<  status->getESCloudState() << endl;
-}
+    if(!remoteEnrollee)
+        return;
 
-void createRemoteEnrollee()
-{
-    easySetupIntance = EasySetup::getInstance();
     try
     {
-        remoteEnrollee = easySetupIntance->createRemoteEnrollee();
+        remoteEnrollee->getConfiguration(GetConfigurationCallback);
     }
     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 deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus > provStatus)
 {
-    try
+    if(provStatus->getESResult() != ES_OK)
     {
-        remoteEnrollee->initRemoteEnrollee();
+      cout << "Device Provisioning is failed." << endl;
+      return;
     }
-    catch (OCException &e)
+    else
     {
-        std::cout << "Exception during initRemoteEnrollee call" << e.reason();
-        return;
+      cout << "Device Provisioning is success." << endl;
     }
 }
 
-void requestPropertyData()
+void provisionDeviceProperty()
 {
+    if(!remoteEnrollee)
+        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";
+
     try
     {
-        remoteEnrollee->requestPropertyData(RequestPropertyDataStatusCallback);
+        remoteEnrollee->provisionDeviceProperties(deviceProp, deviceProvisioningStatusCallback);
     }
     catch (OCException &e)
     {
-        std::cout << "Exception during requestPropertyData call" << e.reason();
+        std::cout << "Exception during provisionDeviceProperties call" << e.reason();
         return;
     }
 }
 
-void setDataProvInfo()
-{
-    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);
-}
-
-void setCloudProvInfo()
-{
-    CloudProvInfo cloudProvInfo;
-    cloudProvInfo.authCode = "authCode";
-    cloudProvInfo.authProvider = "authProvider";
-    cloudProvInfo.ciServer = "ciServer";
-
-    remoteEnrollee->setCloudProvInfo(cloudProvInfo);
-}
-
-void startDataProvisioning()
+void cloudProvisioningStatusCallback(std::shared_ptr< CloudPropProvisioningStatus > provStatus)
 {
-    try
+    switch (provStatus->getESCloudState())
     {
-        remoteEnrollee->startDataProvisioning(dataProvisioningStatusCallback);
-    }
-    catch (OCException &e)
-    {
-        std::cout << "Exception during startDataProvisioning call" << e.reason();
-        return;
+        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 startCloudProvisioning()
+void provisionCloudProperty()
 {
+    if(!remoteEnrollee)
+        return;
+
+    CloudProp cloudProp;
+    cloudProp.authCode = "authCode";
+    cloudProp.authProvider = "authProvider";
+    cloudProp.ciServer = "ciServer";
+
     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_CONFIGURATION = 2;
+    constexpr int PROVISION_DEVICE_PROPERTY = 3;
+    constexpr int PROVISION_CLOUD_PROPERTY = 4;
 
     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_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_CONFIGURATION:
+            getConfiguration();
             break;
-        case SET_DATA_PROVISIONING_INFO:
-            setDataProvInfo();
+        case PROVISION_DEVICE_PROPERTY:
+            provisionDeviceProperty();
             break;
-        case START_DATA_PROVISIONING:
-            startDataProvisioning();
-            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;
@@ -269,6 +296,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
@@ -288,6 +316,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
diff --git a/service/easy-setup/sampleapp/mediator/linux/richsdk_sample/oic_svr_db_client.dat b/service/easy-setup/sampleapp/mediator/linux/richsdk_sample/oic_svr_db_client.dat
new file mode 100755 (executable)
index 0000000..62fd559
Binary files /dev/null and b/service/easy-setup/sampleapp/mediator/linux/richsdk_sample/oic_svr_db_client.dat differ