{
Success = 0, ///< Success.
Error, ///< Some error occurred.
- SessionExpired, ///< Session is expired.
- NotLoggedIn, ///< Not logged in.
- IncorrectIdOrPsswd, ///< Incorrect id or password.
LibNotFound, ///< Library isn't found.
- FunctionNotFound ///< Function isn't found.
+ FunctionNotFound, ///< Function isn't found.
+ UnregisteredCommand, ///< Can`t process message, unregistered command.
+ InvalidArgument, ///< Invalid argument value.
+ BadMessageFormat, ///< Can`t process message, wrong message format.
+ ProcessingFailed, ///< Can`t process message, internal message handler error.
+ InternalConnectionFailed, ///< Can`t process message, internal service configuration error.
+ NoConnection, ///< There is no internet connection.
+ ConnectionTimedOut, ///< Connection is timed out.
+ SessionExpired, ///< Session is expired.
+ NotAuthorized, ///< Not logged in.
+ AuthError ///< Incorrect id or password.
};
+inline std::string GetErrorDescription(ErrorCode code)
+{
+ switch (code)
+ {
+ case ErrorCode::NoConnection: case ErrorCode::ConnectionTimedOut:
+ {
+ return std::string("An error occurred while loading the data. Please, check the connection.");
+ }
+ case ErrorCode::AuthError:
+ {
+ return std::string("Wrong credentials");
+ }
+ case ErrorCode::SessionExpired:
+ {
+ return std::string("Session expired. Please, sign in again");
+ }
+ default:
+ {
+ return std::string();
+ }
+ }
+}
+
enum class SessionState
{
Open = 0,
Expired
};
-/**
- @brief Message processing results for UI communication.
- */
-enum class MessageProcessResult: int
-{
- Success = 0, ///< Message processing succeed.
- Error, ///< In case of any other error.
- ErrorUnregisteredCommand, ///< Can`t process message, unregistered command.
- ErrorBadMessageFormat, ///< Can`t process message, wrong message format.
- ErrorProcessingFailed, ///< Can`t process message, internal message handler error.
- ErrorInternalConnectionFailed, ///< Can`t process message, internal service configuration error.
- IncorrectIdOrPsswd, ///< Invalid used id or password.
- NotLoggedIn
-};
-
}
#endif /* IOTSWC_COMMON_TYPES_H_ */
virtual ErrorCode LogOut() = 0;
- virtual SessionState GetSessionState() = 0;
+ virtual ErrorCode GetSessionState(SessionState& state) = 0;
- virtual DeviceList GetOwnedDeviceList() = 0;
+ virtual ErrorCode GetOwnedDeviceList(DeviceList& deviceList) = 0;
- virtual DeviceList GetUnownedDeviceList() = 0;
+ virtual ErrorCode GetUnownedDeviceList(DeviceList& deviceList) = 0;
virtual ErrorCode UnownDevice(const std::string& deviceId) = 0;
virtual ErrorCode LogOut();
- virtual SessionState GetSessionState();
+ virtual ErrorCode GetSessionState(SessionState&);
- virtual DeviceList GetOwnedDeviceList();
+ virtual ErrorCode GetOwnedDeviceList(DeviceList&);
- virtual DeviceList GetUnownedDeviceList();
+ virtual ErrorCode GetUnownedDeviceList(DeviceList&);
virtual ErrorCode UnownDevice(const std::string& deviceId);
virtual void AddObserver(IModelObserverWPtr observer);
+ virtual void NotificationCallback(const std::string& title, const std::string& message);
+
private:
ModelObserversList m_observers;
INetworkManagerUPtr m_networkManager;
namespace iotswsec
{
+using NotificationCallback = std::function<void(const std::string& title, const std::string& body)>;
+
+
class INetworkManager
{
public:
- using NotificationCallback = std::function<void(const std::string& title, const std::string& body)>;
-
virtual ~INetworkManager() = default;
- //TODO: define interface.
-
virtual ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) = 0;
virtual ErrorCode LogOut() = 0;
- virtual SessionState GetSessionState() = 0;
+ virtual ErrorCode GetSessionState(SessionState&) = 0;
- virtual DeviceList GetOwnedDeviceList() = 0;
+ virtual ErrorCode GetOwnedDeviceList(DeviceList&) = 0;
- virtual DeviceList GetUnownedDeviceList() = 0;
+ virtual ErrorCode GetUnownedDeviceList(DeviceList&) = 0;
virtual ErrorCode UnownDevice(const std::string& deviceId) = 0;
virtual ErrorCode OwnDevice(const std::string& deviceId) = 0;
-// virtual ErrorCode AddNotificationCallback(const NotificationCallback&) = 0;
+ virtual ErrorCode RegisterNotificationCallback(const NotificationCallback&) = 0;
};
using INetworkManagerPtr = std::shared_ptr<INetworkManager>;
typedef void (*DeviceStateChangedCallback)(const char* devId, DeviceState state, void* userDefined);
typedef void (*DeviceEnumCallback)(NM_hDeviceList, const char* devId, void* userDefined);
- typedef void (*NotificationCallback)(NM_NotificationData,void*);
+ typedef void (*NmNotificationCallback)(NM_NotificationData,void*);
typedef NM_ErrorCode (*InitFunc)(NM_hContext*);
typedef void (*CleanUpFunc)(NM_hContext*);
typedef void (*FreeDeviceInfoFunc)(NM_DeviceInfo*);
typedef NM_ErrorCode (*OwnDeviceFunc)(NM_hDeviceList, const char* devId);
typedef NM_ErrorCode (*UnownDeviceFunc)(NM_hDeviceList dev_list, const char* devId);
- typedef NM_ErrorCode (*SubsOnNotificationsFunc)(NM_hContext, NotificationCallback, void*);
+ typedef NM_ErrorCode (*SubsOnNotificationsFunc)(NM_hContext, NmNotificationCallback, void*);
+ typedef void (*UnsubsOnNotificationsFunc)(NM_hContext);
public:
ErrorCode LogOut() override;
- SessionState GetSessionState() override;
+ ErrorCode GetSessionState(SessionState&) override;
- DeviceList GetOwnedDeviceList() override;
+ ErrorCode GetOwnedDeviceList(DeviceList&) override;
- DeviceList GetUnownedDeviceList() override;
+ ErrorCode GetUnownedDeviceList(DeviceList&) override;
ErrorCode UnownDevice(const std::string& deviceId) override;
ErrorCode OwnDevice(const std::string& deviceId) override;
-// ErrorCode AddNotificationCallback(const NotificationCallback&) override;
+ ErrorCode RegisterNotificationCallback(const NotificationCallback&) override;
+
+ ErrorCode NotifySubscribers(const std::string& title, const std::string& message);
struct ModuleSymbols
{
OwnDeviceFunc m_nmOwnDeviceFunc;
UnownDeviceFunc m_nmUnownDeviceFunc;
SubsOnNotificationsFunc m_nmSubsOnNotifFunc;
+ UnsubsOnNotificationsFunc m_nmUnsubsOnNotifFunc;
};
void FreeCache();
void FreeDeviceList(NM_hDeviceList* nmList);
ErrorCode SubscribeOnNotifications();
+ ErrorCode UnsubscribeOnNotifications();
private:
- ModuleSymbols m_moduleSymbols;
- NM_hContext m_context;
- NM_hDeviceList m_ownedDevices;
- NM_hDeviceList m_unownedDevices;
+ ModuleSymbols m_moduleSymbols;
+ NM_hContext m_context;
+ NM_hDeviceList m_ownedDevices;
+ NM_hDeviceList m_unownedDevices;
+ std::vector<NotificationCallback> m_notificationCallbackList;
};
} //namespace iotswsec
#ifndef NETWORK_NM_TYPES_H_
#define NETWORK_NM_TYPES_H_
+#include "common_types.h"
+
namespace iotswsec
{
// ...
} NM_ErrorCode;
+inline ErrorCode GetCorrespondingErrorCode(NM_ErrorCode code)
+{
+ switch (code)
+ {
+ case NM_ErrorCode::EC_AUTH_ERROR:
+ {
+ return ErrorCode::AuthError;
+ }
+ case NM_ErrorCode::EC_TIMEOUT_ERROR:
+ {
+ return ErrorCode::ConnectionTimedOut;
+ }
+ case NM_ErrorCode::EC_IOTIVITY_ERROR:
+ {
+ return ErrorCode::NoConnection; //TODO: no eto ne tochno
+ }
+ default:
+ {
+ return ErrorCode::Error;
+ }
+ }
+}
+
typedef enum
{
DS_Offline = 0,
ErrorCode LogOut() override;
- SessionState GetSessionState() override;
+ ErrorCode GetSessionState(SessionState& state) override;
void UpdateSessionState(SessionState) override;
- DeviceList GetOwnedDeviceList() override;
+ ErrorCode GetOwnedDeviceList(DeviceList& deviceList) override;
- DeviceList GetUnownedDeviceList() override;
+ ErrorCode GetUnownedDeviceList(DeviceList& deviceList) override;
ErrorCode UnownDevice(const std::string& deviceId) override;
*/
std::string ProcessRequest(const std::string& command, const JSONNode& attachedObject);
- typedef std::function<MessageProcessResult(const JSONNode&, JSONNode&, std::string&)> Callback;
+ typedef std::function<ErrorCode(const JSONNode&, JSONNode&, std::string&)> Callback;
/**
@brief Registers a callback for requests.
*/
void RegisterCallback(const std::string& command, const Callback& callback);
- MessageProcessResult LogIn(const JSONNode& request, JSONNode& response,
+ ErrorCode LogIn(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult LogOut(const JSONNode& request, JSONNode& response,
+ ErrorCode LogOut(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult GetSessionState(const JSONNode& request, JSONNode& response,
+ ErrorCode GetSessionState(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult GetOwnedDevices(const JSONNode& request, JSONNode& response,
+ ErrorCode GetOwnedDevices(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult GetUnownedDevices(const JSONNode& request, JSONNode& response,
+ ErrorCode GetUnownedDevices(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult OwnDevice(const JSONNode& request, JSONNode& response,
+ ErrorCode OwnDevice(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult UnownDevice(const JSONNode& request, JSONNode& response,
+ ErrorCode UnownDevice(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
- MessageProcessResult GetDashboard(const JSONNode& request, JSONNode& response,
+ ErrorCode GetDashboard(const JSONNode& request, JSONNode& response,
std::string& errDescription) const;
private:
**/
#include "model.h"
+#include "push_notification.h"
namespace iotswsec
{
m_observers(),
m_networkManager(new NetworkManager())
{
+ m_networkManager->RegisterNotificationCallback(std::bind(&Model::NotificationCallback, this,
+ std::placeholders::_1, std::placeholders::_2));
}
ErrorCode Model::LogIn(const UserInfo& userInfo, const std::string& psswd)
return m_networkManager->LogOut();
}
-SessionState Model::GetSessionState()
+ErrorCode Model::GetSessionState(SessionState& state)
{
- return m_networkManager->GetSessionState();
+ return m_networkManager->GetSessionState(state);
}
void Model::AddObserver(IModelObserverWPtr observer)
m_observers.push_back(observer);
}
-DeviceList Model::GetOwnedDeviceList()
+ErrorCode Model::GetOwnedDeviceList(DeviceList& deviceList)
{
- return m_networkManager->GetOwnedDeviceList();
+ return m_networkManager->GetOwnedDeviceList(deviceList);
}
-DeviceList Model::GetUnownedDeviceList()
+ErrorCode Model::GetUnownedDeviceList(DeviceList& deviceList)
{
- return m_networkManager->GetUnownedDeviceList();
+ return m_networkManager->GetUnownedDeviceList(deviceList);
}
ErrorCode Model::UnownDevice(const std::string& deviceId)
return m_networkManager->OwnDevice(deviceId);
}
+void Model::NotificationCallback(const std::string& title,
+ const std::string& message)
+{
+ PushNotification::GetInstance().Push(title, message);
+}
+
}
#include "utils/log.h"
#include "utils/to_string.h"
#include "utils/assert_return.h"
-#include "push_notification.h"
+#include "push_notification.h" //TODO: remove
#include <dlfcn.h>
#define NM_OWN_DEV_FUNC_NAME "NM_ownDevice"
#define NM_UNOWN_DEV_FUNC_NAME "NM_unOwnDevice"
#define NM_SUBS_NOTIFICATIONS_FUNC_NAME "NM_subscribeNotifications"
+#define NM_UNSUBS_NOTIF_FUNC_NAME "NM_unsubscribeNotifications"
namespace iotswsec
{
m_nmFreeDeviceInfoFunc(nullptr),
m_nmOwnDeviceFunc(nullptr),
m_nmUnownDeviceFunc(nullptr),
- m_nmSubsOnNotifFunc(nullptr)
+ m_nmSubsOnNotifFunc(nullptr),
+ m_nmUnsubsOnNotifFunc(nullptr)
{
if (LoadModule() != ErrorCode::Success)
{
dlsym(this->m_module, NM_UNOWN_DEV_FUNC_NAME));
m_nmSubsOnNotifFunc = reinterpret_cast<SubsOnNotificationsFunc>(
dlsym(this->m_module, NM_SUBS_NOTIFICATIONS_FUNC_NAME));
+ m_nmUnsubsOnNotifFunc = reinterpret_cast<UnsubsOnNotificationsFunc>(
+ dlsym(this->m_module, NM_UNSUBS_NOTIF_FUNC_NAME));
if (!m_nmInitFunc ||
!m_nmCleanUpFunc ||
!m_nmFreeDeviceInfoFunc ||
!m_nmOwnDeviceFunc ||
!m_nmUnownDeviceFunc ||
- !m_nmSubsOnNotifFunc)
+ !m_nmSubsOnNotifFunc ||
+ !m_nmUnsubsOnNotifFunc)
{
LOG_ERR("Couldn't load one of the functions due to error: %s", dlerror());
m_nmOwnDeviceFunc = nullptr;
m_nmUnownDeviceFunc = nullptr;
m_nmSubsOnNotifFunc = nullptr;
+ m_nmUnsubsOnNotifFunc = nullptr;
if (dlclose(m_module) == 0)
{
m_moduleSymbols(),
m_context(nullptr),
m_ownedDevices(nullptr),
- m_unownedDevices(nullptr)
+ m_unownedDevices(nullptr),
+ m_notificationCallbackList()
{
NM_ErrorCode res;
};
}
-static void PushNotificationCallback(NM_NotificationData data, void*)
+static void PushNotificationCallback(NM_NotificationData data, void* userData)
{
LOG_DBG("BEGIN");
if (data.message && data.title)
{
+ //TODO: remove this line and uncomment the next two when "subscribe on notification" function is fixed.
PushNotification::GetInstance().Push(data.title, data.message);
+
+ /*NetworkManager* nm = reinterpret_cast<NetworkManager*>(userData);
+ nm->NotifySubscribers(data.message, data.title);*/
}
else
{
LOG_DBG("END");
}
-//ErrorCode NetworkManager::AddNotificationCallback(
-// const NotificationCallback& callback)
-//{
-// return ErrorCode::Success;
-//}
+ErrorCode NetworkManager::RegisterNotificationCallback(
+ const NotificationCallback& callback)
+{
+ if (callback)
+ {
+ m_notificationCallbackList.push_back(callback);
+ LOG_DBG("callback was registered successfully");
+
+ return ErrorCode::Success;
+ }
+
+ return ErrorCode::InvalidArgument;
+}
+
+ErrorCode NetworkManager::NotifySubscribers(const std::string& title, const std::string& message)
+{
+ for (auto callback : m_notificationCallbackList)
+ {
+ try
+ {
+ callback(title, message);
+ }
+ catch (...)
+ {
+ LOG_ERR("Caught exception");
+ }
+ }
+
+ return ErrorCode::Success;
+}
ErrorCode NetworkManager::SubscribeOnNotifications()
{
return ErrorCode::Success;
}
+ErrorCode NetworkManager::UnsubscribeOnNotifications()
+{
+ ASSERT_RETURN(m_moduleSymbols.m_nmUnsubsOnNotifFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
+
+ m_moduleSymbols.m_nmUnsubsOnNotifFunc(m_context);
+ return ErrorCode::Success;
+}
void NetworkManager::FreeCache()
{
if (m_context)
{
FreeCache();
+ UnsubscribeOnNotifications();
m_moduleSymbols.m_nmCleanUpFunc(&m_context);
}
}
!= NM_ErrorCode::EC_OK)
{
LOG_INFO("nnlib returned: %s", std::to_string(res).c_str());
- switch (res)
- {
- case NM_ErrorCode::EC_AUTH_ERROR:
- {
- return ErrorCode::IncorrectIdOrPsswd;
- }
- default:
- {
- //TODO: define what we should do in case of no internet connection
- return ErrorCode::Error;
- }
- }
+
+ return GetCorrespondingErrorCode(res);
}
SubscribeOnNotifications();
{
ASSERT_RETURN(m_moduleSymbols.m_nmSignOutFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
FreeCache();
+ UnsubscribeOnNotifications();
m_moduleSymbols.m_nmSignOutFunc(m_context);
return ErrorCode::Success;
}
-SessionState NetworkManager::GetSessionState()
+ErrorCode NetworkManager::GetSessionState(SessionState& sessionState)
{
- ASSERT_RETURN(m_moduleSymbols.m_nmGetSessionStateFunc, "function isn't loaded", SessionState::Closed);
+ ASSERT_RETURN(m_moduleSymbols.m_nmGetSessionStateFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
SignInState state = m_moduleSymbols.m_nmGetSessionStateFunc(m_context);
switch (state)
{
- case SignInState::SIGNED_IN:
- {
- return SessionState::Open;
- }
- default:
- {
- return SessionState::Closed;
- }
+ case SignInState::SIGNED_IN:
+ {
+ sessionState = SessionState::Open;
+ break;
+ }
+ default:
+ {
+ sessionState = SessionState::Closed;
+ break;
+ }
}
- return SessionState::Closed;
+ return ErrorCode::Success;
}
DevicePtr NetworkManager::ToDevice(const NM_DeviceInfo* device)
nmList = nullptr;
}
-DeviceList NetworkManager::GetOwnedDeviceList()
+ErrorCode NetworkManager::GetOwnedDeviceList(DeviceList& deviceList)
{
- ASSERT_RETURN(m_moduleSymbols.m_nmGetOwnedDevFunc, "function isn't loaded", DeviceList());
+ ASSERT_RETURN(m_moduleSymbols.m_nmGetOwnedDevFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
NM_ErrorCode res;
if (m_ownedDevices)
{
LOG_ERR("network manager returned: %s", std::to_string(res).c_str());
- return DeviceList();
+ return GetCorrespondingErrorCode(res);
}
- return ToDeviceList(m_ownedDevices);
+ deviceList = ToDeviceList(m_ownedDevices);
+
+ return ErrorCode::Success;
}
-DeviceList NetworkManager::GetUnownedDeviceList()
+ErrorCode NetworkManager::GetUnownedDeviceList(DeviceList& deviceList)
{
- ASSERT_RETURN(m_moduleSymbols.m_nmGetUnownedDevFunc, "function isn't loaded", DeviceList());
+ ASSERT_RETURN(m_moduleSymbols.m_nmGetUnownedDevFunc, "function isn't loaded", ErrorCode::FunctionNotFound);
NM_ErrorCode res;
FreeDeviceList(&m_unownedDevices);
{
LOG_ERR("network manager returned: %s", std::to_string(res).c_str());
- return DeviceList();
+ return GetCorrespondingErrorCode(res);
}
- return ToDeviceList(m_unownedDevices);
+ deviceList = ToDeviceList(m_unownedDevices);
+
+ return ErrorCode::Success;
}
ErrorCode NetworkManager::UnownDevice(const std::string& deviceId)
{
LOG_ERR("network manager returned: %s", std::to_string(res).c_str());
- return ErrorCode::Error;
+ return GetCorrespondingErrorCode(res);
}
return ErrorCode::Success;
{
LOG_ERR("network manager returned: %s", std::to_string(res).c_str());
- return ErrorCode::Error;
+ return GetCorrespondingErrorCode(res);
}
return ErrorCode::Success;
return m_model->LogOut();
}
-SessionState Presenter::GetSessionState()
+ErrorCode Presenter::GetSessionState(SessionState& state)
{
- return m_model->GetSessionState();
+ return m_model->GetSessionState(state);
}
void Presenter::UpdateSessionState(SessionState sessionState)
{
}
-DeviceList Presenter::GetOwnedDeviceList()
+ErrorCode Presenter::GetOwnedDeviceList(DeviceList& deviceList)
{
- return m_model->GetOwnedDeviceList();
+ return m_model->GetOwnedDeviceList(deviceList);
}
-DeviceList Presenter::GetUnownedDeviceList()
+ErrorCode Presenter::GetUnownedDeviceList(DeviceList& deviceList)
{
- return m_model->GetUnownedDeviceList();
+ return m_model->GetUnownedDeviceList(deviceList);
}
ErrorCode Presenter::UnownDevice(const std::string& deviceId)
if (!webViewPtr)
{
LOG_ERR("Error:connector error");
- response = std::to_string(iotswsec::MessageProcessResult::ErrorInternalConnectionFailed);
+ response = std::to_string(iotswsec::ErrorCode::InternalConnectionFailed);
throw std::runtime_error("WebView instance is uninitialized");
}
catch (const std::invalid_argument& e)
{
LOG_ERR("failed to parse JSON: %s", e.what());
- response = std::to_string(iotswsec::MessageProcessResult::ErrorBadMessageFormat);
+ response = std::to_string(iotswsec::ErrorCode::BadMessageFormat);
}
catch (const std::exception& ex)
{
return false;
}
- // Bind model to presenter.;
+ // Bind model to presenter.
presenterPtr->SetModel(modelPtr);
// Initialize view.
webViewPtr = std::make_shared<iotswsec::WebView>(presenterPtr, WebViewCallback);
void service_app_terminate(void *data)
{
- // Todo: add your code here.
+ LOG_DBG("BEGIN");
+ LOG_DBG("END");
return;
}
std::string WebView::ProcessRequest(const std::string& command, const JSONNode& attachedObject)
{
- LOG_INFO("command: %s", command.c_str());
- LOG_INFO("attached object: %s", attachedObject.write().c_str());
+ LOG_DBG("command: %s", command.c_str());
+ LOG_DBG("attached object: %s", attachedObject.write().c_str());
JSONNode response(JSON_NODE);
if (callbackIterator == m_callbackMap.end())
{
LOG_ERR("Command %s, has no according callback", command.c_str());
- response.push_back(JSONNode(ErrorCodeNodeName, std::to_string(MessageProcessResult::ErrorUnregisteredCommand)));
+ response.push_back(JSONNode(ErrorCodeNodeName, std::stoi(std::to_string(ErrorCode::UnregisteredCommand))));
}
else
{
JSONNode data = SetJSONNodeName(DataNodeName, JSONNode(JSON_NODE));
std::string errDescription;
- MessageProcessResult result = callbackIterator->second(attachedObject, data, errDescription);
+ ErrorCode result = callbackIterator->second(attachedObject, data, errDescription);
response.push_back(JSONNode(ErrorCodeNodeName, std::stoi(std::to_string(result))));
- if (result != MessageProcessResult::Success)
+ if (result != ErrorCode::Success)
{
- response.push_back(JSONNode(ErrDescNodeName, errDescription));
+ response.push_back(JSONNode(ErrDescNodeName, GetErrorDescription(result)));
}
response.push_back(data);
m_callbackMap.insert(CallbackMap::value_type(command, callback));
}
-MessageProcessResult WebView::LogIn(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+ErrorCode WebView::LogIn(const JSONNode& request, JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
// "data" : {"login" : login, "password" : password}
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
}
auto iteratorLogin = request.find("login");
UserInfo userInfo;
userInfo.SetUserLogin(iteratorLogin->as_string());
- if (presenter->LogIn(userInfo, iteratorPsswd->as_string()) != ErrorCode::Success)
- {
- errDescription = "Wrong credentials";
- return MessageProcessResult::IncorrectIdOrPsswd;
- }
-
- return MessageProcessResult::Success;
+ return presenter->LogIn(userInfo, iteratorPsswd->as_string());
}
- return MessageProcessResult::ErrorBadMessageFormat;
+ return ErrorCode::BadMessageFormat;
}
-MessageProcessResult WebView::LogOut(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+ErrorCode WebView::LogOut(const JSONNode& request, JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
// "data" : {}
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
- }
-
- if (presenter->LogOut() != ErrorCode::Success)
- {
- return MessageProcessResult::NotLoggedIn;
+ return ErrorCode::InternalConnectionFailed;
}
- return MessageProcessResult::Success;
+ return presenter->LogOut();
}
-MessageProcessResult WebView::GetSessionState(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+ErrorCode WebView::GetSessionState(const JSONNode& request, JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
// "data" : {}
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
}
- SessionState state = presenter->GetSessionState();
+ SessionState state = SessionState::Closed;
+ ErrorCode res;
int webState = 1;
+ if ((res = presenter->GetSessionState(state)) != ErrorCode::Success)
+ {
+ return res;
+ }
+
switch (state)
{
case SessionState::Open:
response.push_back(JSONNode("state", webState));
- return MessageProcessResult::Success;
+ return ErrorCode::Success;
}
-MessageProcessResult WebView::GetOwnedDevices(const JSONNode& request,
+ErrorCode WebView::GetOwnedDevices(const JSONNode& request,
JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
}
- response.push_back(SetJSONNodeName("devices", ToJson(presenter->GetOwnedDeviceList())));
+ DeviceList deviceList;
+ ErrorCode res;
+
+ if ((res = presenter->GetOwnedDeviceList(deviceList)) != ErrorCode::Success)
+ {
+ return res;
+ }
- return MessageProcessResult::Success;
+ response.push_back(SetJSONNodeName("devices", ToJson(deviceList)));
+
+ return ErrorCode::Success;
}
-MessageProcessResult WebView::GetUnownedDevices(const JSONNode& request,
+ErrorCode WebView::GetUnownedDevices(const JSONNode& request,
JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
+ }
+
+ DeviceList deviceList;
+ ErrorCode res;
+
+ if ((res = presenter->GetUnownedDeviceList(deviceList)) != ErrorCode::Success)
+ {
+ return res;
}
- response.push_back(SetJSONNodeName("devices", ToJson(presenter->GetUnownedDeviceList())));
+ response.push_back(SetJSONNodeName("devices", ToJson(deviceList)));
- return MessageProcessResult::Success;
+ return ErrorCode::Success;
}
-MessageProcessResult WebView::OwnDevice(const JSONNode& request,
+ErrorCode WebView::OwnDevice(const JSONNode& request,
JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
}
auto iteratorId = request.find("id");
if (iteratorId == request.end())
{
- return MessageProcessResult::ErrorBadMessageFormat;
+ return ErrorCode::BadMessageFormat;
}
- if (presenter->OwnDevice(iteratorId->as_string()) == ErrorCode::Success)
- {
- response.push_back(JSONNode("id", iteratorId->as_string()));
+ ErrorCode res;
- return MessageProcessResult::Success;
+ if ((res = presenter->OwnDevice(iteratorId->as_string())) != ErrorCode::Success)
+ {
+ return res;
}
- return MessageProcessResult::Error;
+ response.push_back(JSONNode("id", iteratorId->as_string()));
+
+ return ErrorCode::Success;
}
-MessageProcessResult WebView::UnownDevice(const JSONNode& request,
+ErrorCode WebView::UnownDevice(const JSONNode& request,
JSONNode& response, std::string& errDescription) const
{
//Expected JSON:
if (!presenter)
{
- return MessageProcessResult::ErrorInternalConnectionFailed;
+ return ErrorCode::InternalConnectionFailed;
}
auto iteratorId = request.find("id");
if (iteratorId == request.end())
{
- return MessageProcessResult::ErrorBadMessageFormat;
+ return ErrorCode::BadMessageFormat;
}
- if (presenter->UnownDevice(iteratorId->as_string()) == ErrorCode::Success)
- {
- response.push_back(JSONNode("id", iteratorId->as_string()));
+ ErrorCode res;
- return MessageProcessResult::Success;
+ if ((res = presenter->UnownDevice(iteratorId->as_string())) != ErrorCode::Success)
+ {
+ return res;
}
- return MessageProcessResult::Error;
+ response.push_back(JSONNode("id", iteratorId->as_string()));
+
+ return ErrorCode::Success;
}
-MessageProcessResult WebView::GetDashboard(const JSONNode& request,
+ErrorCode WebView::GetDashboard(const JSONNode& request,
JSONNode& response, std::string& errDescription) const
{
- return MessageProcessResult::Error;
+ return ErrorCode::Error;
}
} //namespace iotswsec
TEST_F(NetworkManagerTest, GetSessionStateTest2)
{
- ASSERT_EQ(SessionState::Closed, m_nm.GetSessionState());
+ SessionState state;
+ ASSERT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ ASSERT_EQ(SessionState::Closed, state);
}
TEST_F(NetworkManagerTest, GetSessionStateTest)
{
ASSERT_EQ(ErrorCode::Success, m_nm.LogIn(m_usrInf, m_pass));
- ASSERT_EQ(SessionState::Open, m_nm.GetSessionState());
+ SessionState state;
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ EXPECT_EQ(SessionState::Open, state);
EXPECT_EQ(ErrorCode::Success, m_nm.LogOut());
}
{
ASSERT_EQ(ErrorCode::Success, m_nm.LogIn(m_usrInf, m_pass));
- ASSERT_EQ(SessionState::Open, m_nm.GetSessionState());
+ SessionState state;
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ EXPECT_EQ(SessionState::Open, state);
- DeviceList devList = m_nm.GetOwnedDeviceList();
+ DeviceList devList;
+
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetOwnedDeviceList(devList));
std::cout << "Number of found owned diveces: " << devList.size() << std::endl;
{
ASSERT_EQ(ErrorCode::Success, m_nm.LogIn(m_usrInf, m_pass));
- ASSERT_EQ(SessionState::Open, m_nm.GetSessionState());
+ SessionState state;
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ EXPECT_EQ(SessionState::Open, state);
+
+ DeviceList devList;
- DeviceList devList = m_nm.GetUnownedDeviceList();
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetUnownedDeviceList(devList));
std::cout << "Number of found unowned diveces: " << devList.size() << std::endl;
{
ASSERT_EQ(ErrorCode::Success, m_nm.LogIn(m_usrInf, m_pass));
- ASSERT_EQ(SessionState::Open, m_nm.GetSessionState());
+ SessionState state;
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ EXPECT_EQ(SessionState::Open, state);
- DeviceList devList = m_nm.GetUnownedDeviceList();
+ DeviceList devList;
+
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetUnownedDeviceList(devList));
std::cout << "Number of found unowned diveces: " << devList.size() << std::endl;
{
ASSERT_EQ(ErrorCode::Success, m_nm.LogIn(m_usrInf, m_pass));
- ASSERT_EQ(SessionState::Open, m_nm.GetSessionState());
+ SessionState state;
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetSessionState(state));
+ EXPECT_EQ(SessionState::Open, state);
+
+ DeviceList devList;
- DeviceList devList = m_nm.GetOwnedDeviceList();
+ EXPECT_EQ(ErrorCode::Success, m_nm.GetUnownedDeviceList(devList));
std::cout << "Number of found owned diveces: " << devList.size() << std::endl;
if (userInfo.GetUserLogin().compare("iotgod")
|| psswd.compare("iotivity"))
{
- return ErrorCode::IncorrectIdOrPsswd;
+ return ErrorCode::AuthError;
}
m_sessionState = true;
{
if (!m_sessionState)
{
- return ErrorCode::NotLoggedIn;
+ return ErrorCode::NotAuthorized;
}
m_sessionState = false;
return ErrorCode::Success;
}
- SessionState GetSessionState() override
+ ErrorCode GetSessionState(SessionState& state) override
{
- return m_sessionState ? SessionState::Open : SessionState::Closed;
+ state = (m_sessionState) ? SessionState::Open : SessionState::Closed;
+
+ return ErrorCode::Success;
}
- DeviceList GetOwnedDeviceList() override
+ ErrorCode GetOwnedDeviceList(DeviceList& deviceList) override
{
- DeviceList list({std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
- std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)});
+ deviceList = {std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
+ std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)};
- return list;
+ return ErrorCode::Success;
}
- DeviceList GetUnownedDeviceList() override
+ ErrorCode GetUnownedDeviceList(DeviceList& deviceList) override
{
- DeviceList list({std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
- std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)});
+ deviceList = {std::make_shared<Device>("123456789", "My TV", "smart-tv", "TV device", 0),
+ std::make_shared<Device>("1234567890", "My Smartphone", "smartphone", "Smartphone device", 1)};
- return list;
+ return ErrorCode::Success;
}
ErrorCode UnownDevice(const std::string& deviceId) override
request.push_back(JSONNode("password", "iotivity"));
JSONNode response;
std::string errDesc;
- MessageProcessResult result = webViewPtr->LogIn(request, response, errDesc);
+ ErrorCode result = webViewPtr->LogIn(request, response, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
}
TEST_F(WebViewTest, LogInTest2)
request.push_back(JSONNode("password", "smth_else"));
JSONNode response;
std::string errDesc;
- MessageProcessResult result = webViewPtr->LogIn(request, response, errDesc);
+ ErrorCode result = webViewPtr->LogIn(request, response, errDesc);
- EXPECT_EQ(MessageProcessResult::IncorrectIdOrPsswd, result);
+ EXPECT_EQ(ErrorCode::AuthError, result);
}
TEST_F(WebViewTest, LogOutTest1)
JSONNode request(JSON_NODE);
JSONNode response;
std::string errDesc;
- MessageProcessResult result = webViewPtr->LogOut(request, response, errDesc);
+ ErrorCode result = webViewPtr->LogOut(request, response, errDesc);
- EXPECT_EQ(MessageProcessResult::NotLoggedIn, result);
+ EXPECT_EQ(ErrorCode::NotAuthorized, result);
}
TEST_F(WebViewTest, LogOutTest2)
std::string errDesc;
webViewPtr->LogIn(request, response, errDesc);
- MessageProcessResult result = webViewPtr->LogOut(request, response, errDesc);
+ ErrorCode result = webViewPtr->LogOut(request, response, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
}
TEST_F(WebViewTest, GetSessionState1)
std::string errDesc;
webViewPtr->LogIn(request, response, errDesc);
- MessageProcessResult result = webViewPtr->GetSessionState(request, response, errDesc);
+ ErrorCode result = webViewPtr->GetSessionState(request, response, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
}
TEST_F(WebViewTest, GetSessionState2)
JSONNode stateRequest;
JSONNode stateResponse;
- MessageProcessResult result = webViewPtr->GetSessionState(stateRequest, stateResponse, errDesc);
+ ErrorCode result = webViewPtr->GetSessionState(stateRequest, stateResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
EXPECT_EQ(std::string("{\"state\":0}"), stateResponse.write());
}
JSONNode ownedGetRequest;
JSONNode ownedGetResponse;
std::string errDesc;
- MessageProcessResult result = webViewPtr->GetOwnedDevices(ownedGetRequest, ownedGetResponse, errDesc);
+ ErrorCode result = webViewPtr->GetOwnedDevices(ownedGetRequest, ownedGetResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
EXPECT_EQ(std::string(R"({"devices":[{"id":"123456789","name":"My TV","type":"smart-tv","status":0,)"
R"("description":"TV device"},)"
R"({"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,)"
JSONNode unownedGetRequest;
JSONNode unownedGetResponse;
std::string errDesc;
- MessageProcessResult result = webViewPtr->GetUnownedDevices(unownedGetRequest, unownedGetResponse, errDesc);
+ ErrorCode result = webViewPtr->GetUnownedDevices(unownedGetRequest, unownedGetResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
EXPECT_EQ(std::string(R"({"devices":[{"id":"123456789","name":"My TV","type":"smart-tv","status":0,)"
R"("description":"TV device"},)"
R"({"id":"1234567890","name":"My Smartphone","type":"smartphone","status":1,)"
std::string errDesc;
const std::string id("123");
ownRequest.push_back(JSONNode("id", id));
- MessageProcessResult result = webViewPtr->OwnDevice(ownRequest, ownResponse, errDesc);
+ ErrorCode result = webViewPtr->OwnDevice(ownRequest, ownResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
EXPECT_EQ(std::string(R"({"id":")" + id + R"("})"), ownResponse.write());
}
std::string errDesc;
const std::string id("123");
unownRequest.push_back(JSONNode("id", id));
- MessageProcessResult result = webViewPtr->UnownDevice(unownRequest, unownResponse, errDesc);
+ ErrorCode result = webViewPtr->UnownDevice(unownRequest, unownResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
EXPECT_EQ(std::string(R"({"id":")" + id + R"("})"), unownResponse.write());
}
std::string errDesc;
const std::string id("123");
getDashboardRequest.push_back(JSONNode("id", id));
- MessageProcessResult result = webViewPtr->GetDashboard(getDashboardRequest, getDashboardResponse, errDesc);
+ ErrorCode result = webViewPtr->GetDashboard(getDashboardRequest, getDashboardResponse, errDesc);
- EXPECT_EQ(MessageProcessResult::Success, result);
+ EXPECT_EQ(ErrorCode::Success, result);
}
} //namespace iotswsec