void Service::stop()
{
+ mainloop.stop();
}
Service::ConnectionRegistry::iterator Service::getConnectionIterator(const int id)
{
throw IoTInternalError("HubClient::getPrimitiveDevices callback not called", EC_UNAUTHORIZED);
}
- else
+
+ if (callback->errorCode > OC_STACK_RESOURCE_CHANGED)
{
- callback->representation.getValue("devices", devices);
+ throw IoTInternalError("HubClient::getPrimitiveDevices error", callback->errorCode);
}
+
+ callback->representation.getValue("devices", devices);
}
Json::Reader reader;
{
throw IoTInternalError("PolicyClient::getPolicy callback not called", EC_UNAUTHORIZED);
}
- else
+
+ if (callback->errorCode > OC_STACK_RESOURCE_CHANGED)
{
- callback->representation.getValue("policy", policy);
+ throw IoTInternalError("PolicyClient::getPolicy callback error", callback->errorCode);
}
+
+ callback->representation.getValue("policy", policy);
}
return policy;
{
throw IoTInternalError("PolicyClient::postPolicy callback not called", EC_UNAUTHORIZED);
}
+
+ if (callback->errorCode > OC_STACK_RESOURCE_CHANGED)
+ {
+ throw IoTInternalError("PolicyClient::postPolicy callback error", callback->errorCode);
+ }
}
} // namespace NetworkManager
{
throw IoTInternalError("ReportClient::getReport callback not called", EC_UNAUTHORIZED);
}
- else
+
+ if (callback->errorCode > OC_STACK_RESOURCE_CHANGED)
{
- callback->representation.getValue("report", report);
+ throw IoTInternalError("ReportClient::getReport callback error", callback->errorCode);
}
+
+ callback->representation.getValue("report", report);
}
return report;
{
throw IoTInternalError("ReportClient::postReport callback not called", EC_UNAUTHORIZED);
}
+
+ if (callback->errorCode > OC_STACK_RESOURCE_CHANGED)
+ {
+ throw IoTInternalError("ReportClient::postReport callback error", callback->errorCode);
+ }
}
} // namespace NetworkManager
const std::string NOTIF_FIND = "Notification findResource()";
const std::string NOTIF_REQUEST = string(OC_RSRVD_WELL_KNOWN_URI) + "?rt=" + NetworkManager::NOTIFICATION_TYPE;
+/**
+ * @brief Check if resource client created and resource found
+ */
template<class ResourceClass>
void checkResource(std::shared_ptr<ResourceClass>& sptr, const std::string& host)
{
}
}
+/**
+ * @brief Wrapper function to perform resource operations, if operation fails reource reseted.
+ */
+template<class Callable, class Obj, typename... Args>
+void callResourceWithResetNonReturn(Callable fn, std::shared_ptr<Obj>& obj, Args&&... params)
+{
+ try
+ {
+ (obj.get()->*fn)(std::forward<Args>(params)...);
+ }
+ catch(std::exception& e)
+ {
+ NetworkManager::IoTivity::getInstance()->signIn();
+ obj.reset();
+ throw e;
+ }
+}
+
+/**
+ * @brief Wrapper function to perform resource operations, if operation fails reource reseted.
+ */
+template<class Callable, class Obj, typename... Args>
+auto callResourceWithResetReturn(Callable fn, std::shared_ptr<Obj>& obj, Args&&... params) -> decltype((std::declval<Obj>().*fn)(params...))
+{
+ try
+ {
+ return (obj.get()->*fn)(std::forward<Args>(params)...);
+ }
+ catch(std::exception& e)
+ {
+ NetworkManager::IoTivity::getInstance()->signIn();
+ obj.reset();
+ throw e;
+ }
+}
+
} // namespace
namespace NetworkManager
if (it != owned.end())
{
-// if (dev->getHost().compare(0, COAP_IF.length(), COAP_IF) == 0)
- {
- it->second = found_device;
- }
-
+ it->second = found_device;
}
else
{
catch (std::exception& e)
{
hubClient.reset();
+ iotivity->signIn();
LOG_E(TAG, "getOwnedDevices from Hub exception: %s", e.what());
}
catch (std::exception& e)
{
hubClient.reset();
+ iotivity->signIn();
LOG_E(TAG, "getUnOwnedDevices from Hub exception: %s", e.what());
}
checkResource(reportClient, iotivity->host());
QueryParamsMap qp{{"did", uuid}};
- return reportClient->getReport(qp);
+ return callResourceWithResetReturn(&ReportClient::getReport, reportClient, qp);
}
void SecurityContext::postReport(const std::string& report)
{
FN_VISIT
checkResource(reportClient, iotivity->host());
- reportClient->postReport(report);
+ callResourceWithResetNonReturn(&ReportClient::postReport, reportClient, report);
}
std::string SecurityContext::getPolicy(const std::string& uuid, const std::string& agent)
auto it = getIoTDevice(uuid);
auto route = it->getRouting();
- return policyClient->getPolicy(route, agent);
+ return callResourceWithResetReturn(&PolicyClient::getPolicy, policyClient, route, agent);
}
void SecurityContext::postPolicy(const std::string& uuid, const std::string& policy)
checkResource(policyClient, iotivity->host());
auto it = getIoTDevice(uuid);
auto route = it->getRouting();
-
- policyClient->postPolicy(route, "", policy);
+ callResourceWithResetNonReturn(&PolicyClient::postPolicy, policyClient, route, "", policy);
}
std::string SecurityContext::getAgentsList(const std::string& uuid)
auto it = getIoTDevice(uuid);
auto route = it->getRouting();
- return policyClient->getPolicy(route, "list");
+ return callResourceWithResetReturn(&PolicyClient::getPolicy, policyClient, route, "list");
}
IoTDevicePtr SecurityContext::getIoTDevice(const std::string& uuid)
std::condition_variable signal;
std::mutex mtx;
std::unique_lock<std::mutex> lock;
+ int errorCode = OC_STACK_ERROR;
CallbackBase() : mtx(), lock(mtx) {}
struct ResourcePublishCallback : public CallbackBase
{
typedef std::shared_ptr<ResourcePublishCallback> Sptr;
- int ecode;
/**
* @brief call - callback routine, called from iotivity framework
{
typedef std::shared_ptr<MqCreateTopicCallback> Sptr;
std::shared_ptr<OC::OCResource> resource;
- int errorCode = OC_STACK_ERROR;
/**
* @brief call - callback routine, called from iotivity framework
std::unique_lock<std::mutex> lck(mtx);
std::condition_variable cvar;
bool fired = false;
- res = _resource->post( /*_type,
- _interface,*/
- _representation,
- _query_params,
- [&](const HeaderOptions&, const OCRepresentation & _rep, const int _ecode)
+ res = _resource->post(
+ _representation,
+ _query_params,
+ [&](const HeaderOptions&, const OCRepresentation & _rep, const int _ecode)
{
- if(_ecode != 4)
+ if(_ecode > 4)
+ {
+ // Fail codes have numbers bigger than 4
res = static_cast<OCStackResult>(_ecode);
+ }
+
fired = true;
cvar.notify_all();
- } );
+ });
+
cvar.wait_for(lck, std::chrono::seconds(3), [&fired] { return fired; });
+
if (!fired)
{
- std::cout << "Post timeout" << std::endl;
- }
- else
- {
- std::cout << "Post code " << res << std::endl;
+ res = OC_STACK_TIMEOUT;
}
}
if (!state)
{
LOG_D(TAG, "Connection losts.");
- instance->connected = false;
+// instance->connected = false;
}
+
+ instance->connected = state;
}
}
}
OC::OCPlatform::findResourceList(cloud ? cloud_host : "", uri, con_type, bind_callback(callback, PH::_1), OC::QualityOfService::HighQos);
callback->wait_timeout(std::chrono::seconds(DEFAULT_TIMEOUT));
-
return devices;
}
bool ok = callback->wait();
- if(!ok || callback->ecode > OC_STACK_RESOURCE_CHANGED)
+ if(!ok || callback->errorCode > OC_STACK_RESOURCE_CHANGED)
{
- throw IoTInternalError("Failed to publish resources", callback->ecode);
+ throw IoTInternalError("Failed to publish resources", callback->errorCode);
}
}
if (auto p = ctx.lock())
{
p->errorCode = errorCode;
-
p->signalize();
}
}
{
try
{
+ p->errorCode = eCode;
+
if(eCode == OC_STACK_OK)
{
p->representation = rep;
{
if (auto p = ctx.lock())
{
- p->ecode = code;
+ p->errorCode = code;
p->signalize();
}
}
service->start();
}
+void AgentPolicyService::stop()
+{
+ service->stop();
+}
+
pid_t AgentPolicyService::getServicePid()
{
return getpid();
*/
void run();
+ /**
+ * @brief stop method stop service
+ */
+ void stop();
+
private:
EnforcePolicyHandler m_enforcePolicyHandler;
std::unique_ptr<rmi::Service> service;
}
}
- std::cout << TAG "Running as ";
+ const char* mode = "unknown";
+
switch (g_working_mode) {
case WorkingMode::Standard:
- std::cout << "standard";
+ mode = "standard";
break;
case WorkingMode::Hub:
- std::cout << "hub";
+ mode = "hub";
break;
case WorkingMode::Primitive:
- std::cout << "primitive";
- break;
- default:
- std::cout << "unknown";
+ mode = "primitive";
break;
}
- std::cout << " device" <<std::endl;
+
+ write_log(TAG "Running as %s device\n", mode);
auto res = signal(SIGSEGV, sig_handler);
}
}
+ agent_policy_service.stop();
// rmithread.stop();
}
{
if (!resource)
{
- std::cout << "[RMI_THREADS] Search for policy resource" << std::endl;
+ LOG_D(TAG, "[RMI_THREADS] Search for policy resource");
findResource();
}
bool PolicyHandler::findResource()
{
- try
- {
- resource = iotivity->findResource(iotivity->host(), POLICY_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, sid);
- }
- catch (std::exception& e)
+ resource = iotivity->findResource(iotivity->host(), POLICY_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, sid);
+
+ if (!resource)
{
- if (iotivity->isSignedIn())
+ if (iotivity->isConnected())
{
try
}
}
+// try
+// {
+// resource = iotivity->findResource(iotivity->host(), POLICY_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, sid);
+// }
+// catch (std::exception& e)
+// {
+// if (iotivity->isConnected())
+// {
+
+// try
+// {
+// iotivity->signIn();
+// resource = iotivity->findResource(iotivity->host(), POLICY_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, sid);
+// }
+// catch(...)
+// {
+
+// }
+// }
+// }
+
if(resource)
{
QueryParamsMap query{{"did", device_id}};
void ReportHandler::findResource()
{
- try
- {
- resource = iotivity->findResource(iotivity->host(), REPORT_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, server_id);
- }
- catch (std::exception& e)
+ resource = iotivity->findResource(iotivity->host(), REPORT_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, server_id);
+
+ if (!resource)
{
- if (iotivity->isSignedIn())
+ if (iotivity->isConnected())
{
+ std::cout << "!!! Try to signIn" << std::endl;
iotivity->signIn();
try
{
{
}
}
+ else
+ {
+ std::cout << "Not connected don't try to signIn" << std::endl;
+ }
}
+
+// try
+// {
+// resource = iotivity->findResource(iotivity->host(), REPORT_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, server_id);
+// }
+// catch (std::exception& e)
+// {
+// if (iotivity->isConnected())
+// {
+// std::cout << "!!! Try to signIn" << std::endl;
+// iotivity->signIn();
+// try
+// {
+// resource = iotivity->findResource(iotivity->host(), REPORT_RESOURCE_TYPE, OC_RSRVD_WELL_KNOWN_URI, server_id);
+// }
+// catch(...)
+// {
+// }
+// }
+// else
+// {
+// std::cout << "Not connected don't try to signIn" << std::endl;
+// }
+// }
}