INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/aul)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/aul/api)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/aul/app_manager)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/aul/common)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/aul/socket)
SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
AUX_SOURCE_DIRECTORY(src SOURCES)
AUX_SOURCE_DIRECTORY(aul AUL_SOURCES)
AUX_SOURCE_DIRECTORY(aul/api AUL_API_SOURCES)
+AUX_SOURCE_DIRECTORY(aul/app_manager AUL_APP_MANAGER_SOURCES)
+AUX_SOURCE_DIRECTORY(aul/common AUL_COMMON_SOURCES)
+AUX_SOURCE_DIRECTORY(aul/socket AUL_SOCKET_SOURCES)
add_library(aul SHARED
${SOURCES}
${AUL_SOURCES}
- ${AUL_API_SOURCES})
+ ${AUL_API_SOURCES}
+ ${AUL_APP_MANAGER_SOURCES}
+ ${AUL_COMMON_SOURCES}
+ ${AUL_SOCKET_SOURCES})
TARGET_LINK_LIBRARIES(aul ${libpkgs_LDFLAGS})
SET_TARGET_PROPERTIES(aul PROPERTIES SOVERSION ${MAJORVER})
SET_TARGET_PROPERTIES(aul PROPERTIES VERSION ${FULLVER})
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_API_HH_
-#define AUL_API_HH_
-
-#ifdef API
-#undef API
-#endif
-#define API __attribute__ ((visibility("default")))
-
-#endif // AUL_API_HH_
#include <tizen.h>
#include <unistd.h>
-#include "aul/api.hh"
#include "aul/api/aul_app_context.h"
-#include "aul/app_manager.hh"
-#include "aul/log_private.hh"
+#include "aul/app_manager/app_manager.hh"
+#include "aul/common/api.hh"
+#include "aul/common/log_private.hh"
using namespace aul;
*/
#include "aul/api/aul_app_event.h"
-#include "aul/api.hh"
-#include "aul/app_event.hh"
-#include "aul/exception.hh"
-#include "aul/log_private.hh"
+#include "aul/app_manager/app_event.hh"
+#include "aul/common/api.hh"
+#include "aul/common/exception.hh"
+#include "aul/common/log_private.hh"
namespace {
using namespace aul;
#include <tizen.h>
-#include "aul/api.hh"
#include "aul/api/aul_app_manager.h"
-#include "aul/app_manager.hh"
-#include "aul/log_private.hh"
+#include "aul/app_manager/app_manager.hh"
+#include "aul/common/api.hh"
+#include "aul/common/log_private.hh"
using namespace aul;
+++ /dev/null
-/*
- * Copyright (c) 2020 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 <memory>
-
-#include "include/aul.h"
-#include "aul/app_context.hh"
-
-namespace aul {
-
-AppContext::Builder& AppContext::Builder::SetAppId(std::string app_id) {
- app_id_ = std::move(app_id);
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetPkgId(std::string pkg_id) {
- pkg_id_ = std::move(pkg_id);
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetInstId(std::string inst_id) {
- inst_id_ = std::move(inst_id);
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetExec(std::string exec) {
- exec_ = std::move(exec);
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetPid(int pid) {
- pid_ = pid;
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetStatus(int status) {
- status_ = status;
- return *this;
-}
-
-AppContext::Builder& AppContext::Builder::SetIsSubApp(bool is_sub_app) {
- is_sub_app_ = is_sub_app;
- return *this;
-}
-
-AppContext AppContext::Builder::Build() {
- return AppContext(std::move(app_id_), std::move(pkg_id_), std::move(inst_id_),
- std::move(exec_), pid_, status_, is_sub_app_);
-}
-
-AppContext::AppContext(tizen_base::Bundle b) {
- Set(std::move(b));
-}
-
-AppContext::AppContext(std::string app_id,
- std::string pkg_id,
- std::string inst_id,
- std::string exec,
- int pid,
- int status,
- bool is_sub_app)
- : app_id_(std::move(app_id)),
- pkg_id_(std::move(pkg_id)),
- inst_id_(std::move(inst_id)),
- exec_(std::move(exec)),
- pid_(pid),
- status_(status),
- is_sub_app_(is_sub_app) {
-}
-
-AppContext::~AppContext() = default;
-
-const std::string& AppContext::GetAppId() const {
- return app_id_;
-}
-
-const std::string& AppContext::GetPkgId() const {
- return pkg_id_;
-}
-
-const std::string& AppContext::GetInstId() const {
- return inst_id_;
-}
-
-const std::string& AppContext::GetExec() const {
- return exec_;
-}
-
-int AppContext::GetPid() const {
- return pid_;
-}
-
-int AppContext::GetStatus() const {
- return status_;
-}
-
-void AppContext::SetStatus(int status) {
- status_ = status;
-}
-
-bool AppContext::IsSubApp() const {
- return is_sub_app_;
-}
-
-void AppContext::Set(tizen_base::Bundle b) {
- app_id_ = std::move(b.GetString(AUL_K_APPID));
- pkg_id_ = std::move(b.GetString(AUL_K_PKGID));
- inst_id_ = std::move(b.GetString(AUL_K_INSTANCE_ID));
- exec_ = std::move(b.GetString(AUL_K_EXEC));
- pid_ = std::stoi(b.GetString(AUL_K_PID));
- status_ = std::stoi(b.GetString(AUL_K_STATUS));
- is_sub_app_ = static_cast<bool>(std::stoi(b.GetString(AUL_K_IS_SUBAPP)));
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_APP_CONTEXT_HH_
-#define AUL_APP_CONTEXT_HH_
-
-#include <bundle_cpp.h>
-
-#include <string>
-
-namespace aul {
-
-class AppContext {
- public:
- class Builder {
- public:
- Builder& SetAppId(std::string app_id);
- Builder& SetPkgId(std::string pkg_id);
- Builder& SetInstId(std::string inst_id);
- Builder& SetExec(std::string exec);
- Builder& SetPid(int pid);
- Builder& SetStatus(int status);
- Builder& SetIsSubApp(bool is_sub_app);
- AppContext Build();
-
- private:
- std::string app_id_;
- std::string pkg_id_;
- std::string inst_id_;
- std::string exec_;
- int pid_;
- int status_;
- bool is_sub_app_;
- };
-
- AppContext(tizen_base::Bundle b);
- virtual ~AppContext();
-
- const std::string& GetAppId() const;
- const std::string& GetPkgId() const;
- const std::string& GetInstId() const;
- const std::string& GetExec() const;
- int GetPid() const;
- int GetStatus() const;
- bool IsSubApp() const;
-
- void SetStatus(int status);
-
- private:
- AppContext(std::string app_id,
- std::string pkg_id,
- std::string inst_id,
- std::string exec,
- int pid,
- int status,
- bool is_sub_app);
-
- void Set(tizen_base::Bundle b);
-
- private:
- std::string app_id_;
- std::string pkg_id_;
- std::string inst_id_;
- std::string exec_;
- int pid_;
- int status_;
- bool is_sub_app_;
-};
-
-} // namespace aul
-
-#endif // AUL_APP_CONTEXT_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 "aul/app_event.hh"
-#include "aul/exception.hh"
-#include "aul/log_private.hh"
-
-namespace aul {
-
-AppEvent::AppEvent(std::string app_id, AppEvent::IEvent* ev)
- : app_id_(std::move(app_id)), ev_(ev) {
- int ret = aul_listen_app_status(app_id_.c_str(), OnAppStatusCb,
- this, &handle_);
- if (ret != AUL_R_OK) {
- _E("Failed to listen app status event. error(%d)", ret);
- THROW(ret);
- }
-}
-
-AppEvent::~AppEvent() {
- if (handle_)
- aul_ignore_app_status(handle_);
-}
-
-int AppEvent::OnAppStatusCb(aul_app_info* info, int ctx_status, void* data) {
- AppContext::Builder builder;
- builder.SetAppId(info->appid);
- builder.SetPkgId(info->pkgid);
- builder.SetInstId(info->instance_id);
- builder.SetExec(info->app_path);
- builder.SetPid(info->pid);
- builder.SetStatus(info->status);
- builder.SetIsSubApp(static_cast<bool>(info->is_sub_app));
- AppContext app_context = builder.Build();
-
- auto* handle = static_cast<AppEvent*>(data);
- IEvent* ev = handle->ev_;
- if (ctx_status == STATUS_TERMINATE)
- ev->OnAppLaunched(&app_context);
- else
- ev->OnAppTerminated(&app_context);
- return 0;
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_APP_EVENT_HH_
-#define AUL_APP_EVENT_HH_
-
-#include <string>
-
-#include "aul/app_context.hh"
-#include "include/aul.h"
-
-namespace aul {
-
-class AppEvent {
- public:
- class IEvent {
- public:
- virtual void OnAppLaunched(const AppContext* context) = 0;
- virtual void OnAppTerminated(const AppContext* context) = 0;
- };
-
- AppEvent(std::string app_id, IEvent* ev);
- virtual ~AppEvent();
-
- private:
- static int OnAppStatusCb(aul_app_info* info, int ctx_status, void* data);
-
- private:
- std::string app_id_;
- IEvent* ev_;
- status_listen_h handle_ = nullptr;
-};
-
-} // namespace aul
-
-#endif // AUL_APP_EVENT_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 <tizen.h>
-
-#include "aul/app_manager.hh"
-#include "aul/client.hh"
-#include "aul/exception.hh"
-#include "aul/log_private.hh"
-#include "include/aul.h"
-#include "include/aul_cmd.h"
-#include "include/aul_error.h"
-#include "include/aul_sock.h"
-
-namespace aul {
-
-AppManager::AppManager() = default;
-
-AppManager::~AppManager() = default;
-
-AppManager::AppContextList AppManager::GetAppContexts() {
- AppContextList list;
- int ret = aul_app_get_running_app_info(AppInfoCb, &list);
- if (ret != AUL_R_OK)
- _E("Failed to get running app info. error(%d)", ret);
-
- return list;
-}
-
-AppManager::AppContextList AppManager::GetAllAppContexts() {
- AppContextList list;
- int ret = aul_app_get_all_running_app_info(AppInfoCb, &list);
- if (ret != AUL_R_OK)
- _E("Failed to get all running app info. error(%d)", ret);
-
- return list;
-}
-
-std::unique_ptr<AppContext> AppManager::GetAppContext(
- const std::string& app_id) {
- tizen_base::Bundle b;
- b.Add(AUL_K_APPID, app_id);
- Packet packet(APP_CONTEXT_GET, AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
- return GetAppContext(packet);
-}
-
-std::unique_ptr<AppContext> AppManager::GetAppContext(
- const std::string& app_id,
- const std::string& inst_id) {
- tizen_base::Bundle b;
- b.Add(AUL_K_APPID, app_id);
- b.Add(AUL_K_INSTANCE_ID, inst_id);
- Packet packet(APP_CONTEXT_GET_BY_INSTANCE_ID,
- AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
- return GetAppContext(packet);
-}
-
-std::unique_ptr<AppContext> AppManager::GetAppContext(int pid) {
- tizen_base::Bundle b;
- b.Add(AUL_K_PID, std::to_string(pid));
- Packet packet(APP_CONTEXT_GET_BY_PID, AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
- return GetAppContext(packet);
-}
-
-std::unique_ptr<AppContext> AppManager::GetAppContext(const Packet& packet) {
- Packet recv_pkt;
- try {
- Client client(PATH_AMD_SOCK);
- int ret = client.Send(packet);
- if (ret < 0) {
- set_last_result(aul_error_convert(ret));
- return {};
- }
-
- ret = client.Recv(recv_pkt);
- if (ret < 0) {
- set_last_result(aul_error_convert(ret));
- return {};
- }
- } catch (Exception& e) {
- _E("Failed to create client. error(%d)", e.GetErrorCode());
- set_last_result(aul_error_convert(e.GetErrorCode()));
- return {};
- }
-
- if (recv_pkt.GetCmd() != APP_GET_INFO_OK) {
- _E("Failed to find app context");
- set_last_result(aul_error_convert(recv_pkt.GetCmd()));
- return {};
- }
-
- set_last_result(AUL_R_OK);
- tizen_base::Bundle b = recv_pkt.DataToBundle();
- return std::unique_ptr<AppContext>(new (std::nothrow) AppContext(b));
-}
-
-int AppManager::AppInfoCb(const aul_app_info* info, void* data) {
- auto* list = static_cast<AppContextList*>(data);
- AppContext::Builder builder;
- builder.SetAppId(info->appid);
- builder.SetPkgId(info->pkgid);
- builder.SetInstId(info->instance_id);
- builder.SetExec(info->app_path);
- builder.SetPid(info->pid);
- builder.SetStatus(info->status);
- builder.SetIsSubApp(static_cast<bool>(info->is_sub_app));
- list->push_back(std::make_unique<AppContext>(builder.Build()));
- return 0;
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_APP_MANAGER_HH_
-#define AUL_APP_MANAGER_HH_
-
-#include <memory>
-#include <string>
-#include <list>
-
-#include "aul/app_context.hh"
-#include "aul/packet.hh"
-#include "include/aul.h"
-
-namespace aul {
-
-class AppManager {
- public:
- using AppContextList = std::list<std::unique_ptr<AppContext>>;
-
- AppManager();
- virtual ~AppManager();
-
- AppContextList GetAppContexts();
- AppContextList GetAllAppContexts();
- std::unique_ptr<AppContext> GetAppContext(const std::string& app_id);
- std::unique_ptr<AppContext> GetAppContext(const std::string& app_id,
- const std::string& inst_id);
- std::unique_ptr<AppContext> GetAppContext(int pid);
-
- private:
- std::unique_ptr<AppContext> GetAppContext(const Packet& packet);
-
- private:
- static int AppInfoCb(const aul_app_info* info, void* data);
-};
-
-} // namespace aul
-
-#endif // AUL_APP_MANAGER_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <memory>
+
+#include "aul/app_manager/app_context.hh"
+#include "include/aul.h"
+
+namespace aul {
+
+AppContext::Builder& AppContext::Builder::SetAppId(std::string app_id) {
+ app_id_ = std::move(app_id);
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetPkgId(std::string pkg_id) {
+ pkg_id_ = std::move(pkg_id);
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetInstId(std::string inst_id) {
+ inst_id_ = std::move(inst_id);
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetExec(std::string exec) {
+ exec_ = std::move(exec);
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetPid(int pid) {
+ pid_ = pid;
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetStatus(int status) {
+ status_ = status;
+ return *this;
+}
+
+AppContext::Builder& AppContext::Builder::SetIsSubApp(bool is_sub_app) {
+ is_sub_app_ = is_sub_app;
+ return *this;
+}
+
+AppContext AppContext::Builder::Build() {
+ return AppContext(std::move(app_id_), std::move(pkg_id_), std::move(inst_id_),
+ std::move(exec_), pid_, status_, is_sub_app_);
+}
+
+AppContext::AppContext(tizen_base::Bundle b) {
+ Set(std::move(b));
+}
+
+AppContext::AppContext(std::string app_id,
+ std::string pkg_id,
+ std::string inst_id,
+ std::string exec,
+ int pid,
+ int status,
+ bool is_sub_app)
+ : app_id_(std::move(app_id)),
+ pkg_id_(std::move(pkg_id)),
+ inst_id_(std::move(inst_id)),
+ exec_(std::move(exec)),
+ pid_(pid),
+ status_(status),
+ is_sub_app_(is_sub_app) {
+}
+
+AppContext::~AppContext() = default;
+
+const std::string& AppContext::GetAppId() const {
+ return app_id_;
+}
+
+const std::string& AppContext::GetPkgId() const {
+ return pkg_id_;
+}
+
+const std::string& AppContext::GetInstId() const {
+ return inst_id_;
+}
+
+const std::string& AppContext::GetExec() const {
+ return exec_;
+}
+
+int AppContext::GetPid() const {
+ return pid_;
+}
+
+int AppContext::GetStatus() const {
+ return status_;
+}
+
+void AppContext::SetStatus(int status) {
+ status_ = status;
+}
+
+bool AppContext::IsSubApp() const {
+ return is_sub_app_;
+}
+
+void AppContext::Set(tizen_base::Bundle b) {
+ app_id_ = std::move(b.GetString(AUL_K_APPID));
+ pkg_id_ = std::move(b.GetString(AUL_K_PKGID));
+ inst_id_ = std::move(b.GetString(AUL_K_INSTANCE_ID));
+ exec_ = std::move(b.GetString(AUL_K_EXEC));
+ pid_ = std::stoi(b.GetString(AUL_K_PID));
+ status_ = std::stoi(b.GetString(AUL_K_STATUS));
+ is_sub_app_ = static_cast<bool>(std::stoi(b.GetString(AUL_K_IS_SUBAPP)));
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_APP_MANAGER_APP_CONTEXT_HH_
+#define AUL_APP_MANAGER_APP_CONTEXT_HH_
+
+#include <bundle_cpp.h>
+
+#include <string>
+
+namespace aul {
+
+class AppContext {
+ public:
+ class Builder {
+ public:
+ Builder& SetAppId(std::string app_id);
+ Builder& SetPkgId(std::string pkg_id);
+ Builder& SetInstId(std::string inst_id);
+ Builder& SetExec(std::string exec);
+ Builder& SetPid(int pid);
+ Builder& SetStatus(int status);
+ Builder& SetIsSubApp(bool is_sub_app);
+ AppContext Build();
+
+ private:
+ std::string app_id_;
+ std::string pkg_id_;
+ std::string inst_id_;
+ std::string exec_;
+ int pid_;
+ int status_;
+ bool is_sub_app_;
+ };
+
+ AppContext(tizen_base::Bundle b);
+ virtual ~AppContext();
+
+ const std::string& GetAppId() const;
+ const std::string& GetPkgId() const;
+ const std::string& GetInstId() const;
+ const std::string& GetExec() const;
+ int GetPid() const;
+ int GetStatus() const;
+ bool IsSubApp() const;
+
+ void SetStatus(int status);
+
+ private:
+ AppContext(std::string app_id,
+ std::string pkg_id,
+ std::string inst_id,
+ std::string exec,
+ int pid,
+ int status,
+ bool is_sub_app);
+
+ void Set(tizen_base::Bundle b);
+
+ private:
+ std::string app_id_;
+ std::string pkg_id_;
+ std::string inst_id_;
+ std::string exec_;
+ int pid_;
+ int status_;
+ bool is_sub_app_;
+};
+
+} // namespace aul
+
+#endif // AUL_APP_MANAGER_APP_CONTEXT_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 "aul/app_manager/app_event.hh"
+#include "aul/common/exception.hh"
+#include "aul/common/log_private.hh"
+
+namespace aul {
+
+AppEvent::AppEvent(std::string app_id, AppEvent::IEvent* ev)
+ : app_id_(std::move(app_id)), ev_(ev) {
+ int ret = aul_listen_app_status(app_id_.c_str(), OnAppStatusCb,
+ this, &handle_);
+ if (ret != AUL_R_OK) {
+ _E("Failed to listen app status event. error(%d)", ret);
+ THROW(ret);
+ }
+}
+
+AppEvent::~AppEvent() {
+ if (handle_)
+ aul_ignore_app_status(handle_);
+}
+
+int AppEvent::OnAppStatusCb(aul_app_info* info, int ctx_status, void* data) {
+ AppContext::Builder builder;
+ builder.SetAppId(info->appid);
+ builder.SetPkgId(info->pkgid);
+ builder.SetInstId(info->instance_id);
+ builder.SetExec(info->app_path);
+ builder.SetPid(info->pid);
+ builder.SetStatus(info->status);
+ builder.SetIsSubApp(static_cast<bool>(info->is_sub_app));
+ AppContext app_context = builder.Build();
+
+ auto* handle = static_cast<AppEvent*>(data);
+ IEvent* ev = handle->ev_;
+ if (ctx_status == STATUS_TERMINATE)
+ ev->OnAppLaunched(&app_context);
+ else
+ ev->OnAppTerminated(&app_context);
+ return 0;
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_APP_MANAGER__APP_EVENT_HH_
+#define AUL_APP_MANAGER_APP_EVENT_HH_
+
+#include <string>
+
+#include "aul/app_manager/app_context.hh"
+#include "include/aul.h"
+
+namespace aul {
+
+class AppEvent {
+ public:
+ class IEvent {
+ public:
+ virtual void OnAppLaunched(const AppContext* context) = 0;
+ virtual void OnAppTerminated(const AppContext* context) = 0;
+ };
+
+ AppEvent(std::string app_id, IEvent* ev);
+ virtual ~AppEvent();
+
+ private:
+ static int OnAppStatusCb(aul_app_info* info, int ctx_status, void* data);
+
+ private:
+ std::string app_id_;
+ IEvent* ev_;
+ status_listen_h handle_ = nullptr;
+};
+
+} // namespace aul
+
+#endif // AUL_APP_MANAGER_APP_EVENT_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <tizen.h>
+
+#include "aul/app_manager/app_manager.hh"
+#include "aul/common/exception.hh"
+#include "aul/common/log_private.hh"
+#include "aul/socket/client.hh"
+#include "include/aul.h"
+#include "include/aul_cmd.h"
+#include "include/aul_error.h"
+#include "include/aul_sock.h"
+
+namespace aul {
+
+AppManager::AppManager() = default;
+
+AppManager::~AppManager() = default;
+
+AppManager::AppContextList AppManager::GetAppContexts() {
+ AppContextList list;
+ int ret = aul_app_get_running_app_info(AppInfoCb, &list);
+ if (ret != AUL_R_OK)
+ _E("Failed to get running app info. error(%d)", ret);
+
+ return list;
+}
+
+AppManager::AppContextList AppManager::GetAllAppContexts() {
+ AppContextList list;
+ int ret = aul_app_get_all_running_app_info(AppInfoCb, &list);
+ if (ret != AUL_R_OK)
+ _E("Failed to get all running app info. error(%d)", ret);
+
+ return list;
+}
+
+std::unique_ptr<AppContext> AppManager::GetAppContext(
+ const std::string& app_id) {
+ tizen_base::Bundle b;
+ b.Add(AUL_K_APPID, app_id);
+ Packet packet(APP_CONTEXT_GET, AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
+ return GetAppContext(packet);
+}
+
+std::unique_ptr<AppContext> AppManager::GetAppContext(
+ const std::string& app_id,
+ const std::string& inst_id) {
+ tizen_base::Bundle b;
+ b.Add(AUL_K_APPID, app_id);
+ b.Add(AUL_K_INSTANCE_ID, inst_id);
+ Packet packet(APP_CONTEXT_GET_BY_INSTANCE_ID,
+ AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
+ return GetAppContext(packet);
+}
+
+std::unique_ptr<AppContext> AppManager::GetAppContext(int pid) {
+ tizen_base::Bundle b;
+ b.Add(AUL_K_PID, std::to_string(pid));
+ Packet packet(APP_CONTEXT_GET_BY_PID, AUL_SOCK_ASYNC | AUL_SOCK_BUNDLE, b);
+ return GetAppContext(packet);
+}
+
+std::unique_ptr<AppContext> AppManager::GetAppContext(const Packet& packet) {
+ Packet recv_pkt;
+ try {
+ Client client(PATH_AMD_SOCK);
+ int ret = client.Send(packet);
+ if (ret < 0) {
+ set_last_result(aul_error_convert(ret));
+ return {};
+ }
+
+ ret = client.Recv(recv_pkt);
+ if (ret < 0) {
+ set_last_result(aul_error_convert(ret));
+ return {};
+ }
+ } catch (Exception& e) {
+ _E("Failed to create client. error(%d)", e.GetErrorCode());
+ set_last_result(aul_error_convert(e.GetErrorCode()));
+ return {};
+ }
+
+ if (recv_pkt.GetCmd() != APP_GET_INFO_OK) {
+ _E("Failed to find app context");
+ set_last_result(aul_error_convert(recv_pkt.GetCmd()));
+ return {};
+ }
+
+ set_last_result(AUL_R_OK);
+ tizen_base::Bundle b = recv_pkt.DataToBundle();
+ return std::unique_ptr<AppContext>(new (std::nothrow) AppContext(b));
+}
+
+int AppManager::AppInfoCb(const aul_app_info* info, void* data) {
+ auto* list = static_cast<AppContextList*>(data);
+ AppContext::Builder builder;
+ builder.SetAppId(info->appid);
+ builder.SetPkgId(info->pkgid);
+ builder.SetInstId(info->instance_id);
+ builder.SetExec(info->app_path);
+ builder.SetPid(info->pid);
+ builder.SetStatus(info->status);
+ builder.SetIsSubApp(static_cast<bool>(info->is_sub_app));
+ list->push_back(std::make_unique<AppContext>(builder.Build()));
+ return 0;
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_APP_MANAGER_APP_MANAGER_HH_
+#define AUL_APP_MANAGER_APP_MANAGER_HH_
+
+#include <memory>
+#include <string>
+#include <list>
+
+#include "aul/app_manager/app_context.hh"
+#include "aul/socket/packet.hh"
+#include "include/aul.h"
+
+namespace aul {
+
+class AppManager {
+ public:
+ using AppContextList = std::list<std::unique_ptr<AppContext>>;
+
+ AppManager();
+ virtual ~AppManager();
+
+ AppContextList GetAppContexts();
+ AppContextList GetAllAppContexts();
+ std::unique_ptr<AppContext> GetAppContext(const std::string& app_id);
+ std::unique_ptr<AppContext> GetAppContext(const std::string& app_id,
+ const std::string& inst_id);
+ std::unique_ptr<AppContext> GetAppContext(int pid);
+
+ private:
+ std::unique_ptr<AppContext> GetAppContext(const Packet& packet);
+
+ private:
+ static int AppInfoCb(const aul_app_info* info, void* data);
+};
+
+} // namespace aul
+
+#endif // AUL_APP_MANAGER_APP_MANAGER_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 <unistd.h>
-
-#include "aul/client.hh"
-#include "aul/exception.hh"
-#include "aul/log_private.hh"
-#include "aul/parcel.hh"
-
-namespace aul {
-
-Client::Client(std::string path) : Socket(std::move(path)) {
- int retry = 2;
- do {
- int ret = Connect();
- if (ret == 0) {
- break;
- } else if (ret < -1) {
- _E("Maybe peer not launched or peer dead. path(%s), fd(%d)",
- GetPath().c_str(), GetFd());
- usleep(100 * 1000);
- } else if (ret < 0) {
- _E("Failed to connect to socket(%s), fd(%d)",
- GetPath().c_str(), GetFd());
- THROW(-ECOMM);
- }
- } while (retry > 0);
-}
-
-int Client::Send(const Packet& packet) {
- _W("cmd(%d)", packet.GetCmd());
- auto raw = const_cast<Packet&>(packet).GetRaw();
- int ret = Socket::Send(reinterpret_cast<void*>(&raw[0]), raw.size());
- if (ret < 0)
- return ret;
-
- return 0;
-}
-
-int Client::Recv(Packet& packet) {
- int cmd = -1;
- void* p = reinterpret_cast<void*>(&cmd);
- int ret = Socket::Recv(p, sizeof(cmd));
- if (ret < 0)
- return ret;
-
- int size = -1;
- p = reinterpret_cast<void*>(&size);
- ret = Socket::Recv(p, sizeof(size));
- if (ret < 0)
- return ret;
-
- if (size < 0 || size > MAX_PAYLOAD_SIZE) {
- _E("Invalid size(%d)", size);
- return -ECOMM;
- }
-
- int opt = -1;
- p = reinterpret_cast<void*>(&opt);
- ret = Socket::Recv(p, sizeof(opt));
- if (ret < 0)
- return ret;
-
- unsigned char* buf = new (std::nothrow) unsigned char[size];
- if (buf == nullptr) {
- _E("Out of memory");
- return -ENOMEM;
- }
-
- ret = Socket::Recv(reinterpret_cast<void*>(buf), size);
- if (ret < 0) {
- delete[] buf;
- return ret;
- }
-
- std::vector<unsigned char> data(buf, buf + size);
- delete[] buf;
-
- packet.SetCmd(cmd);
- packet.SetOpt(opt);
- packet.SetData(data);
-
- return 0;
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_CLIENT_HH_
-#define AUL_CLIENT_HH_
-
-#include "aul/packet.hh"
-#include "aul/socket.hh"
-
-namespace aul {
-
-class Client : public Socket {
- public:
- Client(std::string path);
-
- int Send(const Packet& packet);
- int Recv(Packet& packet);
-};
-
-} // namespace aul
-
-#endif // AUL_CLIENT_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_COMMON_API_HH_
+#define AUL_COMMON_API_HH_
+
+#ifdef API
+#undef API
+#endif
+#define API __attribute__ ((visibility("default")))
+
+#endif // AUL_COMMON_API_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ *
+ * 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 AUL_COMMON_EXCEPTION_HH_
+#define AUL_COMMON_EXCEPTION_HH_
+
+#include <string>
+#include <exception>
+
+#include "aul/common/log_private.hh"
+
+#define THROW(error_code) throw Exception(error_code)
+
+namespace aul {
+
+class Exception : public std::exception {
+ public:
+ explicit Exception(int error_code, std::string file = __FILE__,
+ int line = __LINE__ ) {
+ error_code_ = error_code;
+ message_ = file.substr(file.find_last_of("/") + 1) + ":"
+ + std::to_string(line) + " code:" + std::to_string(error_code_);
+ _E("%s", message_.c_str());
+ }
+
+ virtual ~Exception() {}
+
+ virtual const char *what(void) const noexcept {
+ return message_.c_str();
+ }
+
+ int GetErrorCode() {
+ return error_code_;
+ }
+
+ private:
+ int error_code_;
+ std::string message_;
+};
+
+} // namespace aul
+
+#endif // AUL_COMMON_EXCEPTION_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_COMMON_LOG_PRIVATE_HH_
+#define AUL_COMMON_LOG_PRIVATE_HH_
+
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "AUL"
+
+#undef _E
+#define _E LOGE
+
+#undef _W
+#define _W LOGW
+
+#undef _I
+#define _I LOGI
+
+#undef _D
+#define _D LOGD
+
+#endif // AUL_COMMON_LOG_PRIVATE_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
- *
- * 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 AUL_EXCEPTION_HH_
-#define AUL_EXCEPTION_HH_
-
-#include <string>
-#include <exception>
-
-#include "aul/log_private.hh"
-
-#define THROW(error_code) throw Exception(error_code)
-
-namespace aul {
-
-class Exception : public std::exception {
- public:
- explicit Exception(int error_code, std::string file = __FILE__,
- int line = __LINE__ ) {
- error_code_ = error_code;
- message_ = file.substr(file.find_last_of("/") + 1) + ":"
- + std::to_string(line) + " code:" + std::to_string(error_code_);
- _E("%s", message_.c_str());
- }
-
- virtual ~Exception() {}
-
- virtual const char *what(void) const noexcept {
- return message_.c_str();
- }
-
- int GetErrorCode() {
- return error_code_;
- }
-
- private:
- int error_code_;
- std::string message_;
-};
-
-} // namespace aul
-
-#endif // AUL_EXCEPTION_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_LOG_PRIVATE_HH_
-#define AUL_LOG_PRIVATE_HH_
-
-#include <dlog.h>
-
-#undef LOG_TAG
-#define LOG_TAG "AUL"
-
-#undef _E
-#define _E LOGE
-
-#undef _W
-#define _W LOGW
-
-#undef _I
-#define _I LOGI
-
-#undef _D
-#define _D LOGD
-
-#endif // AUL_LOG_PRIVATE_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 <memory>
-
-#include "aul/packet.hh"
-#include "aul/parcel.hh"
-
-namespace aul {
-
-Packet::Packet() : cmd_(0), opt_(0) {
-}
-
-Packet::Packet(int cmd, int opt, std::vector<unsigned char> data)
- : cmd_(cmd), opt_(opt), data_(std::move(data)) {
-}
-
-Packet::Packet(int cmd, int opt, tizen_base::Bundle data)
- : cmd_(cmd), opt_(opt) {
- auto raw = data.ToRaw();
- auto* raw_ptr = reinterpret_cast<unsigned char*>(raw.first.get());
- std::copy(raw_ptr, raw_ptr + raw.second, std::back_inserter(data_));
-}
-
-Packet::Packet(const unsigned char* buf, unsigned int size) {
- Parcel parcel(buf, size);
- cmd_ = parcel.ReadInt32();
- int len = parcel.ReadInt32();
- opt_ = parcel.ReadInt32();
- auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
- parcel.Read(p, len);
-}
-
-Packet::~Packet() = default;
-
-int Packet::GetCmd() const {
- return cmd_;
-}
-
-int Packet::GetOpt() const {
- return opt_;
-}
-
-const std::vector<unsigned char>& Packet::GetData() {
- return data_;
-}
-
-void Packet::SetCmd(int cmd) {
- cmd_ = cmd;
-}
-
-void Packet::SetOpt(int opt) {
- opt_ = opt;
-}
-
-void Packet::SetData(std::vector<unsigned char> data) {
- data_ = std::move(data);
-}
-
-tizen_base::Bundle Packet::DataToBundle() {
- const bundle_raw* raw = reinterpret_cast<const bundle_raw*>(&data_[0]);
- bundle* b = bundle_decode(raw, data_.size());
- return tizen_base::Bundle(b, false, true);
-}
-
-const std::vector<unsigned char>& Packet::GetRaw() {
- if (parcel_.GetRaw().size() == 0) {
- parcel_.WriteInt32(cmd_);
- parcel_.WriteInt32(data_.size());
- parcel_.WriteInt32(opt_);
- auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
- parcel_.Write(p, data_.size());
- }
- return parcel_.GetRaw();
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_PACKET_HH_
-#define AUL_PACKET_HH_
-
-#include <bundle_cpp.h>
-
-#include <vector>
-
-#include "aul/parcel.hh"
-
-namespace aul {
-
-class Packet {
- public:
- Packet();
- Packet(int cmd, int opt, std::vector<unsigned char> data);
- Packet(int cmd, int opt, tizen_base::Bundle data);
- Packet(const unsigned char* buf, unsigned int size);
- virtual ~Packet();
-
- int GetCmd() const;
- int GetOpt() const;
- const std::vector<unsigned char>& GetData();
-
- void SetCmd(int cmd);
- void SetOpt(int opt);
- void SetData(std::vector<unsigned char> data);
-
- tizen_base::Bundle DataToBundle();
-
- const std::vector<unsigned char>& GetRaw();
-
- private:
- int cmd_;
- int opt_;
- std::vector<unsigned char> data_;
- Parcel parcel_;
-};
-
-} // namespace aul
-
-#endif // AUL_PACKET_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 "aul/parcel.hh"
-
-namespace aul {
-
-Parcel::Parcel(const unsigned char* buf, unsigned int size)
- : data_(buf, buf + size) {
-}
-
-void Parcel::Write(const unsigned char* buf, unsigned int size) {
- std::copy(buf, buf + size, std::back_inserter(data_));
-}
-
-void Parcel::Read(unsigned char* buf, unsigned int size) {
- if (reader_ + size > data_.size())
- return;
-
- std::copy(&data_[reader_], &data_[reader_] + size, buf);
- reader_ += size;
-}
-
-void Parcel::WriteInt32(int i) {
- Write<int>(i);
-}
-
-int Parcel::ReadInt32() {
- return Read<int>();
-}
-
-const std::vector<unsigned char>& Parcel::GetRaw() {
- return data_;
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_PARCEL_HH_
-#define AUL_PARCEL_HH_
-
-#include <vector>
-
-namespace aul {
-
-class Parcel {
- public:
- Parcel() = default;
- Parcel(const unsigned char* buf, unsigned int size);
-
- void Write(const unsigned char* buf, unsigned int size);
- void Read(unsigned char* buf, unsigned int size);
- void WriteInt32(int i);
- int ReadInt32();
- const std::vector<unsigned char>& GetRaw();
-
- private:
- template<typename T>
- void Write(T d) {
- unsigned char* p = reinterpret_cast<unsigned char*>(&d);
- std::copy(p, p + sizeof(T), std::back_inserter(data_));
- }
-
- template<typename T>
- T Read() {
- T d = 0;
- unsigned char* p = reinterpret_cast<unsigned char*>(&d);
- if (reader_ + sizeof(T) > data_.size())
- return d;
-
- std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p);
- reader_ += sizeof(T);
- return d;
- }
-
- private:
- std::vector<unsigned char> data_;
- unsigned int reader_ = 0;
-};
-
-} // namespace aul
-
-#endif // AUL_PARCEL_HH_
+++ /dev/null
-/*
- * Copyright (c) 2020 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 <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "aul/log_private.hh"
-#include "aul/socket.hh"
-
-namespace aul {
-
-Socket::Socket(std::string path, bool is_client)
- : path_(std::move(path)), fd_(0), is_client_(is_client) {
- Create();
-}
-
-Socket::Socket(int fd, bool is_client)
- : fd_(fd), is_client_(is_client) {
-}
-
-Socket::~Socket() {
- if (fd_ > 0)
- close(fd_);
-}
-
-int Socket::Send(const void* buf, unsigned int size) {
- const unsigned char* buffer = static_cast<const unsigned char*>(buf);
- unsigned int left = size;
- while (left) {
- ssize_t nb = send(fd_, buffer, left, MSG_NOSIGNAL);
- if (nb < 0) {
- _E("send() is failed. fd(%d), errno(%d)", fd_, errno);
- return -ECOMM;
- }
-
- left -= nb;
- buffer += nb;
- }
-
- return 0;
-}
-
-int Socket::Recv(void* buf, unsigned int size) {
- bool is_blocking = true;
- if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
- is_blocking = false;
-
- unsigned char* buffer = static_cast<unsigned char*>(buf);
- unsigned int left = size;
- while (left) {
- ssize_t nb = recv(fd_, buffer, left, 0);
- if (nb == 0) {
- _W("Socket was disconnected. fd(%d)", fd_);
- return -ECOMM;
- } else if (nb < 0) {
- if (errno == EINTR) {
- continue;
- } else if (errno == EAGAIN) {
- if (is_blocking) {
- _E("Timed out. fd(%d)", fd_);
- return -EAGAIN;
- }
-
- continue;
- }
-
- _E("recv() is failed. fd(%d), errno(%d)", fd_, errno);
- return -ECOMM;
- }
-
- left -= nb;
- buffer += nb;
- }
-
- return 0;
-}
-
-int Socket::Listen() {
- int ret = bind(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
- sizeof(addr_));
- if (ret < 0) {
- _E("bind() is failed. fd(%d), errno(%d)", fd_, errno);
- return -1;
- }
-
- SetOption();
-
- ret = listen(fd_, 128);
- if (ret < 0) {
- _E("listen() is failed. fd(%d), errno(%d)", fd_, errno);
- return -1;
- }
-
- return 0;
-}
-
-int Socket::Connect() {
- int flags = fcntl(fd_, F_GETFL, 0);
- fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
-
- int ret = connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
- sizeof(addr_));
- fcntl(fd_, F_SETFL, flags);
- if (ret < 0) {
- if (errno != EAGAIN && errno != EINPROGRESS)
- return -2;
- } else if (ret == 0) {
- SetOption();
- return 0;
- }
-
- fd_set readfds;
- FD_ZERO(&readfds);
- FD_SET(fd_, &readfds);
- fd_set writefds = readfds;
- struct timeval timeout = { 0, 100 * 1000 };
- ret = select(fd_ + 1, &readfds, &writefds, NULL, &timeout);
- if (ret == 0) {
- errno = ETIMEDOUT;
- return -1;
- }
-
- if (FD_ISSET(fd_, &readfds) || FD_ISSET(fd_, &writefds)) {
- int error = 0;
- socklen_t len = sizeof(error);
- if (getsockopt(fd_, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
- return -1;
- }
-
- return -1;
-}
-
-int Socket::GetFd() {
- return fd_;
-}
-
-std::string Socket::GetPath() {
- return path_;
-}
-
-void Socket::SetOption() {
- int size = MAX_AUL_BUFF_SIZE;
- int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
- if (ret < 0) {
- _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
- return;
- }
-
- ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
- if (ret < 0) {
- _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
- return;
- }
-
- if (is_client_) {
- struct timeval timeout = { 5, 200 * 1000 };
- ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
- if (ret < 0)
- _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
- }
-}
-
-void Socket::Create() {
- fd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (fd_ < 0) {
- _E("socket() is failed. errno(%d)", errno);
- return;
- }
-
- addr_.sun_family = AF_UNIX;
- snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
-}
-
-} // namespace aul
+++ /dev/null
-/*
- * Copyright (c) 2020 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 AUL_SOCKET_HH_
-#define AUL_SOCKET_HH_
-
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <sys/un.h>
-
-#include <string>
-
-namespace aul {
-
-static const char PATH_AMD_SOCK[] = "/run/aul/daemons/.amd-sock";
-static const int MAX_PAYLOAD_SIZE = 1024 * 1024 * 1;
-static const int MAX_AUL_BUFF_SIZE = 131071;
-
-class Socket {
- public:
- Socket(std::string path, bool is_client = true);
- Socket(int fd, bool is_client = true);
- virtual ~Socket();
-
- int Send(const void* buf, unsigned int len);
- int Recv(void* buf, unsigned int len);
- int Listen();
- int Connect();
-
- int GetFd();
- std::string GetPath();
-
- private:
- void SetOption();
- void Create();
-
- private:
- std::string path_;
- int fd_;
- bool is_client_;
- struct sockaddr_un addr_ = { 0, };
-};
-
-} // namespace aul
-
-#endif // AUL_SOCKET_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <unistd.h>
+
+#include "aul/common/exception.hh"
+#include "aul/common/log_private.hh"
+#include "aul/socket/client.hh"
+#include "aul/socket/parcel.hh"
+
+namespace aul {
+
+Client::Client(std::string path) : Socket(std::move(path)) {
+ int retry = 2;
+ do {
+ int ret = Connect();
+ if (ret == 0) {
+ break;
+ } else if (ret < -1) {
+ _E("Maybe peer not launched or peer dead. path(%s), fd(%d)",
+ GetPath().c_str(), GetFd());
+ usleep(100 * 1000);
+ } else if (ret < 0) {
+ _E("Failed to connect to socket(%s), fd(%d)",
+ GetPath().c_str(), GetFd());
+ THROW(-ECOMM);
+ }
+ } while (retry > 0);
+}
+
+int Client::Send(const Packet& packet) {
+ _W("cmd(%d)", packet.GetCmd());
+ auto raw = const_cast<Packet&>(packet).GetRaw();
+ int ret = Socket::Send(reinterpret_cast<void*>(&raw[0]), raw.size());
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+int Client::Recv(Packet& packet) {
+ int cmd = -1;
+ void* p = reinterpret_cast<void*>(&cmd);
+ int ret = Socket::Recv(p, sizeof(cmd));
+ if (ret < 0)
+ return ret;
+
+ int size = -1;
+ p = reinterpret_cast<void*>(&size);
+ ret = Socket::Recv(p, sizeof(size));
+ if (ret < 0)
+ return ret;
+
+ if (size < 0 || size > MAX_PAYLOAD_SIZE) {
+ _E("Invalid size(%d)", size);
+ return -ECOMM;
+ }
+
+ int opt = -1;
+ p = reinterpret_cast<void*>(&opt);
+ ret = Socket::Recv(p, sizeof(opt));
+ if (ret < 0)
+ return ret;
+
+ unsigned char* buf = new (std::nothrow) unsigned char[size];
+ if (buf == nullptr) {
+ _E("Out of memory");
+ return -ENOMEM;
+ }
+
+ ret = Socket::Recv(reinterpret_cast<void*>(buf), size);
+ if (ret < 0) {
+ delete[] buf;
+ return ret;
+ }
+
+ std::vector<unsigned char> data(buf, buf + size);
+ delete[] buf;
+
+ packet.SetCmd(cmd);
+ packet.SetOpt(opt);
+ packet.SetData(data);
+
+ return 0;
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_SOCKET_CLIENT_HH_
+#define AUL_SOCKET_CLIENT_HH_
+
+#include "aul/socket/packet.hh"
+#include "aul/socket/socket.hh"
+
+namespace aul {
+
+class Client : public Socket {
+ public:
+ Client(std::string path);
+
+ int Send(const Packet& packet);
+ int Recv(Packet& packet);
+};
+
+} // namespace aul
+
+#endif // AUL_SOCKET_CLIENT_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <memory>
+
+#include "aul/socket/packet.hh"
+#include "aul/socket/parcel.hh"
+
+namespace aul {
+
+Packet::Packet() : cmd_(0), opt_(0) {
+}
+
+Packet::Packet(int cmd, int opt, std::vector<unsigned char> data)
+ : cmd_(cmd), opt_(opt), data_(std::move(data)) {
+}
+
+Packet::Packet(int cmd, int opt, tizen_base::Bundle data)
+ : cmd_(cmd), opt_(opt) {
+ auto raw = data.ToRaw();
+ auto* raw_ptr = reinterpret_cast<unsigned char*>(raw.first.get());
+ std::copy(raw_ptr, raw_ptr + raw.second, std::back_inserter(data_));
+}
+
+Packet::Packet(const unsigned char* buf, unsigned int size) {
+ Parcel parcel(buf, size);
+ cmd_ = parcel.ReadInt32();
+ int len = parcel.ReadInt32();
+ opt_ = parcel.ReadInt32();
+ auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
+ parcel.Read(p, len);
+}
+
+Packet::~Packet() = default;
+
+int Packet::GetCmd() const {
+ return cmd_;
+}
+
+int Packet::GetOpt() const {
+ return opt_;
+}
+
+const std::vector<unsigned char>& Packet::GetData() {
+ return data_;
+}
+
+void Packet::SetCmd(int cmd) {
+ cmd_ = cmd;
+}
+
+void Packet::SetOpt(int opt) {
+ opt_ = opt;
+}
+
+void Packet::SetData(std::vector<unsigned char> data) {
+ data_ = std::move(data);
+}
+
+tizen_base::Bundle Packet::DataToBundle() {
+ const bundle_raw* raw = reinterpret_cast<const bundle_raw*>(&data_[0]);
+ bundle* b = bundle_decode(raw, data_.size());
+ return tizen_base::Bundle(b, false, true);
+}
+
+const std::vector<unsigned char>& Packet::GetRaw() {
+ if (parcel_.GetRaw().size() == 0) {
+ parcel_.WriteInt32(cmd_);
+ parcel_.WriteInt32(data_.size());
+ parcel_.WriteInt32(opt_);
+ auto* p = reinterpret_cast<unsigned char*>(&data_[0]);
+ parcel_.Write(p, data_.size());
+ }
+ return parcel_.GetRaw();
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_SOCKET_PACKET_HH_
+#define AUL_SOCKET_PACKET_HH_
+
+#include <bundle_cpp.h>
+
+#include <vector>
+
+#include "aul/socket/parcel.hh"
+
+namespace aul {
+
+class Packet {
+ public:
+ Packet();
+ Packet(int cmd, int opt, std::vector<unsigned char> data);
+ Packet(int cmd, int opt, tizen_base::Bundle data);
+ Packet(const unsigned char* buf, unsigned int size);
+ virtual ~Packet();
+
+ int GetCmd() const;
+ int GetOpt() const;
+ const std::vector<unsigned char>& GetData();
+
+ void SetCmd(int cmd);
+ void SetOpt(int opt);
+ void SetData(std::vector<unsigned char> data);
+
+ tizen_base::Bundle DataToBundle();
+
+ const std::vector<unsigned char>& GetRaw();
+
+ private:
+ int cmd_;
+ int opt_;
+ std::vector<unsigned char> data_;
+ Parcel parcel_;
+};
+
+} // namespace aul
+
+#endif // AUL_SOCKET_PACKET_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 "aul/socket/parcel.hh"
+
+namespace aul {
+
+Parcel::Parcel(const unsigned char* buf, unsigned int size)
+ : data_(buf, buf + size) {
+}
+
+void Parcel::Write(const unsigned char* buf, unsigned int size) {
+ std::copy(buf, buf + size, std::back_inserter(data_));
+}
+
+void Parcel::Read(unsigned char* buf, unsigned int size) {
+ if (reader_ + size > data_.size())
+ return;
+
+ std::copy(&data_[reader_], &data_[reader_] + size, buf);
+ reader_ += size;
+}
+
+void Parcel::WriteInt32(int i) {
+ Write<int>(i);
+}
+
+int Parcel::ReadInt32() {
+ return Read<int>();
+}
+
+const std::vector<unsigned char>& Parcel::GetRaw() {
+ return data_;
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_SOCKET_PARCEL_HH_
+#define AUL_SOCKET_PARCEL_HH_
+
+#include <vector>
+
+namespace aul {
+
+class Parcel {
+ public:
+ Parcel() = default;
+ Parcel(const unsigned char* buf, unsigned int size);
+
+ void Write(const unsigned char* buf, unsigned int size);
+ void Read(unsigned char* buf, unsigned int size);
+ void WriteInt32(int i);
+ int ReadInt32();
+ const std::vector<unsigned char>& GetRaw();
+
+ private:
+ template<typename T>
+ void Write(T d) {
+ unsigned char* p = reinterpret_cast<unsigned char*>(&d);
+ std::copy(p, p + sizeof(T), std::back_inserter(data_));
+ }
+
+ template<typename T>
+ T Read() {
+ T d = 0;
+ unsigned char* p = reinterpret_cast<unsigned char*>(&d);
+ if (reader_ + sizeof(T) > data_.size())
+ return d;
+
+ std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p);
+ reader_ += sizeof(T);
+ return d;
+ }
+
+ private:
+ std::vector<unsigned char> data_;
+ unsigned int reader_ = 0;
+};
+
+} // namespace aul
+
+#endif // AUL_SOCKET_PARCEL_HH_
--- /dev/null
+/*
+ * Copyright (c) 2020 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 <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "aul/common/log_private.hh"
+#include "aul/socket/socket.hh"
+
+namespace aul {
+
+Socket::Socket(std::string path, bool is_client)
+ : path_(std::move(path)), fd_(0), is_client_(is_client) {
+ Create();
+}
+
+Socket::Socket(int fd, bool is_client)
+ : fd_(fd), is_client_(is_client) {
+}
+
+Socket::~Socket() {
+ if (fd_ > 0)
+ close(fd_);
+}
+
+int Socket::Send(const void* buf, unsigned int size) {
+ const unsigned char* buffer = static_cast<const unsigned char*>(buf);
+ unsigned int left = size;
+ while (left) {
+ ssize_t nb = send(fd_, buffer, left, MSG_NOSIGNAL);
+ if (nb < 0) {
+ _E("send() is failed. fd(%d), errno(%d)", fd_, errno);
+ return -ECOMM;
+ }
+
+ left -= nb;
+ buffer += nb;
+ }
+
+ return 0;
+}
+
+int Socket::Recv(void* buf, unsigned int size) {
+ bool is_blocking = true;
+ if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
+ is_blocking = false;
+
+ unsigned char* buffer = static_cast<unsigned char*>(buf);
+ unsigned int left = size;
+ while (left) {
+ ssize_t nb = recv(fd_, buffer, left, 0);
+ if (nb == 0) {
+ _W("Socket was disconnected. fd(%d)", fd_);
+ return -ECOMM;
+ } else if (nb < 0) {
+ if (errno == EINTR) {
+ continue;
+ } else if (errno == EAGAIN) {
+ if (is_blocking) {
+ _E("Timed out. fd(%d)", fd_);
+ return -EAGAIN;
+ }
+
+ continue;
+ }
+
+ _E("recv() is failed. fd(%d), errno(%d)", fd_, errno);
+ return -ECOMM;
+ }
+
+ left -= nb;
+ buffer += nb;
+ }
+
+ return 0;
+}
+
+int Socket::Listen() {
+ int ret = bind(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
+ sizeof(addr_));
+ if (ret < 0) {
+ _E("bind() is failed. fd(%d), errno(%d)", fd_, errno);
+ return -1;
+ }
+
+ SetOption();
+
+ ret = listen(fd_, 128);
+ if (ret < 0) {
+ _E("listen() is failed. fd(%d), errno(%d)", fd_, errno);
+ return -1;
+ }
+
+ return 0;
+}
+
+int Socket::Connect() {
+ int flags = fcntl(fd_, F_GETFL, 0);
+ fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
+
+ int ret = connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
+ sizeof(addr_));
+ fcntl(fd_, F_SETFL, flags);
+ if (ret < 0) {
+ if (errno != EAGAIN && errno != EINPROGRESS)
+ return -2;
+ } else if (ret == 0) {
+ SetOption();
+ return 0;
+ }
+
+ fd_set readfds;
+ FD_ZERO(&readfds);
+ FD_SET(fd_, &readfds);
+ fd_set writefds = readfds;
+ struct timeval timeout = { 0, 100 * 1000 };
+ ret = select(fd_ + 1, &readfds, &writefds, NULL, &timeout);
+ if (ret == 0) {
+ errno = ETIMEDOUT;
+ return -1;
+ }
+
+ if (FD_ISSET(fd_, &readfds) || FD_ISSET(fd_, &writefds)) {
+ int error = 0;
+ socklen_t len = sizeof(error);
+ if (getsockopt(fd_, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
+ return -1;
+ }
+
+ return -1;
+}
+
+int Socket::GetFd() {
+ return fd_;
+}
+
+std::string Socket::GetPath() {
+ return path_;
+}
+
+void Socket::SetOption() {
+ int size = MAX_AUL_BUFF_SIZE;
+ int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
+ if (ret < 0) {
+ _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ return;
+ }
+
+ ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
+ if (ret < 0) {
+ _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ return;
+ }
+
+ if (is_client_) {
+ struct timeval timeout = { 5, 200 * 1000 };
+ ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
+ if (ret < 0)
+ _E("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ }
+}
+
+void Socket::Create() {
+ fd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (fd_ < 0) {
+ _E("socket() is failed. errno(%d)", errno);
+ return;
+ }
+
+ addr_.sun_family = AF_UNIX;
+ snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
+}
+
+} // namespace aul
--- /dev/null
+/*
+ * Copyright (c) 2020 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 AUL_SOCKET_SOCKET_HH_
+#define AUL_SOCKET_SOCKET_HH_
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+
+#include <string>
+
+namespace aul {
+
+static const char PATH_AMD_SOCK[] = "/run/aul/daemons/.amd-sock";
+static const int MAX_PAYLOAD_SIZE = 1024 * 1024 * 1;
+static const int MAX_AUL_BUFF_SIZE = 131071;
+
+class Socket {
+ public:
+ Socket(std::string path, bool is_client = true);
+ Socket(int fd, bool is_client = true);
+ virtual ~Socket();
+
+ int Send(const void* buf, unsigned int len);
+ int Recv(void* buf, unsigned int len);
+ int Listen();
+ int Connect();
+
+ int GetFd();
+ std::string GetPath();
+
+ private:
+ void SetOption();
+ void Create();
+
+ private:
+ std::string path_;
+ int fd_;
+ bool is_client_;
+ struct sockaddr_un addr_ = { 0, };
+};
+
+} // namespace aul
+
+#endif // AUL_SOCKET_SOCKET_HH_