Add client library
authorSangyoon Jang <jeremy.jang@samsung.com>
Mon, 28 May 2018 08:29:30 +0000 (17:29 +0900)
committer장상윤/Tizen Platform Lab(SR)/Engineer/삼성전자 <jeremy.jang@samsung.com>
Fri, 29 Jun 2018 09:56:02 +0000 (18:56 +0900)
Change-Id: I0708e669317dea2f396df29df0147ea79a423329
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
CMakeLists.txt
packaging/capmgr.spec
src/CMakeLists.txt
src/client/CMakeLists.txt [new file with mode: 0644]
src/client/client.cc [new file with mode: 0644]
src/client/include/capmgr.h [new file with mode: 0644]

index a73efa4a0dd592250f6e7d18f06a202290b3aa93..563b96553cc275635e3d04a356980bd9eb724561 100644 (file)
@@ -18,6 +18,7 @@ SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Mo
 ## Targets
 SET(TARGET_CAPMGR "capmgr")
 SET(TARGET_LIB_COMMON "capmgr-common")
+SET(TARGET_LIB_CLIENT "capmgr-client")
 
 INCLUDE(FindPkgConfig)
 INCLUDE(ApplyPkgConfig)
index 17bd10a9821c45465a64e39c97090b43353aa0ea..73ebeb68f4c82f3a70d4366629e17479c2c3b969 100644 (file)
@@ -70,6 +70,7 @@ systemctl daemon-reload
 %defattr(-,root,root,-)
 %{_bindir}/capmgr
 %{_bindir}/capmgr_test
+%{_libdir}/libcapmgr-client.so*
 %{_libdir}/libcapmgr-common.so*
 %{_sysconfdir}/dbus-1/system.d/%{name}.conf
 %{_unitdir}/%{name}.service
index 81e45ec83e7901ca66538de1efea0d97cb9f969c..d1ea9c2ea53aa6f8718b0632982a1f6c654d249d 100644 (file)
@@ -1,3 +1,4 @@
-ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(capmgr)
+ADD_SUBDIRECTORY(client)
+ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(unit_tests)
diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt
new file mode 100644 (file)
index 0000000..87f1b36
--- /dev/null
@@ -0,0 +1,16 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} CLIENT_SRCS)
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../)
+
+ADD_LIBRARY(${TARGET_LIB_CLIENT} SHARED
+  ${CLIENT_SRCS}
+)
+
+APPLY_PKG_CONFIG(${TARGET_LIB_CLIENT} PUBLIC
+  DLOG_DEPS
+)
+
+SET_TARGET_PROPERTIES(${TARGET_LIB_CLIENT} PROPERTIES VERSION ${FULLVER})
+SET_TARGET_PROPERTIES(${TARGET_LIB_CLIENT} PROPERTIES SOVERSION ${MAJORVER})
+TARGET_LINK_LIBRARIES(${TARGET_LIB_CLIENT} PUBLIC ${TARGET_LIB_COMMON})
+
+INSTALL(TARGETS ${TARGET_LIB_CLIENT} DESTINATION ${LIB_INSTALL_DIR})
diff --git a/src/client/client.cc b/src/client/client.cc
new file mode 100644 (file)
index 0000000..2fff390
--- /dev/null
@@ -0,0 +1,354 @@
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#include <cstring>
+#include <map>
+#include <memory>
+#include <string>
+
+#include "client/include/capmgr.h"
+#include "common/sql_connection.h"
+#include "common/sql_statement.h"
+#include "common/sqlite_connection.h"
+#include "common/utils/logging.h"
+
+#define API __attribute__((visibility("default")))
+
+namespace {
+
+const char kDBPath[] = "/run/capmgr/capmgr.db";
+
+}  // namespace
+
+struct capmgr_device_s {
+  std::string name;
+  std::string profile;
+  std::string address;
+  std::string uuid;
+};
+
+struct capmgr_app_control_s {
+  capmgr_device_h device;
+  std::string operation;
+  std::string uri;
+  std::string mime;
+  std::string appid;
+  std::map<std::string, std::string> extra_data;
+};
+
+API int capmgr_device_foreach_devices(capmgr_device_foreach_cb cb,
+    void* user_data) {
+  if (!cb)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  std::unique_ptr<capmgr::SQLConnection> sql_conn(
+      new capmgr::SQLiteConnection(kDBPath));
+
+  const char kQueryForeachDevices[] =
+      "SELECT name, profile, address, uuid FROM devices";
+  std::shared_ptr<capmgr::SQLStatement> stmt = sql_conn->PrepareStatement(
+      kQueryForeachDevices);
+  while (stmt->Step() == capmgr::SQLStatement::StepResult::ROW) {
+    struct capmgr_device_s dev;
+    dev.name = stmt->GetColumnString(0);
+    dev.profile = stmt->GetColumnString(1);
+    dev.address = stmt->GetColumnString(2);
+    dev.uuid = stmt->GetColumnString(3);
+    if (cb(&dev, user_data))
+      break;
+  }
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_clone(const capmgr_device_h device,
+    capmgr_device_h* device_clone) {
+  if (!device || !device_clone)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  try {
+    struct capmgr_device_s* clone = new struct capmgr_device_s;
+
+    clone->name = device->name;
+    clone->profile = device->profile;
+    clone->address = device->address;
+    clone->uuid = device->uuid;
+
+    *device_clone = clone;
+  } catch (const std::bad_alloc& e) {
+    LOG(ERROR) << e.what();
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+  }
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_destroy(capmgr_device_h device) {
+  if (!device)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  delete device;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_get_name(capmgr_device_h device, char** name) {
+  if (!device || !name)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *name = strdup(device->name.c_str());
+  if (*name == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_get_profile(capmgr_device_h device, char** profile) {
+  if (!device || !profile)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *profile = strdup(device->profile.c_str());
+  if (*profile == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_get_address(capmgr_device_h device, char** address) {
+  if (!device || !address)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *address = strdup(device->address.c_str());
+  if (*address == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_device_get_uuid(capmgr_device_h device, char** uuid) {
+  if (!device || !uuid)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *uuid = strdup(device->uuid.c_str());
+  if (*uuid == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_create(capmgr_app_control_h* app_control) {
+  if (!app_control)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  try {
+    struct capmgr_app_control_s* control = new struct capmgr_app_control_s;
+
+    *app_control = control;
+  } catch (const std::bad_alloc& e) {
+    LOG(ERROR) << e.what();
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+  }
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_clone(const capmgr_app_control_h app_control,
+    capmgr_app_control_h* app_control_clone) {
+  if (!app_control || !app_control_clone)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  try {
+    struct capmgr_app_control_s* clone = new struct capmgr_app_control_s;
+
+    int ret = capmgr_device_clone(app_control->device, &clone->device);
+    if (ret != CAPMGR_ERROR_NONE) {
+      delete clone;
+      return CAPMGR_ERROR_OUT_OF_MEMORY;
+    }
+    clone->operation = app_control->operation;
+    clone->uri = app_control->uri;
+    clone->mime = app_control->mime;
+    clone->appid = app_control->appid;
+
+    *app_control_clone = clone;
+  } catch (const std::bad_alloc& e) {
+    LOG(ERROR) << e.what();
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+  }
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_destroy(capmgr_app_control_h app_control) {
+  if (!app_control)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  capmgr_device_destroy(app_control->device);
+  delete app_control;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_get_operation(capmgr_app_control_h app_control,
+    char** operation) {
+  if (!app_control || !operation)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *operation = strdup(app_control->operation.c_str());
+  if (*operation == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_get_uri(capmgr_app_control_h app_control,
+    char** uri) {
+  if (!app_control || !uri)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *uri = strdup(app_control->uri.c_str());
+  if (*uri == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_get_mime(capmgr_app_control_h app_control,
+    char** mime) {
+  if (!app_control || !mime)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *mime = strdup(app_control->mime.c_str());
+  if (*mime == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_get_appid(capmgr_app_control_h app_control,
+    char** appid) {
+  if (!app_control || !appid)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  *appid = strdup(app_control->appid.c_str());
+  if (*appid == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_get_extra_data(capmgr_app_control_h app_control,
+    const char* key, char** value) {
+  if (!app_control || !key || !value)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  // TODO(jeremy.jang): handle reserved key
+
+  const auto it = app_control->extra_data.find(key);
+  if (it == app_control->extra_data.end()) {
+    LOG(ERROR) << "There is no extra data of key(" << key << ")";
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+  }
+
+  *value = strdup(it->second.c_str());
+  if (*value == nullptr)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_set_device(capmgr_app_control_h app_control,
+    const capmgr_device_h device) {
+  if (!app_control || !device)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  int ret = capmgr_device_clone(device, &app_control->device);
+  if (ret != CAPMGR_ERROR_NONE)
+    return CAPMGR_ERROR_OUT_OF_MEMORY;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_set_operation(capmgr_app_control_h app_control,
+    const char* operation) {
+  if (!app_control || !operation)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  app_control->operation = operation;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_set_uri(capmgr_app_control_h app_control,
+    const char* uri) {
+  if (!app_control || !uri)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  app_control->uri = uri;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_set_mime(capmgr_app_control_h app_control,
+    const char* mime) {
+  if (!app_control || !mime)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  app_control->mime = mime;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_set_appid(capmgr_app_control_h app_control,
+    const char* appid) {
+  if (!app_control || !appid)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  app_control->appid = appid;
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_add_extra_data(capmgr_app_control_h app_control,
+    const char* key, const char* value) {
+  if (!app_control || !key || !value)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  // TODO(jeremy.jang): handle reserved key
+
+  auto r = app_control->extra_data.emplace(key, value);
+  if (!r.second) {
+    auto elem = r.first;
+    elem->second = value;
+  }
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_remove_extra_data(capmgr_app_control_h app_control,
+    const char* key) {
+  if (!app_control || !key)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  // TODO(jeremy.jang): handle reserved key
+
+  const auto it = app_control->extra_data.find(key);
+  if (it == app_control->extra_data.end()) {
+    LOG(ERROR) << "There is no extra data of key(" << key << ")";
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+  }
+
+  app_control->extra_data.erase(it);
+
+  return CAPMGR_ERROR_NONE;
+}
+
+API int capmgr_app_control_send(capmgr_app_control_h app_control) {
+  if (!app_control || !app_control->device)
+    return CAPMGR_ERROR_INVALID_PARAMETER;
+
+  // send app_control
+
+  return CAPMGR_ERROR_NONE;
+}
diff --git a/src/client/include/capmgr.h b/src/client/include/capmgr.h
new file mode 100644 (file)
index 0000000..92dc8bf
--- /dev/null
@@ -0,0 +1,178 @@
+// Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef CLIENT_INCLUDE_CAPMGR_H_
+#define CLIENT_INCLUDE_CAPMGR_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/**
+ * @brief
+ */
+enum capmgr_error {
+  CAPMGR_ERROR_NONE,
+  CAPMGR_ERROR_INVALID_PARAMETER,
+  CAPMGR_ERROR_OUT_OF_MEMORY,
+};
+
+/**
+ * @brief
+ */
+typedef struct capmgr_device_s* capmgr_device_h;
+
+/**
+ * @brief
+ */
+typedef struct capmgr_app_control_s* capmgr_app_control_h;
+
+/**
+ * @brief
+ */
+typedef int (*capmgr_device_foreach_cb)(const capmgr_device_h device,
+    void* user_data);
+
+/**
+ * @brief
+ */
+int capmgr_device_foreach_devices(capmgr_device_foreach_cb cb,
+    void* user_data);
+
+/**
+ * @brief
+ */
+int capmgr_device_clone(const capmgr_device_h device,
+    capmgr_device_h* device_clone);
+
+/**
+ * @brief
+ */
+int capmgr_device_destroy(capmgr_device_h device);
+
+/**
+ * @brief
+ */
+int capmgr_device_get_name(capmgr_device_h device, char** name);
+
+/**
+ * @brief
+ */
+int capmgr_device_get_profile(capmgr_device_h device, char** profile);
+
+/**
+ * @brief
+ */
+int capmgr_device_get_address(capmgr_device_h device, char** address);
+
+/**
+ * @brief
+ */
+int capmgr_device_get_uuid(capmgr_device_h device, char** uuid);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_create(capmgr_app_control_h* app_control);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_clone(const capmgr_app_control_h app_control,
+    capmgr_app_control_h* app_control_clone);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_destroy(capmgr_app_control_h app_control);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_device(capmgr_app_control_h app_control,
+    capmgr_device_h* device);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_operation(capmgr_app_control_h app_control,
+    char** operation);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_uri(capmgr_app_control_h app_control,
+    char** uri);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_mime(capmgr_app_control_h app_control,
+    char** mime);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_appid(capmgr_app_control_h app_control,
+    char** appid);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_get_extra_data(capmgr_app_control_h app_control,
+    const char* key, char** value);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_set_device(capmgr_app_control_h app_control,
+    const capmgr_device_h device);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_set_operation(capmgr_app_control_h app_control,
+    const char* operation);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_set_uri(capmgr_app_control_h app_control,
+    const char* uri);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_set_mime(capmgr_app_control_h app_control,
+    const char* mime);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_set_appid(capmgr_app_control_h app_control,
+    const char* appid);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_add_extra_data(capmgr_app_control_h app_control,
+    const char* key, const char* value);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_remove_extra_data(capmgr_app_control_h app_control,
+    const char* key);
+
+/**
+ * @brief
+ */
+int capmgr_app_control_send(capmgr_app_control_h app_control);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // CLIENT_INCLUDE_CAPMGR_H_