--- /dev/null
+// 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;
+}
--- /dev/null
+// 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_