#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,
--- /dev/null
+/*
+ * 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
+
--- /dev/null
+/*
+ * 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_
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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_