GetAction from action service using tool
authorSukhyungKang <shine.kang@samsung.com>
Fri, 7 Feb 2025 05:51:05 +0000 (14:51 +0900)
committerSukhyungKang <shine.kang@samsung.com>
Fri, 7 Feb 2025 05:51:05 +0000 (14:51 +0900)
- db smack label should be changed to test
- temporary files are added (json parser / sqlite)

Signed-off-by: SukhyungKang <shine.kang@samsung.com>
src/common/model/action_model.cc [new file with mode: 0644]
src/common/model/action_model.h [new file with mode: 0644]
src/common/model/action_parameter.cc [new file with mode: 0644]
src/common/model/action_parameter.h [new file with mode: 0644]
src/service/src/service.cc
src/service/src/utils/json_parser.cc [new file with mode: 0644]
src/service/src/utils/json_parser.h [new file with mode: 0644]
src/service/src/utils/sqlite_db.cc [new file with mode: 0644]
src/service/src/utils/sqlite_db.h [new file with mode: 0644]

diff --git a/src/common/model/action_model.cc b/src/common/model/action_model.cc
new file mode 100644 (file)
index 0000000..d8ce64b
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2024 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 "action_model.h"
+
+namespace action {
+
+class ActionModel {
+
+}
+
+}
diff --git a/src/common/model/action_model.h b/src/common/model/action_model.h
new file mode 100644 (file)
index 0000000..8b30007
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2024 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.
+ */
+
+#ifndef MODEL_ACTION_MODEL_H_
+#define MODEL_ACTION_MODEL_H_
+
+#include <vector>
+#include "action_parameter.h"
+namespace action {
+
+enum class ActionType {
+  AppControl = 0,
+  Plugin = 1
+}
+
+class ActionModel {
+ public:
+  ActionModel() = default;
+  ~ActionModel() = default;
+
+ private:
+  std::vector<ActionParameter> params_;
+}
+
+}   // namespace action
+
+#endif  // MODEL_ACTION_MODEL_H_
diff --git a/src/common/model/action_parameter.cc b/src/common/model/action_parameter.cc
new file mode 100644 (file)
index 0000000..3fb7901
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2024 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 "action_parameter.h"
+
+namespace action {
+
+class ActionParameter {
+
+}
+
+}
diff --git a/src/common/model/action_parameter.h b/src/common/model/action_parameter.h
new file mode 100644 (file)
index 0000000..d94c9e4
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2024 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.
+ */
+
+#ifndef MODEL_ACTION_MODEL_H_
+#define MODEL_ACTION_MODEL_H_
+
+#include "action_parameter.h"
+
+namespace action {
+
+enum class ParameterType {
+  IntType = 1,
+  StringType = 0
+}
+
+class ActionParameter {
+ public:
+  ActionParameter() = default;
+  ~ActionParameter();
+
+ private:
+  ParameterType type_;
+  std::string name_;
+  std::string description_;
+  bool is_required_;
+}
+
+
+
+}   // namespace action
+
+#endif  // MODEL_ACTION_MODEL_H_
index 7a13c0de138d32b567fe2c93dd41f7ea4485438e..337fac29453f540b31facc4b90a0d175fbb80754 100644 (file)
 
 #include "irequest_handler.h"
 #include "service.h"
+#include "utils/logging.h"
+
+#include "utils/sqlite_db.h"
+#include "utils/json_parser.h"
 
 namespace service {
 
@@ -37,7 +41,43 @@ std::vector<rs::Action> Service::ListActions() {
 rs::Action Service::GetAction(std::string action_id) {
   // return action by id
   // handler_->GetAction(action_id);
-  return {};
+  LOG(DEBUG) << "GetAction : " << action_id;
+
+  std::string result;
+  utils::SqliteDb sqlite_db;
+
+  sqlite_db.Select(action_id, result);
+
+  LOG(DEBUG) << "GetAction result : " << result;
+
+  auto ps = new utils::JsonParser(result);
+
+  auto params = ps->GetParameters();
+
+  rs::Action action;
+  std::vector<rs::Parameter> actionparams;
+
+  action.Setapp_id(ps->GetAppId());
+  action.Setaction_id(ps->GetName());
+  action.Setlabel(ps->GetName());
+  action.Setdescription(ps->GetDescription());
+
+  for (auto const& iter : params) {
+    auto param_value = iter.second;
+
+    auto param = rs::Parameter(iter.first, param_value["type"], param_value["desc"],
+                              param_value["isMandatory"].compare("true") == 0 ? true : false);
+
+    actionparams.push_back(param);
+  }
+
+  action.Setparameters(actionparams);
+
+  LOG(DEBUG) << "appid : " << action.Getapp_id();
+  LOG(DEBUG) << "actionid : " << action.Getaction_id();
+
+  // return {};
+  return action;
 }
 
 std::vector<rs::VectorDbResult> Service::GetActionId(
diff --git a/src/service/src/utils/json_parser.cc b/src/service/src/utils/json_parser.cc
new file mode 100644 (file)
index 0000000..3cf1393
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2025 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 <stdexcept>
+
+#include "json_parser.h"
+
+#include "logging.h"
+
+namespace utils {
+
+JsonParser::JsonParser(std::string json_str)
+    : json_str_(json_str) {
+  LOG(DEBUG) << "ParseStr const";
+  
+  std::size_t pos = 0;
+  std::size_t lpos = 0;
+  std::string params;
+
+  // remove {}
+  pos = json_str_.find("{");
+  lpos = json_str_.rfind("}");
+  json_str_ = json_str_.substr(pos + 1, lpos - pos - 1);
+
+  // seperate params
+  pos = json_str_.find("{");
+  lpos = json_str_.rfind("}");
+  params = json_str_.substr(pos + 1, lpos - pos - 1);
+  json_str_.replace(pos + 1, lpos - pos - 1, " ");
+
+  // make nodes
+  while(true) {
+    pos = json_str_.find(",");
+
+    if (pos == std::string::npos)
+      break;
+    
+    std::string sub_ = json_str_.substr(0, pos);
+    std::size_t spos = sub_.find(":");
+    std::string key = sub_.substr(1, spos - 2);
+    sub_ = sub_.substr(spos + 1);
+    std::string value = sub_.substr(1, sub_.length() - 2);
+
+    nodes_[key] = value;
+
+    // LOG(DEBUG) << key << " : " << value;
+
+    json_str_ = json_str_.substr(pos + 1);
+  }
+
+//   LOG(DEBUG) << params;
+
+  std::size_t compos = 0;
+
+  // make params
+  while(true) {
+    pos = params.find("{");
+    lpos = params.find("}");
+
+    std::string key = params.substr(1, pos - 3);
+    std::string value = params.substr(pos + 1, lpos - pos - 1);
+
+    // LOG(DEBUG) << key << " : " << value;
+
+
+    std::size_t subpos = 0;
+    std::size_t nextpos = 0;
+    std::size_t compos_ = 0;
+    std::map<std::string, std::string> mvalue;
+
+    // make param value
+    while(true) {
+    //   LOG(DEBUG) << value;
+
+      subpos = value.find("\"");
+      nextpos = value.find("\"", subpos + 1);
+
+      std::string subkey = value.substr(1, nextpos - 1);
+
+      subpos = value.find("\"", nextpos + 1);
+      nextpos = value.find("\"", subpos + 1);
+
+      std::string subval = value.substr(subpos + 1, nextpos - subpos - 1);
+
+      mvalue[subkey] = subval;
+
+      compos_ = value.find(",", nextpos);
+      if (compos_ == std::string::npos)
+        break;
+
+      value = value.substr(compos_ + 1);
+    }
+    params_[key] = mvalue;
+
+    compos = params.find(",", lpos);
+    if (compos == std::string::npos)
+      break;
+
+    params = params.substr(lpos + 2);
+  }
+
+  LOG(DEBUG) << "ParseStr const end";
+}
+
+std::string JsonParser::GetAppId() {
+  return nodes_["appId"];
+}
+
+std::string JsonParser::GetName() {
+  return nodes_["name"];
+}
+
+std::string JsonParser::GetDescription() {
+  return nodes_["desc"];
+}
+
+std::string JsonParser::GetType() {
+  return nodes_["type"];
+}
+
+std::string JsonParser::GetVersion() {
+  return nodes_["version"];
+}
+
+std::string JsonParser::GetRequiredPrivilege() {
+  return nodes_["required-privileges"];
+}
+
+std::string JsonParser::GetReturns() {
+  return nodes_["required-privileges"];
+}
+
+std::map<std::string, std::map<std::string, std::string>> JsonParser::GetParameters() {
+  return params_;
+}
+
+}  // namespace utils
diff --git a/src/service/src/utils/json_parser.h b/src/service/src/utils/json_parser.h
new file mode 100644 (file)
index 0000000..10666fa
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2025 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.
+ */
+
+#ifndef JSON_PARSER_H_
+#define JSON_PARSER_H_
+
+// #include <json/json.h>
+
+#include <string>
+#include <map>
+
+namespace utils {
+
+/* temporary parser */
+/* need to be changed to use json */
+
+class JsonParser {
+ public:
+  JsonParser(std::string json_str);
+  ~JsonParser() = default;
+
+  std::string GetAppId();
+  std::string GetName();
+  std::string GetDescription();
+  std::string GetType();
+  std::string GetVersion();
+  std::string GetRequiredPrivilege();
+  std::string GetReturns();
+  std::map<std::string, std::map<std::string, std::string>> GetParameters();
+
+ private:
+  std::string json_str_;
+  std::map<std::string, std::string> nodes_;
+  std::map<std::string, std::map<std::string, std::string>> params_;
+};
+
+}  // namespace utils
+
+#endif  // 
diff --git a/src/service/src/utils/sqlite_db.cc b/src/service/src/utils/sqlite_db.cc
new file mode 100644 (file)
index 0000000..3de6d9a
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2025 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 "sqlite_db.h"
+
+#include "logging.h"
+
+namespace {
+
+constexpr char kDbPath[] = "/opt/dbspace/.tizen_action.db";
+constexpr char kSelectQuery[] =
+    "SELECT json_str FROM action WHERE action_name = ?";
+
+}  // namespace
+
+namespace utils {
+
+bool SqliteDb::Select(std::string action_id, std::string& result) {
+  LOG(DEBUG) << "select : " << action_id;
+
+  int ret;
+  char *val;
+  sqlite3 *db;
+  sqlite3_stmt *stmt = nullptr;
+
+       ret = sqlite3_open_v2(kDbPath, &db, SQLITE_OPEN_READONLY, nullptr);
+       if (ret != SQLITE_OK) {
+    LOG(ERROR) << "failed to open db : " << kDbPath << " , : " << ret;
+               return false;
+       }
+
+  ret = sqlite3_prepare_v2(db, kSelectQuery, strlen(kSelectQuery), &stmt, nullptr);
+       if (ret != SQLITE_OK) {
+               LOG(ERROR) << "failed to prepare : " << ret;
+    return false;
+       }
+
+  sqlite3_bind_text(stmt, 1, action_id.c_str(), -1, SQLITE_STATIC);
+
+  ret = sqlite3_step(stmt);
+  if (ret != SQLITE_ROW) {
+    LOG(ERROR) << "failed to step : " << sqlite3_errmsg(db);
+    return false;
+  }
+
+       val = (char *)sqlite3_column_text(stmt, 0);
+  LOG(DEBUG) << "result : " << val;
+
+  result = val;
+
+  sqlite3_finalize(stmt);
+       sqlite3_close_v2(db);
+
+  return true;
+}
+
+}  // namespace utils
diff --git a/src/service/src/utils/sqlite_db.h b/src/service/src/utils/sqlite_db.h
new file mode 100644 (file)
index 0000000..cd67502
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2025 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.
+ */
+
+#ifndef SQLITE_DB_H_
+#define SQLITE_DB_H_
+
+// #include <database.hpp>
+#include <sqlite3.h>
+
+#include <string>
+
+/* need to be changed to use cpp tizen-database */
+/* neec to be changed smack label for .tizen_action.db */
+
+namespace utils {
+
+class SqliteDb {
+ public:
+  SqliteDb() = default;
+  ~SqliteDb() = default;
+
+  bool Select(std::string action_id, std::string& result);
+
+};
+
+}  // namespace utils
+
+#endif  // SQLITE_DB_H_
\ No newline at end of file