NM_ErrorCode NM_setDevicePolicy(NM_hContext ctx, const char* dev_id, const char* policy);
/**
+ * @brief NM_getDeviceAgents get agents list in JSON format for specified device
+ * @param ctx [in] handle to context
+ * @param dev_id [in] device id
+ * @param agents [out] agents list in JSON format (as ASCII-Z), you should call NM_freeCharBuffer on agents
+ * if data not needed any more
+ * @return error code
+ */
+NM_ErrorCode NM_getDeviceAgents(NM_hContext ctx, const char* dev_id, char** agents);
+
+/**
* @brief subscribeNotifications subscribes to secure server notifications.
* Triggered by secure server to notify user.
* @param ctx context handle for internal data storage
return EC_OK;
}
+NM_ErrorCode NM_getDeviceAgents(NM_hContext ctx, const char* dev_id, char** agents)
+{
+ if (ctx == nullptr || dev_id == nullptr || agents == nullptr) return EC_NULL_POINTER;
+ *agents = nullptr;
+
+ try
+ {
+ std::string result = ctx->instance->getAgentsList(dev_id);
+ if (result.empty()) result = "[]";
+ *agents = toASCIIZ(result);
+ }
+ catch(NMexception& e)
+ {
+ LOG_E(TAG, "Get agents list error: %s, error code %d", e.what(), e.errorCode());
+ return static_cast<NM_ErrorCode>(e.errorCode());
+ }
+ catch (std::exception& e)
+ {
+ LOG_E(TAG, "Get agents list error: %s", e.what());
+ return EC_INTERNAL_ERROR;
+ }
+
+ return EC_OK;
+}
+
void NM_freeCharBuffer(char* buffer)
{
if (buffer != nullptr) delete[] buffer;
*/
void postPolicy(const std::string& uuid, const std::string& policy);
-// /**
-// * @brief getPolicy return policy for specified device and agent
-// * @param uuid [in] device identifier
-// * @param agent [in] agent identifier or empty string for user enforced policies
-// * @return policy in JSON format or throw exception on error
-// */
-// std::string getAgentsList(const std::string& uuid, const std::string& agent);
+ /**
+ * @brief getAgentsList return list of agents in JSON format
+ * @param uuid [in] device identifier
+ * @return agents list in JSON format
+ */
+ std::string getAgentsList(const std::string& uuid);
private:
void guardUnauthorized();
+ void checkPolicyResource();
+ void checkReportResource();
static const std::string DEFAULT_PROVIDER;
static const int DEFAULT_TIMEOUT;
unsubscribeNotifications();
+ // Currently OCPlatform::unsubscribePresence works incorrect
/*if (params->subscribed)
{
OCPlatform::unsubscribePresence(params->presenceHandle);
std::string IoTivity::getDeviceReport(const std::string& uuid)
{
- if (!params->reportClient)
- {
- params->reportClient = make_shared<ReportClient>(oAuthCred.host);
- }
-
- if (!bool(*(params->reportClient)))
- {
- LOG_E(TAG, "Report resource not found");
- params->reportClient.reset();
- throw IoTInternalError("Report resource not found");
- }
+ checkReportResource();
QueryParamsMap qp{{"did", uuid}};
void IoTivity::postReport(const std::string& report)
{
- if (!params->reportClient)
- {
- params->reportClient = make_shared<ReportClient>(oAuthCred.host);
- }
-
- if (!bool(*(params->reportClient)))
- {
- LOG_E(TAG, "Report resource not found");
- params->reportClient.reset();
- throw IoTInternalError("Report resource not found");
- }
-
+ checkReportResource();
params->reportClient->postReport(report);
}
std::string IoTivity::getPolicy(const std::string& uuid, const std::string& agent)
{
- if (!params->policyClient)
- {
- params->policyClient = make_shared<PolicyClient>(oAuthCred.host);
- }
-
- if (!bool(*(params->policyClient)))
- {
- LOG_E(TAG, "Policy resource not found");
- params->policyClient.reset();
- throw IoTInternalError("Policy resource not found");
- }
-
+ checkPolicyResource();
return params->policyClient->getPolicy(uuid, agent);
}
void IoTivity::postPolicy(const std::string& uuid, const std::string& policy)
{
+ checkPolicyResource();
+ params->policyClient->postPolicy(uuid, "", policy);
+}
+
+std::string IoTivity::getAgentsList(const std::string& uuid)
+{
+ checkPolicyResource();
+ return params->policyClient->getPolicy(uuid, "list");
+}
+
+void IoTivity::setPersistentStoragePath(std::string newPath)
+{
+ params->device_id.clear();
+ persistentStoragePath = newPath;
+}
+
+std::string IoTivity::getPersistentStoragePath()
+{
+ return persistentStoragePath;
+}
+
+void IoTivity::checkPolicyResource()
+{
if (!params->policyClient)
{
params->policyClient = make_shared<PolicyClient>(oAuthCred.host);
params->policyClient.reset();
throw IoTInternalError("Policy resource not found");
}
-
- params->policyClient->postPolicy(uuid, "", policy);
}
-void IoTivity::setPersistentStoragePath(std::string newPath)
+void IoTivity::checkReportResource()
{
- params->device_id.clear();
- persistentStoragePath = newPath;
-}
+ if (!params->reportClient)
+ {
+ params->reportClient = make_shared<ReportClient>(oAuthCred.host);
+ }
-std::string IoTivity::getPersistentStoragePath()
-{
- return persistentStoragePath;
+ if (!bool(*(params->reportClient)))
+ {
+ LOG_E(TAG, "Report resource not found");
+ params->reportClient.reset();
+ throw IoTInternalError("Report resource not found");
+ }
}
}
auto agents = Agent::get().all();
Json::Value root;
- Json::Value jagents;
for (auto agent : agents)
{
Json::Value jagent;
jagent["name"] = agent.getName();
jagent["id"] = agent.getId();
- jagents.append(jagent);
+ root.append(jagent);
}
- root["agents"] = jagents;
policy = root.toStyledString();
}
else
#ESMediatorRich
${CTRL_APP_LIB_PROJECT_NAME}
${AGENT_LIB_PROJECT_NAME}
+ jsoncpp
)
install(TARGETS ${PROJECT_NAME} DESTINATION ${TESTS_DIR})
#include "iotivity.h"
#include <thread>
#include <chrono>
+#include <jsoncpp/json/reader.h>
using namespace NetworkManager;
ASSERT_EQ(policy, policy_recv);
ASSERT_NO_THROW(NM_freeCharBuffer(policy_recv));
}
+
+/**
+ * Test checks policy features
+ * 1. Get agents list
+ * 2. Check it's not empty
+ * 3. Check JSON format is correct
+ */
+TEST_F(IoTDevManagerTest, agentsListTest)
+{
+ char* agents;
+ ASSERT_EQ(EC_NULL_POINTER, NM_getDeviceAgents(ctx, nullptr, &agents));
+ ASSERT_EQ(EC_OK, NM_getDeviceAgents(ctx, IoTivity::getInstance()->getDeviceID().c_str(), &agents));
+ ASSERT_NE(nullptr, agents);
+
+ std::string agents_list(agents);
+ ASSERT_NO_THROW(NM_freeCharBuffer(agents));
+
+ ASSERT_FALSE(agents_list.empty());
+
+ Json::Value root;
+ Json::Reader reader;
+ std::cout << agents_list << std::endl;
+ ASSERT_TRUE(reader.parse(agents_list, root));
+}