Added get Agents list API for iotivity and control app.
authorLomtev Dmytro <d.lomtev@samsung.com>
Wed, 24 May 2017 08:13:37 +0000 (11:13 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Wed, 24 May 2017 08:13:37 +0000 (11:13 +0300)
device_core/ctrl_app_lib/inc/nmlib.h
device_core/ctrl_app_lib/src/ctrl_app_support.cpp
device_core/iotivity_lib/inc/iotivity.h
device_core/iotivity_lib/src/iotivity.cpp
device_core/secserver/policy_resource.cpp
device_core/utest/CMakeLists.txt
device_core/utest/test_iot_dev_manager.cpp

index c26176c..62f34b0 100644 (file)
@@ -261,6 +261,16 @@ NM_ErrorCode NM_getDevicePolicy(NM_hContext ctx, const char* dev_id, const char*
 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
index db7f831..b485d6a 100644 (file)
@@ -445,6 +445,31 @@ NM_ErrorCode NM_setDevicePolicy(NM_hContext ctx, const char* dev_id, const char*
     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;
index 7e05a3f..6ae95ab 100644 (file)
@@ -160,16 +160,17 @@ public:
      */
     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;
index dd4ff75..751c0fb 100644 (file)
@@ -275,6 +275,7 @@ void IoTivity::signOut()
 
     unsubscribeNotifications();
 
+    // Currently OCPlatform::unsubscribePresence works incorrect
     /*if (params->subscribed)
     {
         OCPlatform::unsubscribePresence(params->presenceHandle);
@@ -568,17 +569,7 @@ void IoTivity::unsubscribeNotifications()
 
 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}};
 
@@ -587,40 +578,41 @@ std::string IoTivity::getDeviceReport(const std::string& 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);
@@ -632,19 +624,21 @@ void IoTivity::postPolicy(const std::string& uuid, const std::string& policy)
         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");
+    }
 }
 
 }
index d754218..ac72d48 100644 (file)
@@ -96,16 +96,14 @@ OCRepresentation PolicyResource::get(const QueryParamsMap& params)
         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
index 3612edd..d06a087 100644 (file)
@@ -28,6 +28,7 @@ target_link_libraries(${PROJECT_NAME}
         #ESMediatorRich
        ${CTRL_APP_LIB_PROJECT_NAME}
        ${AGENT_LIB_PROJECT_NAME}
+       jsoncpp
 )
 
 install(TARGETS ${PROJECT_NAME} DESTINATION ${TESTS_DIR})
index 147d0fd..f40cf72 100644 (file)
@@ -6,6 +6,7 @@
 #include "iotivity.h"
 #include <thread>
 #include <chrono>
+#include <jsoncpp/json/reader.h>
 
 using namespace NetworkManager;
 
@@ -235,3 +236,27 @@ TEST_F(IoTDevManagerTest, policyTest)
     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));
+}