#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;
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;
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)
{
condVar.notify_all();
};
- accountMgr->signOut(oAuthCred.accessToken, signOutCb);
+ guardErrorCode(accountMgr->signOut(oAuthCred.accessToken, signOutCb), "Sign out error");
condVar.wait(lock);
}
{
auto res = map.emplace(dev->getUUID(), dev);
- if (!res.second) {
+ if (!res.second)
+ {
throw IoTInternalError("Out of memory");
}
}