Implemented sign in
authorAndriy Gudz <a.gudz@samsung.com>
Thu, 27 Apr 2017 08:31:23 +0000 (11:31 +0300)
committerAndriy Gudz <a.gudz@samsung.com>
Thu, 27 Apr 2017 08:31:23 +0000 (11:31 +0300)
network-manager/nmlib/IoT/src/iotivity.cpp
network-manager/nmlib/include/iotivity.h
network-manager/nmlib/include/nmlib.h

index a12efc1..50ef9c9 100644 (file)
 #pragma GCC diagnostic pop
 #include <unistd.h>
 #include "nmexceptions.h"
+#include "nmlib.h"
 
 using namespace OC;
 
-namespace NetworkManager {
+namespace NetworkManager
+{
 
 const std::string IoTivity::DEFAULT_PROVIDER = "samsung";
+const int IoTivity::DEFAULT_TIMEOUT = 10;
 
-struct Params {
+struct Params
+{
     OC::PlatformConfig config;
     OCPersistentStorage ps;
+    OCAccountManager::Ptr accountMgr;
 };
 
 IoTivity* IoTivity::instance = nullptr;
@@ -57,28 +62,44 @@ IoTivity* IoTivity::getInstance()
 
 void IoTivity::cleanUp()
 {
-    if (instance != nullptr) {
+    if (instance != nullptr)
+    {
         if (instance->params != nullptr) delete instance->params;
         delete instance;
         instance = nullptr;
     }
 }
 
+void guardErrorCode(OCStackResult resultCode, std::string message)
+{
+    if (resultCode != OC_STACK_OK)
+        throw IoTInternalError(message + ":" + std::to_string(resultCode), EC_IOTIVITY_ERROR);
+}
+
 void IoTivity::signIn(const std::string& host, const std::string& login, const std::string& password)
 {
     if (host.empty() || login.empty() || password.empty())
         throw std::invalid_argument("Wrong login credentials");
 
     OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(host, CT_ADAPTER_TCP);
+    instance->params->accountMgr = accountMgr;
 
     std::mutex mtx;
     std::unique_lock<std::mutex> lock(mtx);
     std::condition_variable condVar;
 
-    auto signUpCb = [&](const OC::HeaderOptions& headerOptions,
-                                      const OC::OCRepresentation & rep,
-                                      const int resultCode)
+    // TODO add authcode provider
+    std::string authcode = "AuthCode_A";
+    signedIn = false;
+    int resultCodeCb;
+    bool signUpTimeout = true;
+    bool signInTimeout = true;
+
+    auto signUpCb = [&](const OC::HeaderOptions & headerOptions,
+                        const OC::OCRepresentation & rep,
+                        const int resultCode)
     {
+        resultCodeCb = resultCode;
         if(resultCode == 4)
         {
             oAuthCred.host = host;
@@ -90,36 +111,63 @@ void IoTivity::signIn(const std::string& host, const std::string& login, const s
             oAuthCred.accessToken = rep.getValueToString("accesstoken");
 
             oAuthCred.tokenProvider = DEFAULT_PROVIDER;
-
-            signedIn = true;
         }
+        signUpTimeout = false;
         condVar.notify_all();
     };
 
-    // TODO add authcode provider
-    std::string authcode = "AuthCode_A";
+    guardErrorCode(accountMgr->signUp(DEFAULT_PROVIDER, authcode, signUpCb), "Sign up error");
 
-    accountMgr->signUp(DEFAULT_PROVIDER, authcode, signUpCb);
+    condVar.wait_for(lock, std::chrono::seconds(10));
 
-    condVar.wait(lock);
+    if (signUpTimeout)
+        throw IoTInternalError("Timeout exceed", EC_TIMEOUT_ERROR);
+
+    if (resultCodeCb != 4)
+        throw IoTInternalError("Sign up cb error: " + std::to_string(resultCodeCb), EC_IOTIVITY_ERROR);
+
+
+
+
+
+    auto signInCb = [&](const OC::HeaderOptions & headerOptions,
+                        const OC::OCRepresentation & rep,
+                        const int resultCode)
+    {
+        resultCodeCb = resultCode;
+        signInTimeout = false;
+        condVar.notify_all();
+    };
+
+    guardErrorCode(accountMgr->signIn(oAuthCred.userId, oAuthCred.accessToken, signInCb), "Sign in error");
+
+    condVar.wait_for(lock, std::chrono::seconds(10));
+
+    if (signInTimeout)
+        throw IoTInternalError("Timeout exceed", EC_TIMEOUT_ERROR);
+
+    if (resultCodeCb != 4)
+        throw IoTInternalError("Sign in cb error: " + std::to_string(resultCodeCb), EC_IOTIVITY_ERROR);
+
+    signedIn = true;
 }
 
 void IoTivity::signOut()
 {
-    if (oAuthCred.host.empty() || oAuthCred.accessToken.empty()) {
+    if (oAuthCred.host.empty() || oAuthCred.accessToken.empty())
+    {
         // TODO add warn
         return;
     }
 
-    OCAccountManager::Ptr accountMgr = OCPlatform::constructAccountManagerObject(oAuthCred.host, CT_ADAPTER_TCP);
-
+    OCAccountManager::Ptr accountMgr = instance->params->accountMgr;
     std::mutex mtx;
     std::unique_lock<std::mutex> lock(mtx);
     std::condition_variable condVar;
 
-    auto signOutCb = [&](const OC::HeaderOptions& headerOptions,
-                                      const OC::OCRepresentation & rep,
-                                      const int resultCode)
+    auto signOutCb = [&](const OC::HeaderOptions & headerOptions,
+                         const OC::OCRepresentation & rep,
+                         const int resultCode)
     {
         if(resultCode == 4)
         {
@@ -128,7 +176,7 @@ void IoTivity::signOut()
         condVar.notify_all();
     };
 
-    accountMgr->signOut(oAuthCred.accessToken, signOutCb);
+    guardErrorCode(accountMgr->signOut(oAuthCred.accessToken, signOutCb), "Sign out error");
 
     condVar.wait(lock);
 }
@@ -159,7 +207,8 @@ IoTDevicesMap IoTivity::getUnOwnedDevices()
         {
             auto res = map.emplace(dev->getUUID(), dev);
 
-            if (!res.second) {
+            if (!res.second)
+            {
                 throw IoTInternalError("Out of memory");
             }
         }
index 4fd5914..829a860 100644 (file)
@@ -30,6 +30,7 @@ class IoTivity
 {
 private:
     static const std::string DEFAULT_PROVIDER;
+    static const int DEFAULT_TIMEOUT;
 
     IoTivity();
     static IoTivity* instance;
index 368a1cb..c657c84 100644 (file)
@@ -25,6 +25,7 @@ typedef enum  {
     EC_NOT_IMPLEMENTED_YET,
     EC_ITEM_NOT_FOUND,
     EC_TIMEOUT_ERROR,
+    EC_IOTIVITY_ERROR,
     // ...
 } NM_ErrorCode;