Move files to separated directories 95/236295/1
authorHwankyu Jhun <h.jhun@samsung.com>
Tue, 16 Jun 2020 06:04:25 +0000 (15:04 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Tue, 16 Jun 2020 06:04:25 +0000 (15:04 +0900)
Creates directories:
 - aul/app_manager
 - aul/common
 - aul/socket

Change-Id: I86a89dccad8bddf0c13082467e5f5c52eec6371f
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
38 files changed:
CMakeLists.txt
aul/api.hh [deleted file]
aul/api/aul_app_context.cc
aul/api/aul_app_event.cc
aul/api/aul_app_manager.cc
aul/app_context.cc [deleted file]
aul/app_context.hh [deleted file]
aul/app_event.cc [deleted file]
aul/app_event.hh [deleted file]
aul/app_manager.cc [deleted file]
aul/app_manager.hh [deleted file]
aul/app_manager/app_context.cc [new file with mode: 0644]
aul/app_manager/app_context.hh [new file with mode: 0644]
aul/app_manager/app_event.cc [new file with mode: 0644]
aul/app_manager/app_event.hh [new file with mode: 0644]
aul/app_manager/app_manager.cc [new file with mode: 0644]
aul/app_manager/app_manager.hh [new file with mode: 0644]
aul/client.cc [deleted file]
aul/client.hh [deleted file]
aul/common/api.hh [new file with mode: 0644]
aul/common/exception.hh [new file with mode: 0644]
aul/common/log_private.hh [new file with mode: 0644]
aul/exception.hh [deleted file]
aul/log_private.hh [deleted file]
aul/packet.cc [deleted file]
aul/packet.hh [deleted file]
aul/parcel.cc [deleted file]
aul/parcel.hh [deleted file]
aul/socket.cc [deleted file]
aul/socket.hh [deleted file]
aul/socket/client.cc [new file with mode: 0644]
aul/socket/client.hh [new file with mode: 0644]
aul/socket/packet.cc [new file with mode: 0644]
aul/socket/packet.hh [new file with mode: 0644]
aul/socket/parcel.cc [new file with mode: 0644]
aul/socket/parcel.hh [new file with mode: 0644]
aul/socket/socket.cc [new file with mode: 0644]
aul/socket/socket.hh [new file with mode: 0644]

index bde5d3e78b5a9085a193152f8789f053fe156464..0284f511ef2a42debcbb87fb3b8684deb6cb00e4 100644 (file)
@@ -45,6 +45,9 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/feature)
 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")
@@ -66,10 +69,16 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 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})
diff --git a/aul/api.hh b/aul/api.hh
deleted file mode 100644 (file)
index bb3d002..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * 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_
index 99788c6cb7af64205074b07e6eebde73ca339f6b..6238865c4a36f198cd153521a55dac858fd93e72 100644 (file)
 #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;
 
index 0f3f7b2b2c601c63138482d9430375de36f9e372..77fce7c17647da6442a0576ebed170506bac8a8c 100644 (file)
  */
 
 #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;
index c9387fbe711febd03a23321184ad7b3ea576805d..07b71ecd3f319a8e747a0aa62479daabd04b7faa 100644 (file)
 
 #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;
 
diff --git a/aul/app_context.cc b/aul/app_context.cc
deleted file mode 100644 (file)
index ab017ba..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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
diff --git a/aul/app_context.hh b/aul/app_context.hh
deleted file mode 100644 (file)
index 0b28e39..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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_
diff --git a/aul/app_event.cc b/aul/app_event.cc
deleted file mode 100644 (file)
index 1ae4a94..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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
diff --git a/aul/app_event.hh b/aul/app_event.hh
deleted file mode 100644 (file)
index f1cd75b..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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_
diff --git a/aul/app_manager.cc b/aul/app_manager.cc
deleted file mode 100644 (file)
index a883351..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * 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
diff --git a/aul/app_manager.hh b/aul/app_manager.hh
deleted file mode 100644 (file)
index 7eaba70..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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_
diff --git a/aul/app_manager/app_context.cc b/aul/app_manager/app_context.cc
new file mode 100644 (file)
index 0000000..7bd494f
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * 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
diff --git a/aul/app_manager/app_context.hh b/aul/app_manager/app_context.hh
new file mode 100644 (file)
index 0000000..d997b37
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * 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_
diff --git a/aul/app_manager/app_event.cc b/aul/app_manager/app_event.cc
new file mode 100644 (file)
index 0000000..d5c0eb7
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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
diff --git a/aul/app_manager/app_event.hh b/aul/app_manager/app_event.hh
new file mode 100644 (file)
index 0000000..cfc19ca
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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_
diff --git a/aul/app_manager/app_manager.cc b/aul/app_manager/app_manager.cc
new file mode 100644 (file)
index 0000000..2832999
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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
diff --git a/aul/app_manager/app_manager.hh b/aul/app_manager/app_manager.hh
new file mode 100644 (file)
index 0000000..136c849
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * 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_
diff --git a/aul/client.cc b/aul/client.cc
deleted file mode 100644 (file)
index 20a83bb..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * 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
diff --git a/aul/client.hh b/aul/client.hh
deleted file mode 100644 (file)
index 7bac6b9..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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_
diff --git a/aul/common/api.hh b/aul/common/api.hh
new file mode 100644 (file)
index 0000000..b763d59
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * 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_
diff --git a/aul/common/exception.hh b/aul/common/exception.hh
new file mode 100644 (file)
index 0000000..37f54c9
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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_
diff --git a/aul/common/log_private.hh b/aul/common/log_private.hh
new file mode 100644 (file)
index 0000000..ba6f565
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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_
diff --git a/aul/exception.hh b/aul/exception.hh
deleted file mode 100644 (file)
index 8f6b38d..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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_
diff --git a/aul/log_private.hh b/aul/log_private.hh
deleted file mode 100644 (file)
index eec2b9c..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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_
diff --git a/aul/packet.cc b/aul/packet.cc
deleted file mode 100644 (file)
index 7a7e331..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * 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
diff --git a/aul/packet.hh b/aul/packet.hh
deleted file mode 100644 (file)
index da68553..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * 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_
diff --git a/aul/parcel.cc b/aul/parcel.cc
deleted file mode 100644 (file)
index 02f0f7c..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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
diff --git a/aul/parcel.hh b/aul/parcel.hh
deleted file mode 100644 (file)
index 2a52fe0..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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_
diff --git a/aul/socket.cc b/aul/socket.cc
deleted file mode 100644 (file)
index a693cab..0000000
+++ /dev/null
@@ -1,188 +0,0 @@
-/*
- * 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
diff --git a/aul/socket.hh b/aul/socket.hh
deleted file mode 100644 (file)
index d774304..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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_
diff --git a/aul/socket/client.cc b/aul/socket/client.cc
new file mode 100644 (file)
index 0000000..4949dba
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * 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
diff --git a/aul/socket/client.hh b/aul/socket/client.hh
new file mode 100644 (file)
index 0000000..27a3eb4
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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_
diff --git a/aul/socket/packet.cc b/aul/socket/packet.cc
new file mode 100644 (file)
index 0000000..d312441
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * 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
diff --git a/aul/socket/packet.hh b/aul/socket/packet.hh
new file mode 100644 (file)
index 0000000..0cc3245
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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_
diff --git a/aul/socket/parcel.cc b/aul/socket/parcel.cc
new file mode 100644 (file)
index 0000000..a860339
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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
diff --git a/aul/socket/parcel.hh b/aul/socket/parcel.hh
new file mode 100644 (file)
index 0000000..3358353
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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_
diff --git a/aul/socket/socket.cc b/aul/socket/socket.cc
new file mode 100644 (file)
index 0000000..6307a2a
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * 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
diff --git a/aul/socket/socket.hh b/aul/socket/socket.hh
new file mode 100644 (file)
index 0000000..00d9e07
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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_