Add ServiceContext & ServiceInfo classes
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 18 Mar 2025 02:04:48 +0000 (11:04 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 18 Mar 2025 02:04:48 +0000 (11:04 +0900)
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/service.hh
src/service_context.cc [new file with mode: 0644]
src/service_context.hh [new file with mode: 0644]
src/service_info.cc [new file with mode: 0644]
src/service_info.hh [new file with mode: 0644]

index acdf1afe806b50d16a188fa2bf41ef8d8d9ce5bb..d03cc8f110f8f3f06e68e6b3dc474b6c8690840a 100644 (file)
 #ifndef SERVICE_HH_
 #define SERVICE_HH_
 
+#include <bundle_cpp.h>
 #include <tizen_core.h>
 #include <tizen_core_channel.h>
 
-#include <bundle_cpp.h>
-
+#include <memory>
 #include <string>
 
 namespace tizen_base {
 
-class Service {
+class Service : public std::enable_shared_from_this<Service> {
  public:
   enum class State {
     Initialized,
diff --git a/src/service_context.cc b/src/service_context.cc
new file mode 100644 (file)
index 0000000..701c843
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * 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 "service_context.hh"
+
+#include <dlfcn.h>
+
+#include <string>
+#include <utility>
+
+#include "log_private.hh"
+
+namespace {
+
+constexpr const char kUnitedServiceInit[] = "UNITED_SERVICE_INIT";
+constexpr const char kUnitedServiceShutdown[] = "UNITED_SERVICE_SHUTDOWN";
+
+}  // namespace
+
+namespace tizen_base {
+
+ServiceContext::ServiceContext(std::shared_ptr<ServiceInfo> info)
+    : info_(std::move(info)) {}
+
+ServiceContext::~ServiceContext() { Shutdown(); }
+
+bool ServiceContext::Init() {
+  if (!Load()) return false;
+
+  auto* init_func = reinterpret_cast<void* (*)(const char*)>(
+      dlsym(handle_, kUnitedServiceInit));
+  if (init_func == nullptr) {
+    _E("dlsym() is failed. error=%s", dlerror());
+    Unload();
+    return false;
+  }
+
+  auto* handle = init_func(info_->GetName().c_str());
+  if (handle == nullptr) {
+    _E("Failed to initialize united service");
+    Unload();
+    return false;
+  }
+
+  auto* service = static_cast<Service*>(handle);
+  service_ = service->shared_from_this();
+  return true;
+}
+
+void ServiceContext::Shutdown() {
+  if (!handle_) return;
+
+  auto* shutdown_func = reinterpret_cast<void (*)(void)>(dlsym(handle_, kUnitedServiceShutdown));
+  if (shutdown_func == nullptr) {
+    _E("dlsym() is failed. error=%s", dlerror());
+  } else {
+    shutdown_func();
+  }
+
+  Unload();
+}
+
+Service::State ServiceContext::GetState() const { return service_->GetState(); }
+
+const std::string& ServiceContext::GetName() const { return info_->GetName(); }
+
+bool ServiceContext::Load() {
+  if (handle_) return true;
+
+  _D("Load(): %s", info_->GetPath().c_str());
+  handle_ = dlopen(info_->GetPath().c_str(), RTLD_LAZY | RTLD_GLOBAL);
+  if (handle_ == nullptr) {
+    _E("dlopen() is failed. path=%s, error=%s", info_->GetPath().c_str(),
+       dlerror());
+    return false;
+  }
+
+  return true;
+}
+
+void ServiceContext::Unload() {
+  if (!handle_) return;
+
+  _D("Unload(): %s", info_->GetPath().c_str());
+  dlclose(handle_);
+  handle_ = nullptr;
+}
+
+}  // namespace tizen_base
+
diff --git a/src/service_context.hh b/src/service_context.hh
new file mode 100644 (file)
index 0000000..a7fe737
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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 SERVICE_CONTEXT_HH_
+#define SERVICE_CONTEXT_HH_
+
+#include <memory>
+#include <string>
+
+#include "service.hh"
+#include "service_info.hh"
+
+namespace tizen_base {
+
+class ServiceContext {
+ public:
+  ServiceContext(std::shared_ptr<ServiceInfo> info);
+  ~ServiceContext();
+
+  bool Init();
+  void Shutdown();
+  Service::State GetState() const;
+  const std::string& GetName() const;
+
+ private:
+  bool Load();
+  void Unload();
+
+ private:
+  std::shared_ptr<ServiceInfo> info_;
+  std::shared_ptr<Service> service_;
+  void* handle_ = nullptr;
+};
+
+}  // namespace tizen_base
+
+#endif  // SERVICE_CONTEXT_HH_
diff --git a/src/service_info.cc b/src/service_info.cc
new file mode 100644 (file)
index 0000000..f955643
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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 "service_info.hh"
+
+#include <utility>
+
+#include "log_private.hh"
+
+namespace {
+
+static const std::string kTagUnitedService = "United-Service";
+static const std::string kDescription = kTagUnitedService + ":Description";
+static const std::string kName = kTagUnitedService + ":Name";
+static const std::string kMode = kTagUnitedService + ":Mode";
+static const std::string kType = kTagUnitedService + ":Type";
+static const std::string kPath = kTagUnitedService + ":Path";
+
+}  // namespace
+
+namespace tizen_base {
+
+ServiceInfo::ServiceInfo(std::string conf_name, const Dictionary& dictionary)
+    : conf_name_(std::move(conf_name)) {
+  description_ = dictionary.Get(kDescription);
+  name_ = dictionary.Get(kName);
+  mode_ = dictionary.Get(kMode);
+  type_ = dictionary.Get(kType);
+  path_ = dictionary.Get(kPath);
+}
+
+ServiceInfo::~ServiceInfo() {}
+
+const std::string& ServiceInfo::GetConfName() const { return conf_name_; }
+
+const std::string& ServiceInfo::GetDescription() const { return description_; }
+
+const std::string& ServiceInfo::GetName() const { return name_; }
+
+const std::string& ServiceInfo::GetMode() const { return mode_; }
+
+const std::string& ServiceInfo::GetType() const { return type_; }
+
+const std::string& ServiceInfo::GetPath() const { return path_; }
+
+}  // namespace tizen_base
diff --git a/src/service_info.hh b/src/service_info.hh
new file mode 100644 (file)
index 0000000..96a48a7
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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 SERVICE_INFO_HH_
+#define SERVICE_INFO_HH_
+
+#include <string>
+
+#include "dictionary.hh"
+
+namespace tizen_base {
+
+class ServiceInfo {
+ public:
+  ServiceInfo(std::string conf_name, const Dictionary& dictionary);
+  ~ServiceInfo();
+
+  const std::string& GetConfName() const;
+  const std::string& GetDescription() const;
+  const std::string& GetName() const;
+  const std::string& GetMode() const;
+  const std::string& GetType() const;
+  const std::string& GetPath() const;
+
+ private:
+  std::string conf_name_;
+  std::string name_;
+  std::string description_;
+  std::string mode_;
+  std::string type_;
+  std::string path_;
+};
+
+}  // namespace tizen_base
+
+#endif  // SERVICE_INFO_HH_