Add new functions for getting app context 23/235023/14
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 29 May 2020 02:25:34 +0000 (11:25 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 15 Jun 2020 05:34:28 +0000 (14:34 +0900)
Adds:
 - aul_app_manager_foreach_app_context()
 - aul_app_manager_foreach_all_app_context()
 - aul_app_context_create()
 - aul_app_context_create_with_inst_id()
 - aul_app_context_create_with_pid()
 - aul_app_context_destroy()
 - aul_app_context_clone()
 - aul_app_context_get_app_id()
 - aul_app_context_get_pkg_id()
 - aul_app_context_get_inst_id()
 - aul_app_context_get_app_path()
 - aul_app_context_get_pid()
 - aul_app_context_get_status()
 - aul_app_context_is_sub_app()
 - aul_app_context_is_running()
 - aul_app_context_is_equal()
 - aul_app_event_create()
 - aul_app_event_destroy()

Change-Id: I645f2211158c38758fa544003fb0722662b213ab
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
29 files changed:
CMakeLists.txt [changed mode: 0755->0644]
aul.pc.in
aul/api.hh [new file with mode: 0644]
aul/api/aul_app_context.cc [new file with mode: 0644]
aul/api/aul_app_context.h [new file with mode: 0644]
aul/api/aul_app_event.cc [new file with mode: 0644]
aul/api/aul_app_event.h [new file with mode: 0644]
aul/api/aul_app_manager.cc [new file with mode: 0644]
aul/api/aul_app_manager.h [new file with mode: 0644]
aul/app_context.cc [new file with mode: 0644]
aul/app_context.hh [new file with mode: 0644]
aul/app_event.cc [new file with mode: 0644]
aul/app_event.hh [new file with mode: 0644]
aul/app_manager.cc [new file with mode: 0644]
aul/app_manager.hh [new file with mode: 0644]
aul/client.cc [new file with mode: 0644]
aul/client.hh [new file with mode: 0644]
aul/exception.hh [new file with mode: 0644]
aul/log_private.hh [new file with mode: 0644]
aul/packet.cc [new file with mode: 0644]
aul/packet.hh [new file with mode: 0644]
aul/parcel.cc [new file with mode: 0644]
aul/parcel.hh [new file with mode: 0644]
aul/socket.cc [new file with mode: 0644]
aul/socket.hh [new file with mode: 0644]
include/aul_cmd.h
include/aul_sock.h
packaging/aul.spec
src/aul_cmd.c

old mode 100755 (executable)
new mode 100644 (file)
index 573ea5e..bde5d3e
@@ -1,12 +1,10 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
-PROJECT(aul C)
+PROJECT(aul C CXX)
 SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
 SET(EXEC_PREFIX "\${prefix}")
 SET(VERSION ${FULLVER})
 
-AUX_SOURCE_DIRECTORY(src SOURCES)
-
 ADD_DEFINITIONS("-DSHARE_PREFIX=\"${SHARE_INSTALL_PREFIX}/aul\"")
 
 # Set required packages
@@ -44,6 +42,10 @@ ENDFOREACH(APP_LAUNCHER_FLAGS)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/legacy)
 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)
+
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wl,-zdefs" )
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
 SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fpic")
@@ -51,12 +53,23 @@ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Werror")
 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
 SET(CMAKE_C_FLAGS_RELEASE "-O2")
+
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -fvisibility=hidden -std=c++14")
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
+
 SET(CMAKE_SKIP_BUILD_RPATH true)
 # Linker flags
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 # aul
-add_library(aul SHARED ${SOURCES})
+AUX_SOURCE_DIRECTORY(src SOURCES)
+AUX_SOURCE_DIRECTORY(aul AUL_SOURCES)
+AUX_SOURCE_DIRECTORY(aul/api AUL_API_SOURCES)
+add_library(aul SHARED
+       ${SOURCES}
+       ${AUL_SOURCES}
+       ${AUL_API_SOURCES})
 TARGET_LINK_LIBRARIES(aul ${libpkgs_LDFLAGS})
 SET_TARGET_PROPERTIES(aul PROPERTIES SOVERSION ${MAJORVER})
 SET_TARGET_PROPERTIES(aul PROPERTIES VERSION ${FULLVER})
@@ -100,6 +113,15 @@ FOREACH(hfile ${HEADERS_LIB_AUL})
        INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/${hfile} DESTINATION include/aul)
 ENDFOREACH(hfile)
 
+SET(HEADERS_LIB_AUL_API
+       aul_app_context.h
+       aul_app_manager.h
+       aul_app_event.h
+       )
+FOREACH(hfile ${HEADERS_LIB_AUL_API})
+       INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/aul/api/${hfile} DESTINATION include/aul/api)
+ENDFOREACH(hfile)
+
 INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/aul.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/feature/preexec_list.txt DESTINATION ${SHARE_INSTALL_PREFIX}/aul )
 INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data/miregex DESTINATION ${SHARE_INSTALL_PREFIX}/aul )
index 2794ab4..33206c8 100644 (file)
--- a/aul.pc.in
+++ b/aul.pc.in
@@ -10,4 +10,4 @@ Description: new application utility library
 Version: @VERSION@
 Requires: bundle glib-2.0 xdgmime vconf dlog
 Libs: -L${libdir} -laul
-Cflags: -I${includedir} -I${includedir}/aul
+Cflags: -I${includedir} -I${includedir}/aul -I${includedir}/aul/api
diff --git a/aul/api.hh b/aul/api.hh
new file mode 100644 (file)
index 0000000..bb3d002
--- /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_API_HH_
+#define AUL_API_HH_
+
+#ifdef API
+#undef API
+#endif
+#define API __attribute__ ((visibility("default")))
+
+#endif  // AUL_API_HH_
diff --git a/aul/api/aul_app_context.cc b/aul/api/aul_app_context.cc
new file mode 100644 (file)
index 0000000..99788c6
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * 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.h>
+#include <sys/types.h>
+#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"
+
+using namespace aul;
+
+extern "C" API int aul_app_context_create(const char *app_id,
+    aul_app_context_h* app_context) {
+  if (!app_id || !app_context) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  char caller_id[256] = { 0, };
+  int ret = aul_app_get_appid_bypid(getpid(), caller_id, sizeof(caller_id));
+  if (ret == AUL_R_OK) {
+    if (!strcmp(caller_id, app_id)) {
+      _W("Same Application(%s)", app_id);
+      return aul_app_context_create_with_pid(getpid(), app_context);
+    }
+  }
+
+  AppManager app_manager;
+  auto handle = app_manager.GetAppContext(app_id);
+  if (get_last_result() != AUL_R_OK) {
+    _E("Failed to get app context");
+    return get_last_result();
+  }
+
+  *app_context = handle.release();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_create_with_inst_id(
+    const char* app_id,
+    const char* inst_id,
+    aul_app_context_h* app_context) {
+  if (!app_id || !inst_id || !app_context) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppManager app_manager;
+  auto handle = app_manager.GetAppContext(app_id, inst_id);
+  if (get_last_result() != AUL_R_OK) {
+    _E("Failed to get app context");
+    return get_last_result();
+  }
+
+  *app_context = handle.release();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_create_with_pid(int pid,
+    aul_app_context_h* app_context) {
+  if (pid < 2 || !app_context) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppManager app_manager;
+  auto handle = app_manager.GetAppContext(pid);
+  if (get_last_result() != AUL_R_OK) {
+    _E("Failed to get app context");
+    return get_last_result();
+  }
+
+  *app_context = handle.release();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_destroy(aul_app_context_h app_context) {
+  if (!app_context) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  delete handle;
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_clone(aul_app_context_h app_context,
+    aul_app_context_h* clone) {
+  if (!app_context || !clone) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  AppContext::Builder builder;
+  builder.SetAppId(handle->GetAppId());
+  builder.SetPkgId(handle->GetPkgId());
+  builder.SetInstId(handle->GetInstId());
+  builder.SetExec(handle->GetExec());
+  builder.SetPid(handle->GetPid());
+  builder.SetStatus(handle->GetStatus());
+  builder.SetIsSubApp(handle->IsSubApp());
+
+  *clone = std::make_unique<AppContext>(builder.Build()).release();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_app_id(aul_app_context_h app_context,
+    char** app_id) {
+  if (!app_context || !app_id) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *app_id = strdup(handle->GetAppId().c_str());
+  if (*app_id == nullptr) {
+    _E("Failed to duplicate application ID");
+    return AUL_R_ENOMEM;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_pkg_id(aul_app_context_h app_context,
+    char** pkg_id) {
+  if (!app_context || !pkg_id) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *pkg_id = strdup(handle->GetPkgId().c_str());
+  if (*pkg_id == nullptr) {
+    _E("Failed to duplicate package ID");
+    return AUL_R_ENOMEM;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_inst_id(aul_app_context_h app_context,
+    char** inst_id) {
+  if (!app_context || !inst_id) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *inst_id = strdup(handle->GetInstId().c_str());
+  if (*inst_id == nullptr) {
+    _E("Failed to duplicate instance ID");
+    return AUL_R_ENOMEM;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_app_path(aul_app_context_h app_context,
+    char** app_path) {
+  if (!app_context || !app_path) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *app_path = strdup(handle->GetExec().c_str());
+  if (*app_path == nullptr) {
+    _E("Failed to duplicate app path");
+    return AUL_R_ENOMEM;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_pid(aul_app_context_h app_context,
+    int* pid) {
+  if (!app_context || !pid) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *pid = handle->GetPid();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_get_status(aul_app_context_h app_context,
+    int* status) {
+  if (!app_context || !status) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *status = handle->GetStatus();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_is_sub_app(aul_app_context_h app_context,
+    bool* is_sub_app) {
+  if (!app_context || !is_sub_app) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  *is_sub_app = handle->IsSubApp();
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_is_running(aul_app_context_h app_context,
+    bool* is_running) {
+  if (!app_context || !is_running) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppContext*>(app_context);
+  int ret = aul_app_is_running(handle->GetAppId().c_str());
+  if (ret == 1) {
+    *is_running = true;
+  } else {
+    char app_id[256] = { 0, };
+    int ret = aul_app_get_appid_bypid(handle->GetPid(), app_id, sizeof(app_id));
+    if (ret == AUL_R_OK)
+      *is_running = true;
+    else
+      *is_running = false;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_context_is_equal(aul_app_context_h lhs,
+    aul_app_context_h rhs,
+    bool* equal) {
+  if (!lhs || !rhs || !equal) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* lh = static_cast<AppContext*>(lhs);
+  auto* rh = static_cast<AppContext*>(rhs);
+  if (lh->GetAppId() == rh->GetAppId() && lh->GetPid() == rh->GetPid())
+    *equal = true;
+  else
+    *equal = false;
+
+  return AUL_R_OK;
+}
diff --git a/aul/api/aul_app_context.h b/aul/api/aul_app_context.h
new file mode 100644 (file)
index 0000000..383382a
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * 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_AUL_APP_CONTEXT_H_
+#define AUL_API_AUL_APP_CONTEXT_H_
+
+#include <aul.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void *aul_app_context_h;
+
+int aul_app_context_create(const char *app_id, aul_app_context_h *app_context);
+
+int aul_app_context_create_with_inst_id(const char *app_id, const char *inst_id,
+               aul_app_context_h *app_context);
+
+int aul_app_context_create_with_pid(int pid, aul_app_context_h *app_context);
+
+int aul_app_context_destroy(aul_app_context_h app_context);
+
+int aul_app_context_clone(aul_app_context_h app_context,
+               aul_app_context_h *clone);
+
+int aul_app_context_get_app_id(aul_app_context_h app_context, char **app_id);
+
+int aul_app_context_get_pkg_id(aul_app_context_h app_context, char **pkg_id);
+
+int aul_app_context_get_inst_id(aul_app_context_h app_context, char **inst_id);
+
+int aul_app_context_get_app_path(aul_app_context_h app_context,
+               char **app_path);
+
+int aul_app_context_get_pid(aul_app_context_h app_context, int *pid);
+
+int aul_app_context_get_status(aul_app_context_h app_context, int *status);
+
+int aul_app_context_is_sub_app(aul_app_context_h app_context, bool *is_sub_app);
+
+int aul_app_context_is_running(aul_app_context_h app_context, bool *is_running);
+
+int aul_app_context_is_equal(aul_app_context_h lhs, aul_app_context_h rhs,
+               bool *equal);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // AUL_API_AUL_APP_CONTEXT_HH_
diff --git a/aul/api/aul_app_event.cc b/aul/api/aul_app_event.cc
new file mode 100644 (file)
index 0000000..0f3f7b2
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * 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/api/aul_app_event.h"
+#include "aul/api.hh"
+#include "aul/app_event.hh"
+#include "aul/exception.hh"
+#include "aul/log_private.hh"
+
+namespace {
+using namespace aul;
+
+class AppEventStub : public AppEvent,
+                     public AppEvent::IEvent {
+ public:
+  AppEventStub(std::string app_id,
+      aul_app_event_launched_cb launched_cb,
+      aul_app_event_terminated_cb terminated_cb,
+      void* user_data)
+    : AppEvent(std::move(app_id), this),
+      launched_cb_(launched_cb),
+      terminated_cb_(terminated_cb),
+      user_data_(user_data) {
+  }
+
+ void OnAppLaunched(const AppContext* context) override {
+   AppContext* ctx = const_cast<AppContext*>(context);
+   launched_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
+ }
+
+ void OnAppTerminated(const AppContext* context) override {
+   AppContext* ctx = const_cast<AppContext*>(context);
+   terminated_cb_(static_cast<aul_app_context_h>(ctx), user_data_);
+ }
+
+ private:
+  aul_app_event_launched_cb launched_cb_;
+  aul_app_event_terminated_cb terminated_cb_;
+  void* user_data_;
+};
+
+}  // namespace
+
+extern "C" API int aul_app_event_create(const char *app_id,
+    aul_app_event_launched_cb launched_cb,
+    aul_app_event_terminated_cb terminated_cb,
+    void *user_data,
+    aul_app_event_h *app_event) {
+  if (!app_id || !launched_cb || !terminated_cb || !app_event) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppEventStub* handle = nullptr;
+  try {
+    handle = new AppEventStub(app_id, launched_cb, terminated_cb, user_data);
+  } catch (Exception& e) {
+    return e.GetErrorCode();
+  }
+
+  *app_event = static_cast<aul_app_event_h>(handle);
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_event_destroy(aul_app_event_h app_event) {
+  if (!app_event) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  auto* handle = static_cast<AppEventStub*>(app_event);
+  delete handle;
+
+  return AUL_R_OK;
+}
diff --git a/aul/api/aul_app_event.h b/aul/api/aul_app_event.h
new file mode 100644 (file)
index 0000000..8f71383
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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_AUL_APP_EVENT_H_
+#define AUL_API_AUL_APP_EVENT_H_
+
+#include <aul.h>
+#include <aul_app_context.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void *aul_app_event_h;
+
+typedef void (*aul_app_event_launched_cb)(aul_app_context_h app_context,
+               void *user_data);
+
+typedef void (*aul_app_event_terminated_cb)(aul_app_context_h app_context,
+               void *user_data);
+
+int aul_app_event_create(const char *app_id,
+               aul_app_event_launched_cb launched_cb,
+               aul_app_event_terminated_cb terminated_cb,
+               void *user_data,
+               aul_app_event_h *app_event);
+
+int aul_app_event_destroy(aul_app_event_h app_event);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // AUL_API_AUL_APP_EVENT_HH_
diff --git a/aul/api/aul_app_manager.cc b/aul/api/aul_app_manager.cc
new file mode 100644 (file)
index 0000000..c9387fb
--- /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 <tizen.h>
+
+#include "aul/api.hh"
+#include "aul/api/aul_app_manager.h"
+#include "aul/app_manager.hh"
+#include "aul/log_private.hh"
+
+using namespace aul;
+
+extern "C" API int aul_app_manager_foreach_app_context(
+    aul_app_manager_app_context_cb callback,
+    void* user_data) {
+  if (!callback) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppManager app_manager;
+  for (auto& i : app_manager.GetAppContexts()) {
+    if (!callback(static_cast<AppContext*>(i.get()), user_data))
+      break;
+  }
+
+  return AUL_R_OK;
+}
+
+extern "C" API int aul_app_manager_foreach_all_app_context(
+    aul_app_manager_app_context_cb callback,
+    void* user_data) {
+  if (!callback) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  AppManager app_manager;
+  for (auto& i : app_manager.GetAllAppContexts()) {
+    if (!callback(static_cast<AppContext*>(i.get()), user_data))
+      break;
+  }
+
+  return AUL_R_OK;
+}
diff --git a/aul/api/aul_app_manager.h b/aul/api/aul_app_manager.h
new file mode 100644 (file)
index 0000000..93ee368
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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_AUL_APP_MANAGER_H_
+#define AUL_API_AUL_APP_MANAGER_H_
+
+#include <stdbool.h>
+
+#include <aul.h>
+#include <aul_app_context.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef bool (*aul_app_manager_app_context_cb)(aul_app_context_h app_context,
+               void *user_data);
+
+int aul_app_manager_foreach_app_context(aul_app_manager_app_context_cb callback,
+               void *user_data);
+
+int aul_app_manager_foreach_all_app_context(
+               aul_app_manager_app_context_cb callback,
+               void *user_data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // AUL_API_AUL_APP_MANAGER_HH_
diff --git a/aul/app_context.cc b/aul/app_context.cc
new file mode 100644 (file)
index 0000000..ab017ba
--- /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 "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
new file mode 100644 (file)
index 0000000..0b28e39
--- /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_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
new file mode 100644 (file)
index 0000000..1ae4a94
--- /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_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
new file mode 100644 (file)
index 0000000..f1cd75b
--- /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_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
new file mode 100644 (file)
index 0000000..a883351
--- /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.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
new file mode 100644 (file)
index 0000000..7eaba70
--- /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_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/client.cc b/aul/client.cc
new file mode 100644 (file)
index 0000000..20a83bb
--- /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/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
new file mode 100644 (file)
index 0000000..7bac6b9
--- /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_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/exception.hh b/aul/exception.hh
new file mode 100644 (file)
index 0000000..8f6b38d
--- /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_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
new file mode 100644 (file)
index 0000000..eec2b9c
--- /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_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
new file mode 100644 (file)
index 0000000..7a7e331
--- /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/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
new file mode 100644 (file)
index 0000000..da68553
--- /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_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
new file mode 100644 (file)
index 0000000..02f0f7c
--- /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/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
new file mode 100644 (file)
index 0000000..2a52fe0
--- /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_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
new file mode 100644 (file)
index 0000000..a693cab
--- /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/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
new file mode 100644 (file)
index 0000000..697c5bf
--- /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_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, size_t len);
+  int Recv(void* buf, size_t 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_
index 2d7092a..37ed230 100755 (executable)
@@ -183,6 +183,9 @@ enum app_cmd {
        WIDGET_EVENT = 143,
        SET_PRIVATE_SHARING = 144,
        UNSET_PRIVATE_SHARING = 145,
+       APP_CONTEXT_GET = 146,
+       APP_CONTEXT_GET_BY_INSTANCE_ID = 147,
+       APP_CONTEXT_GET_BY_PID = 148,
 
        APP_CMD_MAX
 };
index 0649175..34235c4 100644 (file)
 
 #pragma once
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
+
 #include <unistd.h>
 #include <sys/socket.h>
 #include <sys/un.h>
index 7a1a602..77268a0 100755 (executable)
@@ -154,5 +154,6 @@ chsmack -a 'User::Home' %{TZ_SYS_DB}/.component.db-journal
 
 %files devel
 %{_includedir}/aul/*.h
+%{_includedir}/aul/api/*.h
 %{_libdir}/*.so
 %{_libdir}/pkgconfig/*.pc
index 0d4d0ed..6a58bfb 100755 (executable)
@@ -185,6 +185,9 @@ API const char *aul_cmd_convert_to_string(int cmd)
                "WIDGET_EVENT",
                "SET_PRIVATE_SHARING",
                "UNSET_PRIVATE_SHARING",
+               "APP_CONTEXT_GET",
+               "APP_CONTEXT_GET_BY_INSTANCE_ID",
+               "APP_CONTEXT_GET_BY_PID",
 
                "CUSTOM_COMMAND"
        };