add get_modes API
authorYoungjae Shin <yj99.shin@samsung.com>
Fri, 11 Oct 2019 07:41:10 +0000 (16:41 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 18 Mar 2020 08:53:50 +0000 (17:53 +0900)
client/mdsc_get_modes.c [new file with mode: 0644]
common/dbus.xml
common/dbus_def.h
include/modes.h
supervisor/ModeManager.cpp
supervisor/ModeManager.h
supervisor/ModesEx.cpp
supervisor/RequestHandler.cpp
supervisor/RequestHandler.h
supervisor/Supervisor.cpp
unittest/modes_test_client.cpp

diff --git a/client/mdsc_get_modes.c b/client/mdsc_get_modes.c
new file mode 100644 (file)
index 0000000..e599421
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "modes.h"
+#include <stdlib.h>
+#include <glib.h>
+#include "mdsc.h"
+#include "mdsc_dbus.h"
+#include "common/dbus_def.h"
+
+static GList* _get_mode_list(GVariant *in_data)
+{
+       gchar *mode_name;
+       gint32 state;
+       GVariantIter *iter;
+       GList *mode_list = NULL;;
+
+       g_variant_get(in_data, MODES_DBUS_GET_MODES_SIG, &iter);
+       while (g_variant_iter_loop(iter, MODES_DBUS_GET_MODES_MODE_SIG, &mode_name, &state)) {
+               DBG("mode(%s) : state(%d)", mode_name, state);
+               modes_mode_t *mode = malloc(sizeof(modes_mode_t));
+               if (NULL == mode) {
+                       ERR("malloc() Fail");
+                       return NULL;
+               }
+               mode->name = strdup(mode_name);
+               mode->state = state;
+               mode_list = g_list_append(mode_list, mode);
+       }
+       g_variant_iter_free(iter);
+       g_variant_unref(in_data);
+
+       return mode_list;
+}
+static int _mdsc_dbus_get_modes_sync(mdsDbus *mdsc_dbus, GList **list)
+{
+       gboolean ret;
+       GError *error = NULL;
+       int result = MODES_ERROR_NONE;
+       GVariant *mode_list = NULL;
+
+       RETV_IF(NULL == list, MODES_ERROR_INVALID_PARAMETER);
+
+       ret = mds_dbus_call_get_modes_sync(mdsc_dbus, &mode_list, &result, NULL, &error);
+       if (FALSE == ret) {
+               ERR("mds_dbus_call_get_modes_sync() Fail(%s)", error ? error->message : "unknown");
+               g_error_free(error);
+               return result;
+       }
+
+       *list = _get_mode_list(mode_list);
+
+       return result;
+}
+
+API int modes_get_modes(GList **list)
+{
+       int ret;
+       mdsDbus* dbus_handle;
+
+       RETV_IF(NULL == list, MODES_ERROR_INVALID_PARAMETER);
+
+       dbus_handle = mdsc_dbus_start();
+       if (NULL == dbus_handle) {
+               ERR("_mdsc_dbus_start() Fail");
+               return MODES_ERROR_SYSTEM;
+       }
+
+       ret = _mdsc_dbus_get_modes_sync(dbus_handle, list);
+       if (MODES_ERROR_NONE != ret) {
+               ERR("_mdsc_bus_client_change_mode_sync() Fail(%d)", ret);
+               return ret;
+       }
+
+       mdsc_dbus_stop(dbus_handle);
+
+       return MODES_ERROR_NONE;
+}
+
+static void _free_mode(gpointer data, gpointer user_data)
+{
+       modes_mode_t *mode = data;
+       free(mode->name);
+       free(mode);
+}
+API void modes_free_modes(GList *list)
+{
+       g_list_foreach(list, _free_mode, NULL);
+       g_list_free(list);
+}
index aee273fcd1b7baf06e27669eea0deb9ce1d06212..46901a696f783cb010f8225c6009d17ae82dac62 100644 (file)
                        <arg type="(siv)" name="modeData" direction="in"/>
                        <arg type="i" name="ret" direction="out"/>
                </method>
-
+               <method name="getModes"> 
+                       <arg type="a(si)" name="modeList" direction="out"/>
+                       <arg type="i" name="ret" direction="out"/>
+               </method>
                <signal name="changedMode">
                        <arg type="s" name="modeName"/>
                        <arg type="i" name="modeState"/>
index 16c8a60856696b1393a2ecb9c725da3e9ed426f9..3717cf52e907db25a47d1affc026db8123a3b118 100644 (file)
@@ -25,3 +25,6 @@
 #define MODES_DBUS_MODE_SIG "(siv)"
 #define MODES_DBUS_ACTION_SIG "(sss)"
 #define MODES_DBUS_ACTION_LIST_SIG "a" MODES_DBUS_ACTION_SIG
+
+#define MODES_DBUS_GET_MODES_MODE_SIG "(si)"
+#define MODES_DBUS_GET_MODES_SIG "a" MODES_DBUS_GET_MODES_MODE_SIG
index 9b71e302fae091ea3eb8abe721d4c5d075ab8dd2..34380816ad0438c3892caba54f09bd3788c9d235 100644 (file)
@@ -20,7 +20,7 @@
 extern "C" {
 #endif
 
-
+#include <glib.h>
 #include <modes_types.h>
 #include <modes_errors.h>
 
@@ -191,6 +191,16 @@ int modes_subscribe_mode_changes(modes_noti_fn cb, void *user_data);
  */
 void modes_unsubscribe_mode_changes();
 
+//TODO:it must be opaque type
+typedef struct {
+       char *name;
+       //int type;
+       int state;
+} modes_mode_t;
+
+int modes_get_modes(GList **list);
+void modes_free_modes(GList *list);
+
 /**
  * @}
  */
index 6c8b02ee2ff3ca80b4c5c71afe1baa8df2fe7855..337261fa79539aa10a05c9c428f1bbd1111e3241 100644 (file)
@@ -133,6 +133,18 @@ int ModeManager::undoMode(const std::string & modeName)
        return careTaker.undoMode(modeName);
 }
 
+std::list<std::tuple<std::string, int>> ModeManager::getModes()
+{
+       std::list<std::tuple<std::string, int>> modeList;
+
+       for (auto it = modeMap.begin(); it != modeMap.end(); ++it) {
+               int state = careTaker.isSavedMode(it->first);
+               modeList.push_back(std::tuple<std::string, int>(it->first, state));
+       }
+
+       return modeList;
+}
+
 //it should be called before init()
 void ModeManager::setOptions(const std::set<string> &modeDirs, const string &xsdFile)
 {
index 93f978470eecd3daa290510d9b4b7cf8127868eb..bc0fef810954df7d47fdfc675fb3237d4846639a 100644 (file)
@@ -40,6 +40,7 @@ public:
        int applyMode(const std::string &modeName);
        int registerMode(const Mode &mode);
        int undoMode(const std::string &modeName);
+       std::list<std::tuple<std::string, int>> getModes();
 private:
        bool makeModeMap(const std::string &dirPath);
        void addModeName(ModeParser *parser, std::string &path);
index 5f2053d6f6b0c215ad5c3c69c7d02c17011df86b..61fd4dec085aac5e823721b365431913ff070a3e 100644 (file)
@@ -25,7 +25,7 @@ ModesEx::ModesEx(ErrCode code)
 {
 }
 
-ModesEx::ModesEx(ErrCode code, const std::string & msg)
+ModesEx::ModesEx(ErrCode code, const std::string &msg)
        : err(code), extraMsg(msg)
 {
 }
index 8ae12c32625975a5d5892f9b8c966cae95f39cd7..ce2f727c973e64d5b774c98f99a2b4c3296669a7 100644 (file)
@@ -69,7 +69,7 @@ gboolean RequestHandler::undoModeHandler(mdsDbus *object, GDBusMethodInvocation
 }
 
 gboolean RequestHandler::registerModeHandler(mdsDbus *object, GDBusMethodInvocation *invocation,
-               GVariant *modeData, gpointer userData)
+       GVariant *modeData, gpointer userData)
 {
        RETV_IF(NULL == modeMgr, FALSE);
        RETV_IF(NULL == modeData, FALSE);
@@ -86,6 +86,32 @@ gboolean RequestHandler::registerModeHandler(mdsDbus *object, GDBusMethodInvocat
        return TRUE;
 }
 
+gboolean RequestHandler::getModesHandler(mdsDbus *object, GDBusMethodInvocation *invocation, gpointer userData)
+{
+       RETV_IF(NULL == modeMgr, FALSE);
+
+       std::list<std::tuple<std::string, int>> modeList = modeMgr->getModes();
+       if (modeList.empty()) {
+               ERR("getModes() : No Data");
+               mds_dbus_complete_get_modes(object, invocation, NULL, MODES_ERROR_NO_DATA);
+               return TRUE;
+       }
+
+       GVariantBuilder *modeListBuilder = g_variant_builder_new(G_VARIANT_TYPE(MODES_DBUS_GET_MODES_SIG));
+       for (auto it = modeList.begin(); it != modeList.end(); ++it) {
+               int state;
+               std::string name;
+               std::tie(name, state) = *it;
+               g_variant_builder_add(modeListBuilder, MODES_DBUS_GET_MODES_MODE_SIG, name.c_str(), state);
+       }
+
+       GVariant *outList = g_variant_new(MODES_DBUS_GET_MODES_SIG, modeListBuilder);
+       g_variant_builder_unref(modeListBuilder);
+
+       mds_dbus_complete_get_modes(object, invocation, outList, MODES_ERROR_NONE);
+       return TRUE;
+}
+
 Mode RequestHandler::getModefromData(GVariant *inData)
 {
        GVariant *actionList;
index e8994218607339dfa91c518fee43a59f0f558257..e0f91b60f5479f729a2dbb0f46d90445ebd787c3 100644 (file)
@@ -26,10 +26,12 @@ class RequestHandler {
 public:
        static gboolean applyModeHandler(mdsDbus *object, GDBusMethodInvocation *invocation,
                const gchar *modeName, gpointer userData);
-       static gboolean undoModeHandler(mdsDbus * object, GDBusMethodInvocation * invocation,
-               const gchar * modeName, gpointer userData);
+       static gboolean undoModeHandler(mdsDbus * object, GDBusMethodInvocation *invocation,
+               const gchar *modeName, gpointer userData);
        static gboolean registerModeHandler(mdsDbus *object, GDBusMethodInvocation *invocation,
                GVariant *arg_modeData, gpointer userData);
+       static gboolean getModesHandler(mdsDbus *object, GDBusMethodInvocation *invocation,
+               gpointer userData);
 
        static void setModeManager(ModeManager *mgr);
        static void setRuleManager(RuleManager *mgr);
index 1662854d5ce1b9a7975c17ea36c2cc69c7813560..45b0d62e8c74831cb802df7f2ee6d50f4c4614a2 100644 (file)
@@ -65,4 +65,5 @@ void Supervisor::registerHandler()
        clientConn.addRequestHandler("handle-apply-mode", (GCallback)&RequestHandler::applyModeHandler);
        clientConn.addRequestHandler("handle-undo-mode", (GCallback)&RequestHandler::undoModeHandler);
        clientConn.addRequestHandler("handle-register-mode", (GCallback)&RequestHandler::registerModeHandler);
+       clientConn.addRequestHandler("handle-get-modes", (GCallback)&RequestHandler::getModesHandler);
 }
index 7b2671200f67a33207a9bfb4cab88187423fd6e6..a8308a3ddca5112adb1c96f39d41f1ba77a2b8fb 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <iostream>
 #include <glib.h>
 #include <gtest/gtest.h>
 #include <modes.h>
@@ -111,3 +112,18 @@ TEST_F(ClientTest, undoModeEx2)
        g_main_loop_run(loop);
        EXPECT_EQ(MODES_ERROR_NONE, result);
 }
+
+TEST_F(ClientTest, getModes)
+{
+       GList *list, *cur;
+       int ret = modes_get_modes(&list);
+       EXPECT_EQ(MODES_ERROR_NONE, ret);
+
+       for (cur = g_list_first(list); cur; cur = g_list_next(cur)) {
+               modes_mode_t *mode = (modes_mode_t*)cur->data;
+               EXPECT_NE(nullptr, mode);
+               EXPECT_NE(nullptr, mode->name);
+               std::cout << mode->name << ":state(" << mode->state << ")" << std::endl;
+       }
+       modes_free_modes(list);
+}