From 38ecabfa113d8fc97ad5d7c2f0acd289f3de46aa Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Wed, 28 Mar 2018 14:29:00 +0900 Subject: [PATCH] capi: seperate a file related dbus Signed-off-by: saerome.kim --- capi/CMakeLists.txt | 3 +- capi/src/companion.c | 643 +++++++++------------------------- capi/src/companion_dbus_internal.c | 494 ++++++++++++++++++++++++++ capi/src/companion_dbus_internal.h | 26 ++ capi/src/companion_internal.h | 163 +++++++++ capi/unittest/companion_unit_test.cpp | 0 6 files changed, 858 insertions(+), 471 deletions(-) mode change 100644 => 100755 capi/CMakeLists.txt create mode 100644 capi/src/companion_dbus_internal.c create mode 100644 capi/src/companion_dbus_internal.h create mode 100644 capi/src/companion_internal.h mode change 100644 => 100755 capi/unittest/companion_unit_test.cpp diff --git a/capi/CMakeLists.txt b/capi/CMakeLists.txt old mode 100644 new mode 100755 index b3844f5..575be48 --- a/capi/CMakeLists.txt +++ b/capi/CMakeLists.txt @@ -13,7 +13,8 @@ INCLUDE_DIRECTORIES(${INC_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src) SET(SOURCES src/companion.c - src/companion_gdbus.c) + src/companion_gdbus.c + src/companion_dbus_internal.c) ADD_LIBRARY(${CAPI_FN} SHARED ${SOURCES}) diff --git a/capi/src/companion.c b/capi/src/companion.c index 58f7f57..872ce99 100644 --- a/capi/src/companion.c +++ b/capi/src/companion.c @@ -17,14 +17,14 @@ #include #include -#include -#include #include #include #include #include #include +#include +#include /** * Companion Manager CAPI @@ -42,102 +42,13 @@ do { \ pthread_mutex_unlock(&mutex); \ } while (0); -#define COMP_DBUS_SERVICE "org.tizen.companion" /**< For companion dbus */ -#define COMP_DBUS_GROUP_PATH "/org/tizen/companion/group" /**< For group dbus */ -#define COMP_DBUS_ENABLER_PATH "/org/tizen/companion/enabler" /**< dbus auto-activation */ - int ref_count = 0; /**< How many clients are alive */ bool __is_initialized = false; /**< Initialize or not */ -typedef struct _companion_group_t { - char *uri_path; /**< URI Path for group resource */ - char *device_id; /**< Device ID of the device has this group resource */ - char *group_name; /**< Group Name (Friendly name) */ - char *host_addr; /**< Host address */ - char *resource_type; /**< Resource type */ - companion_group_type_e type; /**< Mine or Remote */ -} companion_group_t; - -typedef struct _companion_device_t { - char *device_id; /**< Device ID */ - char *ip; /**< Device IP */ - char *device_type; /**< Device Type */ - int port; /**< Port Number */ -} companion_device_t; - -typedef struct _group_found_cb_t { - companion_group_found_cb found_cb; - void *user_data; -} group_found_cb_t; - -typedef struct _device_found_cb_t { - companion_device_found_cb found_cb; - void *user_data; -} device_found_cb_t; - -typedef struct _group_find_finish_cb_t { - companion_group_find_finish_cb finish_cb; - void *user_data; -} group_find_finish_cb_t; - -typedef struct _device_find_finish_cb_t { - companion_device_find_finish_cb finish_cb; - void *user_data; -} device_find_finish_cb_t; - -typedef struct _mowned_device_found_cb_t { - companion_device_found_cb found_cb; - void *user_data; -} mowned_device_found_cb_t; - -typedef struct _mowned_device_find_finish_cb_t { - companion_device_find_finish_cb finish_cb; - void *user_data; -} mowned_device_find_finish_cb_t; - -typedef struct _device_invite_result_cb_t { - companion_device_invite_result_cb result_cb; - void *user_data; -} device_invite_result_cb_t; - -typedef struct _device_eject_result_cb_t { - companion_device_eject_result_cb result_cb; - void *user_data; -} device_eject_result_cb_t; - -typedef struct _send_data_finish_cb_t { - companion_send_data_finish_cb finish_cb; - void *user_data; -} send_data_finish_cb_t; - -typedef struct _request_result_cb_t { - companion_request_result_cb result_cb; - void *user_data; -} request_result_cb_t; - -typedef struct _comp_manager_s { - gpointer dbus_connection; - GCancellable *ca; - - Group *group_proxy; /**< To receive signal from companion-manager */ - Enabler *enabler_proxy; /**< Enbler proxy */ - - group_found_cb_t group_found_cb; - device_found_cb_t device_found_cb; - group_find_finish_cb_t group_find_finish_cb; - device_find_finish_cb_t device_find_finish_cb; - mowned_device_found_cb_t mowned_device_found_cb; - mowned_device_find_finish_cb_t mowned_device_find_finish_cb; - device_invite_result_cb_t device_invite_result_cb; - device_eject_result_cb_t device_eject_result_cb; - send_data_finish_cb_t send_data_finish_cb; - request_result_cb_t request_result_cb; -} comp_manager_s; - -static companion_group_t *_create_group_handle(char *uri_path, char *device_id, +static companion_group_s *_create_group_handle(char *uri_path, char *device_id, char *group_name, char *host_addr, char *resource_type, companion_group_type_e type) { - companion_group_t *group = calloc(1, sizeof(companion_group_t)); + companion_group_s *group = calloc(1, sizeof(companion_group_s)); if (NULL == group) { /* LCOV_EXCL_START */ _ERR("Memory allocation failed"); @@ -191,10 +102,10 @@ CREATE_GROUP_HANDLER_ERROR: /* LCOV_EXCL_STOP */ } -static companion_device_t *_create_device_handle(char *device_id, char *ip, +static companion_device_s *_create_device_handle(char *device_id, char *ip, char *device_type, int port) { - companion_device_t *device = calloc(1, sizeof(companion_device_t)); + companion_device_s *device = calloc(1, sizeof(companion_device_s)); if (NULL == device) { /* LCOV_EXCL_START */ _ERR("Memory allocation failed"); @@ -238,12 +149,13 @@ CREATE_DEVICE_HANDLER_ERROR: /* LCOV_EXCL_STOP */ } +/* LCOV_EXCL_START */ static void __group_found_cb(Group *object, GVariant *va, gpointer user_data) { GVariantIter *iter = NULL; const gchar *key; GVariant *key_value = NULL; - companion_group_t *group = NULL; + companion_group_s *group = NULL; char *uri_path = NULL; char *device_id = NULL; char *group_name = NULL; @@ -254,12 +166,7 @@ static void __group_found_cb(Group *object, GVariant *va, gpointer user_data) NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); g_variant_get(va, "a{sv}", &iter); while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) { @@ -290,12 +197,8 @@ static void _group_find_finish_cb(Group *object, gint ret, gpointer user_data) NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->group_find_finish_cb.finish_cb) handle->group_find_finish_cb.finish_cb(ret, handle->group_find_finish_cb.user_data); @@ -308,17 +211,12 @@ static void __device_found_cb(Group *object, gint count, GVariant *va, GVariantIter *iter_row = NULL; const gchar *key; GVariant *key_value; - companion_device_t *device = NULL; + companion_device_s *device = NULL; NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); g_variant_get(va, "aa{sv}", &iter); while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { @@ -351,12 +249,8 @@ static void _device_find_finish_cb(Group *object, gint ret, gpointer user_data) NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->device_find_finish_cb.finish_cb) handle->device_find_finish_cb.finish_cb(ret, handle->device_find_finish_cb.user_data); @@ -369,17 +263,12 @@ static void __mowned_device_found_cb(Group *object, gint count, GVariant *va, GVariantIter *iter_row = NULL; const gchar *key; GVariant *key_value; - companion_device_t *device = NULL; + companion_device_s *device = NULL; NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); g_variant_get(va, "aa{sv}", &iter); while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { @@ -415,12 +304,8 @@ static void _mowned_device_find_finish_cb(Group *object, gint ret, NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->mowned_device_find_finish_cb.finish_cb) handle->mowned_device_find_finish_cb.finish_cb(ret, handle->mowned_device_find_finish_cb.user_data); @@ -431,12 +316,8 @@ static void __device_invite_result_cb(Group *object, gint ret, gpointer user_dat NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->device_invite_result_cb.result_cb) handle->device_invite_result_cb.result_cb(ret, handle->device_invite_result_cb.user_data); @@ -447,12 +328,8 @@ static void __device_eject_result_cb(Group *object, gint ret, gpointer user_data NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->device_eject_result_cb.result_cb) handle->device_eject_result_cb.result_cb(ret, handle->device_eject_result_cb.user_data); @@ -464,12 +341,8 @@ static void __send_data_finish_cb(Group *object, gchar *resp_data, gint ret, NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->send_data_finish_cb.finish_cb) handle->send_data_finish_cb.finish_cb(ret, resp_data, handle->send_data_finish_cb.user_data); @@ -481,166 +354,14 @@ static void __request_result_cb(Group *object, gchar *cmd, gchar *arg, gint ret, NOTUSED(object); comp_manager_s *handle = user_data; - if (!handle) { - /* LCOV_EXCL_START */ - _ERR("handle is null"); - return; - /* LCOV_EXCL_STOP */ - } + companion_check_null_ret("user_data", user_data); + if (handle->request_result_cb.result_cb) handle->request_result_cb.result_cb(cmd, arg, ret, handle->request_result_cb.user_data); } - -static int _group_proxy_init(comp_manager_s *handle); -static int _enabler_proxy_init(comp_manager_s *handle); -static void _group_proxy_deinit(comp_manager_s *handle); -static void _enabler_proxy_deinit(comp_manager_s *handle); - -static int _gdbus_initialize(comp_manager_s *handle) -{ - int ret = COMP_ERROR_NONE; - - _group_proxy_init(handle); - _enabler_proxy_init(handle); - - if (handle->group_proxy == NULL) - ret = -1; /* LCOV_EXCL_LINE */ - - if (handle->enabler_proxy == NULL) - ret = -1; /* LCOV_EXCL_LINE */ - - handle->ca = g_cancellable_new(); - - return ret; -} - -static int _gdbus_deinitialize(comp_manager_s *handle) -{ - int ret = COMP_ERROR_NONE; - - _group_proxy_deinit(handle); - _enabler_proxy_deinit(handle); - - g_cancellable_cancel(handle->ca); - g_object_unref(handle->ca); - handle->ca = NULL; - - return ret; -} - -static int _enabler_proxy_init(comp_manager_s *handle) -{ - int ret = COMP_ERROR_NONE; - GError *error = NULL; - handle->enabler_proxy = enabler_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, COMP_DBUS_SERVICE, - COMP_DBUS_ENABLER_PATH, NULL, &error); - if (NULL == handle->enabler_proxy) { - /* LCOV_EXCL_START */ - if (error != NULL) { - _ERR("Failed to connect to the D-BUS daemon [%s]", error->message); - g_error_free(error); - } - return COMP_ERROR_IO_ERROR; - /* LCOV_EXCL_STOP */ - } - return ret; -} - -static void _group_proxy_deinit(comp_manager_s *handle) -{ - g_object_unref(handle->group_proxy); - handle->group_proxy = NULL; -} - -static void _enabler_proxy_deinit(comp_manager_s *handle) -{ - int ret; - - enabler_call_disable_sync(handle->enabler_proxy, &ret, NULL, NULL); - g_object_unref(handle->enabler_proxy); - handle->enabler_proxy = NULL; -} - -/* LCOV_EXCL_START */ -static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec, - gpointer *user_data) -{ - GDBusProxy *proxy = G_DBUS_PROXY(object); - gchar *name_owner = g_dbus_proxy_get_name_owner(proxy); - comp_manager_s *handle = (comp_manager_s *)user_data; - if (!handle) { - _ERR("handle is null"); - return; - } - - LOGD("Name owner notify [%s]", name_owner); - - if (NULL == name_owner) - _gdbus_deinitialize(handle); - free(name_owner); - -} /* LCOV_EXCL_STOP */ -static int _group_proxy_init(comp_manager_s *handle) -{ - int id = -1; - GError *error = NULL; - - handle->group_proxy = group_proxy_new_for_bus_sync( - G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - COMP_DBUS_SERVICE, - COMP_DBUS_GROUP_PATH, - NULL, - &error); - if (NULL == handle->group_proxy) { - /* LCOV_EXCL_START */ - if (error != NULL) { - _ERR("Failed to connect to the D-BUS daemon [%s]", error->message); - g_error_free(error); - } - return COMP_ERROR_IO_ERROR; - /* LCOV_EXCL_STOP */ - } - - id = g_signal_connect(handle->group_proxy, "notify::g-name-owner", - G_CALLBACK(_dbus_name_owner_notify), handle); - if (0 == id) { - /* LCOV_EXCL_START */ - _ERR("g_signal_connect() Fail"); - g_object_unref(handle->group_proxy); - handle->group_proxy = NULL; - return COMP_ERROR_IO_ERROR; - /* LCOV_EXCL_STOP */ - } - - g_signal_connect(handle->group_proxy, - "group-found", G_CALLBACK(__group_found_cb), handle); - g_signal_connect(handle->group_proxy, - "group-find-finish", G_CALLBACK(_group_find_finish_cb), handle); - g_signal_connect(handle->group_proxy, - "device-found", G_CALLBACK(__device_found_cb), handle); - g_signal_connect(handle->group_proxy, - "device-find-finish", G_CALLBACK(_device_find_finish_cb), handle); - g_signal_connect(handle->group_proxy, - "mowned-device-found", G_CALLBACK(__mowned_device_found_cb), handle); - g_signal_connect(handle->group_proxy, - "mowned-device-find-finish", G_CALLBACK(_mowned_device_find_finish_cb), handle); - g_signal_connect(handle->group_proxy, - "device-invite-result", G_CALLBACK(__device_invite_result_cb), handle); - g_signal_connect(handle->group_proxy, - "device-eject-result", G_CALLBACK(__device_eject_result_cb), handle); - g_signal_connect(handle->group_proxy, - "send-data-finish", G_CALLBACK(__send_data_finish_cb), handle); - g_signal_connect(handle->group_proxy, - "request-result", G_CALLBACK(__request_result_cb), handle); - - return COMP_ERROR_NONE; -} - EXPORT_API int companion_initialize(companion_h *handle) { int ret = COMP_ERROR_NONE; @@ -665,12 +386,14 @@ EXPORT_API int companion_initialize(companion_h *handle) *handle = comp_manager; if (__is_initialized) { + /* LCOV_EXCL_START */ _DBG("Already initialized"); return COMP_ERROR_ALREADY_INITIALIZED; + /* LCOV_EXCL_STOP */ } COMPANION_LOCK; - ret = _gdbus_initialize(*handle); + ret = gdbus_initialize(*handle); __is_initialized = true; COMPANION_UNLOCK; ref_count++; @@ -689,13 +412,15 @@ EXPORT_API int companion_deinitialize(companion_h handle) COMPANION_LOCK; if (0 > --ref_count) { + /* LCOV_EXCL_START */ _DBG("all connections closed\n"); ref_count = 0; return ret; + /* LCOV_EXCL_STOP */ } if (__is_initialized == true && ref_count == 0) { - ret = _gdbus_deinitialize(handle); + ret = gdbus_deinitialize(handle); __is_initialized = false; } @@ -710,12 +435,10 @@ EXPORT_API int companion_deinitialize(companion_h handle) EXPORT_API int companion_group_create(companion_h handle, char *group_name) { int ret = COMP_ERROR_NONE; + GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); /* create group to daemon using gdbus */ group_call_create_sync(_handle->group_proxy, group_name, &ret, NULL, &error); @@ -723,7 +446,7 @@ EXPORT_API int companion_group_create(companion_h handle, char *group_name) return ret; } -EXPORT_API void companion_group_destroy(companion_group_t *group) +EXPORT_API void companion_group_destroy(companion_group_s *group) { companion_check_null_ret("group", group); @@ -759,10 +482,7 @@ EXPORT_API int companion_group_find(companion_h handle, int timeout, GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); _handle->group_found_cb.found_cb = found_cb; _handle->group_find_finish_cb.finish_cb = finish_cb; @@ -783,10 +503,7 @@ EXPORT_API int companion_group_get_found_groups(companion_h handle, GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); /* get groups from daemon using gdbus */ group_call_get_found_groups_sync(_handle->group_proxy, &ret, &va, NULL, &error); @@ -811,7 +528,7 @@ EXPORT_API int companion_group_get_found_groups(companion_h handle, char *host_addr = NULL; char *resource_type = NULL; - companion_group_t *group = NULL; + companion_group_s *group = NULL; companion_group_type_e type = COMPANION_GROUP_TYPE_ERROR; while (g_variant_iter_loop(iter_row, "{sv}", &key, &key_value)) { @@ -876,13 +593,11 @@ EXPORT_API int companion_device_find(companion_h handle, int timeout, void *user_data) { int ret = COMP_ERROR_NONE; + GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); _handle->device_found_cb.found_cb = found_cb; _handle->device_found_cb.user_data = user_data; @@ -900,6 +615,7 @@ EXPORT_API int companion_device_find_mowned_device(companion_h handle, companion_device_find_finish_cb finish_cb, void *user_data) { int ret = COMP_ERROR_NONE; + GError *error = NULL; comp_manager_s *_handle = handle; @@ -923,16 +639,10 @@ EXPORT_API int companion_send_data(companion_h handle, companion_device_h device GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); - companion_device_t *dev = (companion_device_t *)device; - if (!dev) { - _ERR("dev is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_device_s *dev = (companion_device_s *)device; + companion_check_null_ret_error("dev", dev, COMP_ERROR_INVALID_PARAMETER); buf = g_malloc0(len + 1); if (NULL == buf) { @@ -958,16 +668,13 @@ EXPORT_API int companion_send_data(companion_h handle, companion_device_h device EXPORT_API int companion_device_get_found_devices(companion_h handle, companion_device_h **devices, int *count) { - int ret = 0; + int ret = COMP_ERROR_NONE; + int num = 0; GVariant *va = NULL; GError *error = NULL; comp_manager_s *_handle = handle; - - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); /* get groups from daemon using gdbus */ group_call_get_remote_device_sync(_handle->group_proxy, &num, &va, NULL, &error); @@ -990,7 +697,7 @@ EXPORT_API int companion_device_get_found_devices(companion_h handle, char *addr = NULL; char *ver = NULL; int port; - companion_device_t *device = NULL; + companion_device_s *device = NULL; while (g_variant_iter_loop(iter_row, "{sv}", &key, &key_value)) { if (g_strcmp0(key, "DeviceID") == 0) @@ -1018,16 +725,13 @@ EXPORT_API int companion_device_get_found_devices(companion_h handle, EXPORT_API int companion_device_get_found_mowned_devices( companion_h handle, companion_device_h **devices, int *count) { - int ret = 0; + int ret = COMP_ERROR_NONE; + int num = 0; GVariant *va = NULL; GError *error = NULL; comp_manager_s *_handle = handle; - - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); /* get groups from daemon using gdbus */ group_call_get_mowned_device_sync(_handle->group_proxy, &num, &va, NULL, &error); @@ -1050,7 +754,7 @@ EXPORT_API int companion_device_get_found_mowned_devices( char *addr = NULL; char *ver = NULL; int port; - companion_device_t *device = NULL; + companion_device_s *device = NULL; while (g_variant_iter_loop(iter_row, "{sv}", &key, &key_value)) { if (g_strcmp0(key, "DeviceID") == 0) @@ -1078,7 +782,8 @@ EXPORT_API int companion_device_get_found_mowned_devices( EXPORT_API int companion_device_get_my_device(companion_h handle, companion_device_h *device) { - int ret = 0; + int ret = COMP_ERROR_NONE; + GError *error = NULL; GVariant *va = NULL; GVariantIter *iter = NULL; @@ -1088,12 +793,9 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, char *addr = NULL; char *ver = NULL; int port = -1; - comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + comp_manager_s *_handle = handle; + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); group_call_get_my_mowned_device_sync(_handle->group_proxy, &va, NULL, &error); @@ -1119,24 +821,25 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, return ret; } -static void companion_device_destroy(companion_device_t *device) +EXPORT_API void companion_device_destroy(companion_device_h device) { + companion_device_s *_device = device; companion_check_null_ret("device", device); - if (device->device_id) { - g_free(device->device_id); - device->device_id = NULL; + if (_device->device_id) { + g_free(_device->device_id); + _device->device_id = NULL; } - if (device->ip) { - g_free(device->ip); - device->ip = NULL; + if (_device->ip) { + g_free(_device->ip); + _device->ip = NULL; } - if (device->device_type) { - g_free(device->device_type); - device->device_type = NULL; + if (_device->device_type) { + g_free(_device->device_type); + _device->device_type = NULL; } - g_free(device); - device = NULL; + g_free(_device); + _device = NULL; } EXPORT_API int companion_device_get_my_uuid(companion_h handle, char **uuid) @@ -1145,10 +848,7 @@ EXPORT_API int companion_device_get_my_uuid(companion_h handle, char **uuid) GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); group_call_get_my_uuid_sync(_handle->group_proxy, uuid, NULL, &error); @@ -1159,53 +859,52 @@ EXPORT_API int companion_device_get_my_uuid(companion_h handle, char **uuid) EXPORT_API int companion_group_merge(companion_h hadnle, companion_group_h dest_group, companion_group_h src_group) { + int ret = COMP_ERROR_NONE; + NOTUSED(dest_group); NOTUSED(src_group); - return 0; + + return ret; } EXPORT_API int companion_group_delete(companion_h handle, companion_group_h group) { + int ret = COMP_ERROR_NONE; + NOTUSED(handle); NOTUSED(group); - return 0; + + return ret; } EXPORT_API int companion_group_get_member_devices(companion_h handle, companion_group_h group, companion_device_h **devices, int *count) { + int ret = COMP_ERROR_NONE; + NOTUSED(handle); NOTUSED(group); NOTUSED(devices); NOTUSED(count); - return 0; + return ret; } EXPORT_API int companion_device_invite(companion_h handle, companion_group_h group, companion_device_h device, char *PIN, companion_device_invite_result_cb result_cb, void *user_data) { - int ret = 0; + int ret = COMP_ERROR_NONE; GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); - companion_group_t *grp = (companion_group_t *)group; - if (!grp) { - _ERR("grp is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_group_s *grp = (companion_group_s *)group; + companion_check_null_ret_error("grp", grp, COMP_ERROR_INVALID_PARAMETER); - companion_device_t *dev = (companion_device_t *)device; - if (!dev) { - _ERR("dev is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_device_s *dev = (companion_device_s *)device; + companion_check_null_ret_error("dev", dev, COMP_ERROR_INVALID_PARAMETER); _handle->device_invite_result_cb.result_cb = result_cb; _handle->device_invite_result_cb.user_data = user_data; @@ -1230,16 +929,10 @@ EXPORT_API int companion_device_eject(companion_h handle, GError *error = NULL; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); - companion_device_t *dev = (companion_device_t *)device; - if (!dev) { - _ERR("dev is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_device_s *dev = (companion_device_s *)device; + companion_check_null_ret_error("dev", dev, COMP_ERROR_INVALID_PARAMETER); _handle->device_eject_result_cb.result_cb = result_cb; _handle->device_eject_result_cb.user_data = user_data; @@ -1257,22 +950,28 @@ EXPORT_API int companion_device_eject(companion_h handle, EXPORT_API int companion_group_information_create(companion_group_h* group) { - companion_group_h _group = g_malloc0(sizeof(companion_group_t)); + int ret = COMP_ERROR_NONE; + + companion_group_h _group = g_malloc0(sizeof(companion_group_s)); + if (NULL == _group) { + _ERR("Memory allocation Failed(%d)", errno); + return COMP_ERROR_OUT_OF_MEMORY; + } *group = _group; - return 0; + return ret; } EXPORT_API int companion_group_information_clone(companion_group_h target, companion_group_h source) { - companion_group_t * dst = (companion_group_t *)target; - companion_group_t * src = (companion_group_t *)source; + int ret = COMP_ERROR_NONE; - if (!dst || !src) { - _ERR("Invaild parameters: dst=%p src=%p", dst, src); - return -1; - } + companion_group_s * dst = (companion_group_s *)target; + companion_group_s * src = (companion_group_s *)source; + + companion_check_null_ret_error("target", target, COMP_ERROR_INVALID_PARAMETER); + companion_check_null_ret_error("source", source, COMP_ERROR_INVALID_PARAMETER); if (src->uri_path) dst->uri_path = g_strdup(src->uri_path); @@ -1291,15 +990,15 @@ EXPORT_API int companion_group_information_clone(companion_group_h target, dst->type = src->type; - return 0; + return ret; } EXPORT_API int companion_group_information_destroy(companion_group_h data) { - companion_group_t * group = (companion_group_t *)data; + int ret = COMP_ERROR_NONE; - if (!group) - return -1; + companion_group_s * group = (companion_group_s *)data; + companion_check_null_ret_error("group", group, COMP_ERROR_INVALID_PARAMETER); if (group->uri_path) { g_free(group->uri_path); @@ -1314,77 +1013,93 @@ EXPORT_API int companion_group_information_destroy(companion_group_h data) group->group_name = NULL; } if (group->host_addr) { - g_free((companion_group_t *)group->host_addr); + g_free((companion_group_s *)group->host_addr); group->host_addr = NULL; } if (group->resource_type) { - g_free((companion_group_t *)group->resource_type); + g_free((companion_group_s *)group->resource_type); group->resource_type = NULL; } g_free(group); group = NULL; - return 0; + return ret; } EXPORT_API int companion_group_information_get_type(companion_group_h group, companion_group_type_e *type) { - *type = ((companion_group_t *)group)->type; + int ret = COMP_ERROR_NONE; - return 0; + *type = ((companion_group_s *)group)->type; + + return ret; } EXPORT_API int companion_group_information_get_resource_type( companion_group_h group, char **resource_type) { - *resource_type = g_strdup(((companion_group_t *)group)->resource_type); + int ret = COMP_ERROR_NONE; + + *resource_type = g_strdup(((companion_group_s *)group)->resource_type); - return 0; + return ret; } EXPORT_API int companion_group_information_get_uri_path( companion_group_h group, char **uri_path) { - *uri_path = g_strdup(((companion_group_t *)group)->uri_path); + int ret = COMP_ERROR_NONE; - return 0; + *uri_path = g_strdup(((companion_group_s *)group)->uri_path); + + return ret; } EXPORT_API int companion_group_information_get_name( companion_group_h group, char **name) { - *name = g_strdup(((companion_group_t *)group)->group_name); + int ret = COMP_ERROR_NONE; + + *name = g_strdup(((companion_group_s *)group)->group_name); - return 0; + return ret; } EXPORT_API int companion_group_information_get_host_addr (companion_group_h group, char **host_addr) { - *host_addr = g_strdup(((companion_group_t *)group)->host_addr); + int ret = COMP_ERROR_NONE; - return 0; + *host_addr = g_strdup(((companion_group_s *)group)->host_addr); + + return ret; } EXPORT_API int companion_device_information_create(companion_device_h* device) { - companion_device_h _device = g_malloc0(sizeof(companion_device_t)); + int ret = COMP_ERROR_NONE; + + companion_device_h _device = g_malloc0(sizeof(companion_device_s)); + if (NULL == device) { + _ERR("Memory allocation Failed(%d)", errno); + return COMP_ERROR_OUT_OF_MEMORY; + } *device = _device; - return 0; + return ret; } EXPORT_API int companion_device_information_clone(companion_device_h target, companion_device_h source) { - companion_device_t * dst = (companion_device_t *)target; - companion_device_t * src = (companion_device_t *)source; + int ret = COMP_ERROR_NONE; - if (!dst || !src) { - _ERR("Invaild parameters: dst=%p src=%p", dst, src); - return -1; - } + companion_device_s * dst = (companion_device_s *)target; + companion_device_s * src = (companion_device_s *)source; + + companion_check_null_ret_error("target", target, COMP_ERROR_INVALID_PARAMETER); + companion_check_null_ret_error("source", source, COMP_ERROR_INVALID_PARAMETER); if (src->device_id) dst->device_id = g_strdup(src->device_id); @@ -1393,17 +1108,14 @@ EXPORT_API int companion_device_information_clone(companion_device_h target, if (src->device_type) dst->device_type = g_strdup(src->device_type); - return 0; + return ret; } EXPORT_API int companion_device_information_destroy(companion_device_h data) { - companion_device_t * device = (companion_device_t *)data; - - if (!device) { - _ERR("Invaild parameters: device=%p", device); - return -1; - } + int ret = COMP_ERROR_NONE; + companion_device_s * device = (companion_device_s *)data; + companion_check_null_ret_error("data", data, COMP_ERROR_INVALID_PARAMETER); if (device->device_id) { g_free(device->device_id); @@ -1421,31 +1133,37 @@ EXPORT_API int companion_device_information_destroy(companion_device_h data) g_free(device); device = NULL; - return 0; + return ret; } EXPORT_API int companion_device_information_get_device_id( companion_device_h device, char **device_id) { - *device_id = g_strdup(((companion_device_t *)device)->device_id); + int ret = COMP_ERROR_NONE; - return 0; + *device_id = g_strdup(((companion_device_s *)device)->device_id); + + return ret; } EXPORT_API int companion_device_information_get_ip( companion_device_h device, char **ip) { - *ip = g_strdup(((companion_device_t *)device)->ip); + int ret = COMP_ERROR_NONE; - return 0; + *ip = g_strdup(((companion_device_s *)device)->ip); + + return ret; } EXPORT_API int companion_device_information_get_device_type( companion_device_h device, char **device_type) { - *device_type = g_strdup(((companion_device_t *)device)->device_type); + int ret = COMP_ERROR_NONE; + + *device_type = g_strdup(((companion_device_s *)device)->device_type); - return 0; + return ret; } EXPORT_API int companion_request_create_group(companion_h handle, @@ -1454,7 +1172,7 @@ EXPORT_API int companion_request_create_group(companion_h handle, int ret = COMP_ERROR_NONE; GError *error = NULL; comp_manager_s *_handle = handle; - companion_device_t *dev = (companion_device_t *)device; + companion_device_s *dev = (companion_device_s *)device; _DBG("Device id : %s", dev->device_id); @@ -1470,18 +1188,15 @@ EXPORT_API int companion_request_invite(companion_h handle, int ret = COMP_ERROR_NONE; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); - companion_group_t *grp = (companion_group_t *)group; + companion_group_s *grp = (companion_group_s *)group; if (!grp) { _ERR("grp is null"); return COMP_ERROR_INVALID_PARAMETER; } - companion_device_t *dev = (companion_device_t *)device; + companion_device_s *dev = (companion_device_s *)device; if (!dev) { _ERR("dev is null"); return COMP_ERROR_INVALID_PARAMETER; @@ -1504,22 +1219,13 @@ EXPORT_API int companion_request_eject(companion_h handle, int ret = COMP_ERROR_NONE; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); - companion_group_t *grp = (companion_group_t *)group; - if (!grp) { - _ERR("grp is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_group_s *grp = (companion_group_s *)group; + companion_check_null_ret_error("group", group, COMP_ERROR_INVALID_PARAMETER); - companion_device_t *dev = (companion_device_t *)device; - if (!dev) { - _ERR("dev is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_device_s *dev = (companion_device_s *)device; + companion_check_null_ret_error("device", device, COMP_ERROR_INVALID_PARAMETER); _DBG("%s", grp->device_id); _DBG("%s", grp->group_name); @@ -1545,10 +1251,7 @@ EXPORT_API int companion_request_result_callback(companion_h handle, { int ret = COMP_ERROR_NONE; comp_manager_s *_handle = handle; - if (!handle) { - _ERR("handle is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); _handle->request_result_cb.result_cb = result_cb; _handle->request_result_cb.user_data = user_data; diff --git a/capi/src/companion_dbus_internal.c b/capi/src/companion_dbus_internal.c new file mode 100644 index 0000000..ebf8feb --- /dev/null +++ b/capi/src/companion_dbus_internal.c @@ -0,0 +1,494 @@ +/* + * Copyright (c) 2016 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 +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define COMP_DBUS_SERVICE "org.tizen.companion" /**< For companion dbus */ +#define COMP_DBUS_GROUP_PATH "/org/tizen/companion/group" /**< For group dbus */ +#define COMP_DBUS_ENABLER_PATH "/org/tizen/companion/enabler" /**< dbus auto-activation */ + +static companion_group_s *_create_group_handle(char *uri_path, char *device_id, + char *group_name, char *host_addr, char *resource_type, companion_group_type_e type) +{ + companion_group_s *group = calloc(1, sizeof(companion_group_s)); + if (NULL == group) { + /* LCOV_EXCL_START */ + _ERR("Memory allocation failed"); + goto CREATE_GROUP_HANDLER_ERROR; + /* LCOV_EXCL_STOP */ + } + + group->uri_path = g_strdup(uri_path); + group->device_id = g_strdup(device_id); + group->group_name = g_strdup(group_name); + group->host_addr = g_strdup(host_addr); + group->resource_type = g_strdup(resource_type); + group->type = type; + + if (!group->uri_path || !group->device_id || + !group->host_addr || !group->resource_type) { + /* LCOV_EXCL_START */ + _ERR("Memory allocation failed"); + goto CREATE_GROUP_HANDLER_ERROR; + /* LCOV_EXCL_STOP */ + } + return group; + +CREATE_GROUP_HANDLER_ERROR: + /* LCOV_EXCL_START */ + if (group) { + if (group->uri_path) { + free(group->uri_path); + group->uri_path = NULL; + } + if (group->device_id) { + free(group->device_id); + group->device_id = NULL; + } + if (group->group_name) { + free(group->group_name); + group->group_name = NULL; + } + if (group->host_addr) { + free(group->host_addr); + group->host_addr = NULL; + } + if (group->resource_type) { + free(group->resource_type); + group->resource_type = NULL; + } + free(group); + group = NULL; + } + return NULL; + /* LCOV_EXCL_STOP */ +} + +static companion_device_s *_create_device_handle(char *device_id, char *ip, + char *device_type, int port) +{ + companion_device_s *device = calloc(1, sizeof(companion_device_s)); + if (NULL == device) { + /* LCOV_EXCL_START */ + _ERR("Memory allocation failed"); + goto CREATE_DEVICE_HANDLER_ERROR; + /* LCOV_EXCL_STOP */ + } + + device->device_id = g_strdup(device_id); + device->ip = g_strdup(ip); + device->device_type = g_strdup(device_type); + device->port = port; + + if (!device->device_id || !device->ip || !device->device_type) { + /* LCOV_EXCL_START */ + _ERR("Memory allocation failed"); + goto CREATE_DEVICE_HANDLER_ERROR; + /* LCOV_EXCL_STOP */ + } + + return device; + +CREATE_DEVICE_HANDLER_ERROR: + /* LCOV_EXCL_START */ + if (device) { + if (device->device_id) { + free(device->device_id); + device->device_id = NULL; + } + if (device->ip) { + free(device->ip); + device->ip = NULL; + } + if (device->device_type) { + free(device->device_type); + device->device_type = NULL; + } + free(device); + device = NULL; + } + return NULL; + /* LCOV_EXCL_STOP */ +} + +/* LCOV_EXCL_START */ +static void __group_found_cb(Group *object, GVariant *va, gpointer user_data) +{ + GVariantIter *iter = NULL; + const gchar *key; + GVariant *key_value = NULL; + companion_group_s *group = NULL; + char *uri_path = NULL; + char *device_id = NULL; + char *group_name = NULL; + char *host_addr = NULL; + char *resource_type = NULL; + companion_group_type_e type = COMPANION_GROUP_TYPE_ERROR; + + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + g_variant_get(va, "a{sv}", &iter); + while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) { + if (g_strcmp0(key, "URI") == 0) + uri_path = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "DeviceID") == 0) + device_id = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "GroupName") == 0) + group_name = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "HostAddress") == 0) + host_addr = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "GroupDeviceType") == 0) + resource_type = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "GroupType") == 0) + type = g_variant_get_int32(key_value); + } + + g_variant_iter_free(iter); + + group = _create_group_handle(uri_path, device_id, group_name, host_addr, + resource_type, type); + if (handle->group_found_cb.found_cb) + handle->group_found_cb.found_cb(type, group, handle->group_found_cb.user_data); +} + +static void _group_find_finish_cb(Group *object, gint ret, gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->group_find_finish_cb.finish_cb) + handle->group_find_finish_cb.finish_cb(ret, + handle->group_find_finish_cb.user_data); +} + +static void __device_found_cb(Group *object, gint count, GVariant *va, + gpointer user_data) +{ + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + const gchar *key; + GVariant *key_value; + companion_device_s *device = NULL; + + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + g_variant_get(va, "aa{sv}", &iter); + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + char *device_id = NULL; + char *ip = NULL; + char *device_type = NULL; + int port = -1; + + while (g_variant_iter_loop(iter_row, "{sv}", &key, &key_value)) { + if (g_strcmp0(key, "DeviceID") == 0) + device_id = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "Address") == 0) + ip = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "SecVer") == 0) + device_type = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "Port") == 0) + port = g_variant_get_uint16(key_value); + } + g_variant_iter_free(iter_row); + + device = _create_device_handle(device_id, ip, device_type, port); + if (handle->device_found_cb.found_cb) + handle->device_found_cb.found_cb(device, handle->device_found_cb.user_data); + } + g_variant_iter_free(iter); +} + +static void _device_find_finish_cb(Group *object, gint ret, gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->device_find_finish_cb.finish_cb) + handle->device_find_finish_cb.finish_cb(ret, + handle->device_find_finish_cb.user_data); +} + +static void __mowned_device_found_cb(Group *object, gint count, GVariant *va, + gpointer user_data) +{ + GVariantIter *iter = NULL; + GVariantIter *iter_row = NULL; + const gchar *key; + GVariant *key_value; + companion_device_s *device = NULL; + + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + g_variant_get(va, "aa{sv}", &iter); + while (g_variant_iter_next(iter, "a{sv}", &iter_row)) { + char *device_id = NULL; + char *ip = NULL; + char *device_type = NULL; + int port = -1; + + while (g_variant_iter_loop(iter_row, "{sv}", &key, &key_value)) { + if (g_strcmp0(key, "DeviceID") == 0) + device_id = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "Address") == 0) + ip = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "SecVer") == 0) + device_type = (char *)g_variant_get_string(key_value, NULL); + else if (g_strcmp0(key, "Port") == 0) + port = g_variant_get_uint16(key_value); + + } + g_variant_iter_free(iter_row); + + device = _create_device_handle(device_id, ip, device_type, port); + if (handle->mowned_device_found_cb.found_cb) + handle->mowned_device_found_cb.found_cb(device, + handle->mowned_device_found_cb.user_data); + } + g_variant_iter_free(iter); +} + +static void _mowned_device_find_finish_cb(Group *object, gint ret, + gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->mowned_device_find_finish_cb.finish_cb) + handle->mowned_device_find_finish_cb.finish_cb(ret, + handle->mowned_device_find_finish_cb.user_data); +} + +static void __device_invite_result_cb(Group *object, gint ret, gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->device_invite_result_cb.result_cb) + handle->device_invite_result_cb.result_cb(ret, + handle->device_invite_result_cb.user_data); +} + +static void __device_eject_result_cb(Group *object, gint ret, gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->device_eject_result_cb.result_cb) + handle->device_eject_result_cb.result_cb(ret, + handle->device_eject_result_cb.user_data); +} + +static void __send_data_finish_cb(Group *object, gchar *resp_data, gint ret, + gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->send_data_finish_cb.finish_cb) + handle->send_data_finish_cb.finish_cb(ret, resp_data, + handle->send_data_finish_cb.user_data); +} + +static void __request_result_cb(Group *object, gchar *cmd, gchar *arg, gint ret, + gpointer user_data) +{ + NOTUSED(object); + + comp_manager_s *handle = user_data; + companion_check_null_ret("user_data", user_data); + + if (handle->request_result_cb.result_cb) + handle->request_result_cb.result_cb(cmd, arg, ret, + handle->request_result_cb.user_data); +} +/* LCOV_EXCL_STOP */ + + +static int _enabler_proxy_init(comp_manager_s *handle) +{ + int ret = COMP_ERROR_NONE; + GError *error = NULL; + handle->enabler_proxy = enabler_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, COMP_DBUS_SERVICE, + COMP_DBUS_ENABLER_PATH, NULL, &error); + if (NULL == handle->enabler_proxy) { + /* LCOV_EXCL_START */ + if (error != NULL) { + _ERR("Failed to connect to the D-BUS daemon [%s]", error->message); + g_error_free(error); + } + return COMP_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ + } + return ret; +} + +/* LCOV_EXCL_START */ +static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec, + gpointer *user_data) +{ + GDBusProxy *proxy = G_DBUS_PROXY(object); + gchar *name_owner = g_dbus_proxy_get_name_owner(proxy); + comp_manager_s *handle = (comp_manager_s *)user_data; + companion_check_null_ret("user_data", user_data); + + LOGD("Name owner notify [%s]", name_owner); + + if (NULL == name_owner) + gdbus_deinitialize(handle); + free(name_owner); + +} +/* LCOV_EXCL_STOP */ + +static int _group_proxy_init(comp_manager_s *handle) +{ + int id = -1; + GError *error = NULL; + + handle->group_proxy = group_proxy_new_for_bus_sync( + G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + COMP_DBUS_SERVICE, + COMP_DBUS_GROUP_PATH, + NULL, + &error); + if (NULL == handle->group_proxy) { + /* LCOV_EXCL_START */ + if (error != NULL) { + _ERR("Failed to connect to the D-BUS daemon [%s]", error->message); + g_error_free(error); + } + return COMP_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ + } + + id = g_signal_connect(handle->group_proxy, "notify::g-name-owner", + G_CALLBACK(_dbus_name_owner_notify), handle); + if (0 == id) { + /* LCOV_EXCL_START */ + _ERR("g_signal_connect() Fail"); + g_object_unref(handle->group_proxy); + handle->group_proxy = NULL; + return COMP_ERROR_IO_ERROR; + /* LCOV_EXCL_STOP */ + } + + g_signal_connect(handle->group_proxy, + "group-found", G_CALLBACK(__group_found_cb), handle); + g_signal_connect(handle->group_proxy, + "group-find-finish", G_CALLBACK(_group_find_finish_cb), handle); + g_signal_connect(handle->group_proxy, + "device-found", G_CALLBACK(__device_found_cb), handle); + g_signal_connect(handle->group_proxy, + "device-find-finish", G_CALLBACK(_device_find_finish_cb), handle); + g_signal_connect(handle->group_proxy, + "mowned-device-found", G_CALLBACK(__mowned_device_found_cb), handle); + g_signal_connect(handle->group_proxy, + "mowned-device-find-finish", G_CALLBACK(_mowned_device_find_finish_cb), handle); + g_signal_connect(handle->group_proxy, + "device-invite-result", G_CALLBACK(__device_invite_result_cb), handle); + g_signal_connect(handle->group_proxy, + "device-eject-result", G_CALLBACK(__device_eject_result_cb), handle); + g_signal_connect(handle->group_proxy, + "send-data-finish", G_CALLBACK(__send_data_finish_cb), handle); + g_signal_connect(handle->group_proxy, + "request-result", G_CALLBACK(__request_result_cb), handle); + + return COMP_ERROR_NONE; +} + + +static void _group_proxy_deinit(comp_manager_s *handle) +{ + g_object_unref(handle->group_proxy); + handle->group_proxy = NULL; +} + +static void _enabler_proxy_deinit(comp_manager_s *handle) +{ + int ret; + + enabler_call_disable_sync(handle->enabler_proxy, &ret, NULL, NULL); + g_object_unref(handle->enabler_proxy); + handle->enabler_proxy = NULL; +} + +int gdbus_initialize(comp_manager_s *handle) +{ + int ret = COMP_ERROR_NONE; + + _group_proxy_init(handle); + _enabler_proxy_init(handle); + + if (handle->group_proxy == NULL) + ret = -1; /* LCOV_EXCL_LINE */ + + if (handle->enabler_proxy == NULL) + ret = -1; /* LCOV_EXCL_LINE */ + + handle->ca = g_cancellable_new(); + + return ret; +} + +int gdbus_deinitialize(comp_manager_s *handle) +{ + int ret = COMP_ERROR_NONE; + + _group_proxy_deinit(handle); + _enabler_proxy_deinit(handle); + + g_cancellable_cancel(handle->ca); + g_object_unref(handle->ca); + handle->ca = NULL; + + return ret; +} + diff --git a/capi/src/companion_dbus_internal.h b/capi/src/companion_dbus_internal.h new file mode 100644 index 0000000..7c7f267 --- /dev/null +++ b/capi/src/companion_dbus_internal.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2016 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 __TIZEN_NETWORK_COMMON_COMPANION_DBUS_INTERNAL_H__ +#define __TIZEN_NETWORK_COMMON_COMPANION_DBUS_INTERNAL_H__ + +#include + +int gdbus_initialize(comp_manager_s *handle); +int gdbus_deinitialize(comp_manager_s *handle); + +#endif /* __TIZEN_NETWORK_COMMON_COMPANION_DBUS_INTERNAL_H__ */ diff --git a/capi/src/companion_internal.h b/capi/src/companion_internal.h new file mode 100644 index 0000000..b121db9 --- /dev/null +++ b/capi/src/companion_internal.h @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2016 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 __TIZEN_NETWORK_COMMON_COMPANION_INTERNAL_H__ +#define __TIZEN_NETWORK_COMMON_COMPANION_INTERNAL_H__ + +#include +#include +#include +#include + +/** + * @brief New group found callback structure + * @since_tizen 5.0 + */ +typedef struct _group_found_cb_t { + companion_group_found_cb found_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} group_found_cb_t; + +/** + * @brief New device found callback structure + * @since_tizen 5.0 + */ +typedef struct _device_found_cb_t { + companion_device_found_cb found_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} device_found_cb_t; + +/** + * @brief Finding groups done callback structure + * @since_tizen 5.0 + */ +typedef struct _group_find_finish_cb_t { + companion_group_find_finish_cb finish_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} group_find_finish_cb_t; + +/** + * @brief Fiding devices done callback structure + * @since_tizen 5.0 + */ +typedef struct _device_find_finish_cb_t { + companion_device_find_finish_cb finish_cb; + void *user_data; /**< User data pointer */ +} device_find_finish_cb_t; + +/** + * @brief New my own device found callback structure + * @since_tizen 5.0 + */ +typedef struct _mowned_device_found_cb_t { + companion_device_found_cb found_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} mowned_device_found_cb_t; + +/** + * @brief Finding my own device done callback structure + * @since_tizen 5.0 + */ +typedef struct _mowned_device_find_finish_cb_t { + companion_device_find_finish_cb finish_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} mowned_device_find_finish_cb_t; + +/** + * @brief Inviting a device done callback structure + * @since_tizen 5.0 + */ +typedef struct _device_invite_result_cb_t { + companion_device_invite_result_cb result_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} device_invite_result_cb_t; + +/** + * @brief Ejecting the device done callback structure + * @since_tizen 5.0 + */ +typedef struct _device_eject_result_cb_t { + companion_device_eject_result_cb result_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} device_eject_result_cb_t; + +/** + * @brief Sending data to the device done callback structure + * @since_tizen 5.0 + */ +typedef struct _send_data_finish_cb_t { + companion_send_data_finish_cb finish_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} send_data_finish_cb_t; + +/** + * @brief Sending internal commands to the device done callback structure + * @since_tizen 5.0 + */ +typedef struct _request_result_cb_t { + companion_request_result_cb result_cb; /**< User callback to be called */ + void *user_data; /**< User data pointer */ +} request_result_cb_t; + +/** + * @brief The companion-manager context + * @since_tizen 5.0 + */ +typedef struct _comp_manager_s { + GCancellable *ca; /**< Cancelable */ + + Group *group_proxy; /**< To receive signal from companion-manager */ + Enabler *enabler_proxy; /**< Enbler proxy */ + + group_found_cb_t group_found_cb; /**< When it called after finding a every single group */ + group_find_finish_cb_t group_find_finish_cb; /**< When it called the group finging time is up */ + device_found_cb_t device_found_cb; /**< When it called after finding a every single device */ + device_find_finish_cb_t device_find_finish_cb; /**< When it called the device finging time is up */ + mowned_device_found_cb_t mowned_device_found_cb; /**< When it called after finding a every single my own device */ + mowned_device_find_finish_cb_t mowned_device_find_finish_cb; /**< When it called the time of finding my own devices is up */ + device_invite_result_cb_t device_invite_result_cb; /**< When it called after invinting a device done or timeout */ + device_eject_result_cb_t device_eject_result_cb; /**< When it called after ejecting the device done or timeout */ + send_data_finish_cb_t send_data_finish_cb; /**< When it called after sending the device done or timeout */ + request_result_cb_t request_result_cb; /**< When it called after sending private commands or timeout */ +} comp_manager_s; + +/** + * @brief The group structure + * @since_tizen 5.0 + */ +typedef struct _companion_group_s { + char *uri_path; /**< URI Path for group resource */ + char *device_id; /**< Device ID of the device has this group resource */ + char *group_name; /**< Group Name (Friendly name) */ + char *host_addr; /**< Host address */ + char *resource_type; /**< Resource type */ + companion_group_type_e type; /**< Mine or Remote */ +} companion_group_s; + +/** + * @brief The device structure + * @since_tizen 5.0 + */ +typedef struct _companion_device_s { + char *device_id; /**< Device ID */ + char *ip; /**< Device IP */ + char *device_type; /**< Device Type */ + int port; /**< Port Number */ +} companion_device_s; + + +#endif /* __TIZEN_NETWORK_COMMON_COMPANION_INTERNAL_H__ */ diff --git a/capi/unittest/companion_unit_test.cpp b/capi/unittest/companion_unit_test.cpp old mode 100644 new mode 100755 -- 2.7.4