For IoT to Control App lib: enumeration implemented, device owning implemented.
authorLomtev Dmytro <d.lomtev@samsung.com>
Thu, 4 May 2017 09:36:56 +0000 (12:36 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Thu, 4 May 2017 09:36:56 +0000 (12:36 +0300)
network-manager/nmlib/IoT/inc/iotdevice_impl.h
network-manager/nmlib/IoT/src/iotdevice_impl.cpp
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/nmlib/ctrl_app/src/iot_dev_manager.cpp
network-manager/nmlib/include/iotdevice.h
network-manager/nmlib/include/iotivity.h
network-manager/test/test_iot_dev_manager.cpp

index 0edf4a6..493de7b 100644 (file)
@@ -34,7 +34,7 @@ public:
 
     bool isOnline() override;
     
-    void ownDevice(const std::string& provider, const std::string& auth_code) override;
+    void ownDevice() override;
 
     void unOwnDevice() override;
 private:
index 7722527..fdf6f63 100644 (file)
@@ -1,6 +1,7 @@
 #include "iotdevice_impl.h"
 #include "iotivity.h"
 #include <chrono>
+#include <stdexcept>
 //#include <OCProvisioningManager.hpp>
 
 #include "EasySetup.h"
@@ -218,40 +219,55 @@ bool IoTDevice_impl::isOnline()
     return online;
 }
 
-void IoTDevice_impl::ownDevice(const std::string& provider, const std::string& auth_code)
+void IoTDevice_impl::ownDevice()
 {
-    const auto& cred = IoTivity::getInstance()->getOAuthCredentials();
+    auto iotinst = IoTivity::getInstance();
+    const auto& cred = iotinst->getOAuthCredentials();
 
     OIC::Service::CloudProp cp;
-    cp.setCloudProp(auth_code, provider, cred.host);
+    cp.setCloudProp(iotinst->getAuthCode(), cred.tokenProvider, cred.host);
     cp.setCloudID(cred.cloudId);
     cp.setCredID(1);
 
     auto enrolee = OIC::Service::EasySetup::getInstance()->createRemoteEnrollee(dev);
 
+    std::unique_lock<std::mutex> lock(mtx);
+    std::string error{"Unknown error"};
+
     enrolee->provisionCloudProperties(cp,
-        [](std::shared_ptr<OIC::Service::CloudPropProvisioningStatus> status)
+        [this, &error](std::shared_ptr<OIC::Service::CloudPropProvisioningStatus> status)
         {
             switch(status->getESResult())
             {
             case ES_OK:
+                error = "";
                 std::cout << "!!! Cloud Provisioning is success. !!!" << std::endl;
                 break;
             case ES_SECURE_RESOURCE_DISCOVERY_FAILURE:
-                std::cout << "!!! Enrollee is not found in a given network. !!!" << std::endl;
+                error = "!!! Enrollee is not found in a given network. !!!";
+                std::cout << error << std::endl;
                 break;
             case ES_ACL_PROVISIONING_FAILURE:
-                std::cout << "!!! ACL provisioning is failed. !!!" << std::endl;
+                error = "!!! ACL provisioning is failed. !!!";
+                std::cout << error << std::endl;
                 break;
             case ES_CERT_PROVISIONING_FAILURE:
-                std::cout << "!!! CERT provisioning is failed. !!!" << std::endl;
+                error = "!!! CERT provisioning is failed. !!!";
+                std::cout << error << std::endl;
                 break;
             default:
-                std::cout << "!!! Cloud Provisioning is failed. !!!" << std::endl;
+                error = "!!! Cloud Provisioning is failed. !!!";
+                std::cout << error << std::endl;
                 break;
             }
+
+            cond_var.notify_one();
         }
     );
+
+    cond_var.wait_for(lock, std::chrono::seconds(CALLBACK_WAIT_TIMEOUT_S));
+
+    if (error != "") throw std::runtime_error(error);
 }
 
 void IoTDevice_impl::unOwnDevice()
index a1ee643..38902b7 100644 (file)
@@ -86,6 +86,13 @@ struct Params
 
 IoTivity* IoTivity::instance = nullptr;
 
+std::string IoTivity::getAuthCode()
+{
+    // TODO add normal implementation
+    std::string authcode = "AuthCode_A";
+    return authcode;
+}
+
 IoTivity::IoTivity(): signedIn(false)
 {
     params = new Params{OC::PlatformConfig{
@@ -172,8 +179,7 @@ void IoTivity::signIn(const std::string& host, const std::string& login, const s
     std::unique_lock<std::mutex> lock(mtx);
     std::condition_variable condVar;
 
-    // TODO add authcode provider
-    std::string authcode = "AuthCode_A";
+    std::string authcode = getAuthCode();
     signedIn = false;
     int resultCodeCb;
     bool signUpTimeout = true;
index a8ad9d0..f142cf3 100644 (file)
@@ -21,6 +21,7 @@ struct NM_Context {
 
 struct NM_DeviceList {
     IoTDevicesMap map;
+    IoTDevicesMap::iterator it;
 };
 
 NM_ErrorCode NM_init(NM_hContext* ctx)
@@ -89,6 +90,7 @@ NM_ErrorCode NM_getOwnedDevices(NM_hContext ctx, NM_hDeviceList* dev_list)
 {
     if (dev_list == nullptr) return EC_NULL_POINTER;
     *dev_list = new NM_DeviceList{ctx->instance->getOwnedDevices()};
+    (*dev_list)->it = (*dev_list)->map.begin();
     return EC_OK;
 }
 
@@ -110,18 +112,28 @@ size_t NM_getListSize(NM_hDeviceList dev_list)
 NM_ErrorCode NM_getUnownedDevices(NM_hContext ctx, NM_hDeviceList* dev_list)
 {
     if (dev_list == nullptr) return EC_NULL_POINTER;
-    *dev_list = new NM_DeviceList{ctx->instance->getUnOwnedDevices()};
+    *dev_list = new NM_DeviceList{ctx->instance->getUnOwnedDevices()};\
+    (*dev_list)->it = (*dev_list)->map.begin();
     return EC_OK;
 }
 
 void NM_resetDeviceList(NM_hDeviceList dev_list)
 {
-    if (dev_list != nullptr) delete dev_list;
+    if (dev_list != nullptr)
+    {
+        dev_list->it = dev_list->map.begin();
+    }
 }
 
 const char* NM_deviceListEnum(NM_hDeviceList dev_list)
 {
-    return nullptr;
+    const char* uid = nullptr;
+    if (dev_list->it != dev_list->map.end())
+    {
+        uid = dev_list->it->first.c_str();
+        ++(dev_list->it);
+    }
+    return uid;
 }
 
 NM_ErrorCode NM_deviceListForEach(NM_hDeviceList dev_list, NM_deviceEnumerationCb callback, void* user_defined)
@@ -198,7 +210,22 @@ void NM_freeDeviceInfo(NM_DeviceInfo* info)
 
 NM_ErrorCode NM_ownDevice(NM_hDeviceList dev_list, const char* dev_id)
 {
-    return EC_NOT_IMPLEMENTED_YET;
+    if (dev_list == nullptr || dev_id == nullptr) return EC_NULL_POINTER;
+
+    auto it = dev_list->map.find(dev_id);
+    if (it == dev_list->map.end()) return EC_ITEM_NOT_FOUND;
+
+    try
+    {
+        it->second->ownDevice();
+    }
+    catch (std::exception& e)
+    {
+        // TODO: add error logging
+        return EC_IOTIVITY_ERROR;
+    }
+
+    return EC_OK;
 }
 
 NM_ErrorCode NM_unOwnDevice(NM_hDeviceList dev_list, const char* dev_id)
index 6b9eef1..8ef03c2 100644 (file)
@@ -23,7 +23,7 @@ public:
 
     virtual bool isOnline() = 0;
     
-    virtual void ownDevice(const std::string& provider, const std::string& auth_code) = 0;
+    virtual void ownDevice() = 0;
 
     virtual void unOwnDevice() = 0;
 };
index 1e3e3de..2efaa3e 100644 (file)
@@ -54,6 +54,8 @@ public:
         return oAuthCred;
     }
 
+    std::string getAuthCode();
+
     static IoTivity* getInstance();
 
     /**
index b84f39e..42b57c0 100644 (file)
@@ -56,14 +56,27 @@ TEST_F(IoTDevManagerTest, unowned_dev_discovery)
 
     ASSERT_EQ(EC_NULL_POINTER, NM_getUnownedDevices(ctx, nullptr));
 
+    ASSERT_EQ(EC_OK, NM_freeDeviceList(nullptr));
+
+    ASSERT_EQ(EC_OK, NM_getOwnedDevices(ctx, &dev_list));
+    std::cout << "Owned devices found: " << NM_getListSize(dev_list) << std::endl;
+    ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
+
+    ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
+
     ASSERT_EQ(EC_OK, NM_getUnownedDevices(ctx, &dev_list));
     std::cout << "Unowned devices found: " << NM_getListSize(dev_list) << std::endl;
     ASSERT_EQ(EC_NULL_POINTER, NM_deviceListForEach(nullptr, each_callback, nullptr));
     ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
-    NM_freeDeviceList(nullptr);
-    NM_freeDeviceList(&dev_list);
 
-    ASSERT_EQ(EC_OK, NM_getOwnedDevices(ctx, &dev_list));
-    std::cout << "Owned devices found: " << NM_getListSize(dev_list) << std::endl;
-    ASSERT_EQ(EC_OK, NM_deviceListForEach(dev_list, each_callback, nullptr));
+    auto list_size = NM_getListSize(dev_list);
+    if (list_size > 0)
+    {
+        ASSERT_NO_THROW(NM_resetDeviceList(dev_list));
+        const char* uid = nullptr;
+        ASSERT_NE(nullptr, uid = NM_deviceListEnum(dev_list));
+        ASSERT_EQ(EC_OK, NM_ownDevice(dev_list, uid));
+    }
+
+    ASSERT_EQ(EC_OK, NM_freeDeviceList(&dev_list));
 }