From: saerome.kim Date: Wed, 28 Mar 2018 08:18:49 +0000 (+0900) Subject: capi: add feature check routine X-Git-Tag: submit/tizen/20190131.065036~78 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5c6189ec0ae803c0111bf3af56c2655b68bfa044;p=platform%2Fcore%2Fapi%2Fmulti-device-group.git capi: add feature check routine Signed-off-by: saerome.kim --- diff --git a/capi/CMakeLists.txt b/capi/CMakeLists.txt old mode 100755 new mode 100644 index 575be48..b5e0f05 --- a/capi/CMakeLists.txt +++ b/capi/CMakeLists.txt @@ -14,7 +14,8 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src) SET(SOURCES src/companion.c src/companion_gdbus.c - src/companion_dbus_internal.c) + src/companion_util.c + src/companion_dbus.c) ADD_LIBRARY(${CAPI_FN} SHARED ${SOURCES}) diff --git a/capi/src/companion.c b/capi/src/companion.c index 872ce99..1455ade 100644 --- a/capi/src/companion.c +++ b/capi/src/companion.c @@ -21,10 +21,11 @@ #include #include +#include +#include #include #include -#include -#include +#include /** * Companion Manager CAPI @@ -45,334 +46,14 @@ do { \ int ref_count = 0; /**< How many clients are alive */ bool __is_initialized = false; /**< Initialize or not */ -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 */ - EXPORT_API int companion_initialize(companion_h *handle) { int ret = COMP_ERROR_NONE; struct comp_manager_s *comp_manager = NULL; - if (NULL == handle) { - /* LCOV_EXCL_START */ - _ERR("Invalid parameter"); - return COMP_ERROR_INVALID_PARAMETER; - /* LCOV_EXCL_STOP */ - } + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); _BEGIN(); @@ -407,6 +88,8 @@ EXPORT_API int companion_deinitialize(companion_h handle) { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + _BEGIN(); COMPANION_LOCK; @@ -435,8 +118,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; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -448,6 +133,8 @@ EXPORT_API int companion_group_create(companion_h handle, char *group_name) EXPORT_API void companion_group_destroy(companion_group_s *group) { + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_check_null_ret("group", group); if (group->uri_path) { @@ -481,6 +168,8 @@ EXPORT_API int companion_group_find(companion_h handle, int timeout, int ret = COMP_ERROR_NONE; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -502,6 +191,8 @@ EXPORT_API int companion_group_get_found_groups(companion_h handle, GVariant *va = NULL; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -550,7 +241,7 @@ EXPORT_API int companion_group_get_found_groups(companion_h handle, _INFO("DeviceID %s GroupName %s HostAddress %s GroupDeviceType %s", device_id, group_name, host_addr, resource_type); - group = _create_group_handle(uri_path, + group = create_group_handle(uri_path, device_id, group_name, host_addr, resource_type, type); (*groups)[i++] = (companion_group_h)group; @@ -567,6 +258,8 @@ EXPORT_API int companion_group_join(companion_h handle, companion_group_h group, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(handle); NOTUSED(group); NOTUSED(callback); @@ -580,6 +273,8 @@ EXPORT_API int companion_group_leave(companion_h handle, companion_group_h group { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(handle); NOTUSED(group); NOTUSED(callback); @@ -593,9 +288,10 @@ EXPORT_API int companion_device_find(companion_h handle, int timeout, void *user_data) { int ret = COMP_ERROR_NONE; - GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -615,9 +311,12 @@ 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; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); _handle->mowned_device_found_cb.found_cb = found_cb; _handle->mowned_device_found_cb.user_data = user_data; @@ -634,10 +333,11 @@ EXPORT_API int companion_send_data(companion_h handle, companion_device_h device char *data, int len, companion_send_data_finish_cb finish_cb, void *user_data) { int ret = COMP_ERROR_NONE; - char *buf = NULL; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -669,10 +369,12 @@ EXPORT_API int companion_device_get_found_devices(companion_h handle, companion_device_h **devices, int *count) { int ret = COMP_ERROR_NONE; - int num = 0; GVariant *va = NULL; GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -711,7 +413,7 @@ EXPORT_API int companion_device_get_found_devices(companion_h handle, } g_variant_iter_free(iter_row); - device = _create_device_handle(deviceid, addr, ver, port); + device = create_device_handle(deviceid, addr, ver, port); (*devices)[i++] = (companion_device_h)device; } @@ -726,10 +428,12 @@ EXPORT_API int companion_device_get_found_mowned_devices( companion_h handle, companion_device_h **devices, int *count) { int ret = COMP_ERROR_NONE; - int num = 0; GVariant *va = NULL; GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -768,7 +472,7 @@ EXPORT_API int companion_device_get_found_mowned_devices( } g_variant_iter_free(iter_row); - device = _create_device_handle(deviceid, addr, ver, port); + device = create_device_handle(deviceid, addr, ver, port); (*devices)[i++] = (companion_device_h)device; } @@ -783,7 +487,6 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, companion_device_h *device) { int ret = COMP_ERROR_NONE; - GError *error = NULL; GVariant *va = NULL; GVariantIter *iter = NULL; @@ -794,6 +497,8 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, char *ver = NULL; int port = -1; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -814,7 +519,7 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, if (deviceid == NULL || addr == NULL || ver == NULL) return -1; - *device = (companion_device_h)_create_device_handle(deviceid, addr, ver, port); + *device = (companion_device_h)create_device_handle(deviceid, addr, ver, port); g_variant_iter_free(iter); @@ -823,6 +528,8 @@ EXPORT_API int companion_device_get_my_device(companion_h handle, EXPORT_API void companion_device_destroy(companion_device_h device) { + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_device_s *_device = device; companion_check_null_ret("device", device); @@ -847,6 +554,8 @@ EXPORT_API int companion_device_get_my_uuid(companion_h handle, char **uuid) int ret = 0; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -861,6 +570,8 @@ EXPORT_API int companion_group_merge(companion_h hadnle, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(dest_group); NOTUSED(src_group); @@ -871,6 +582,8 @@ EXPORT_API int companion_group_delete(companion_h handle, companion_group_h grou { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(handle); NOTUSED(group); @@ -882,6 +595,8 @@ EXPORT_API int companion_group_get_member_devices(companion_h handle, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(handle); NOTUSED(group); NOTUSED(devices); @@ -897,6 +612,8 @@ EXPORT_API int companion_device_invite(companion_h handle, int ret = COMP_ERROR_NONE; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -928,6 +645,8 @@ EXPORT_API int companion_device_eject(companion_h handle, int ret = COMP_ERROR_NONE; GError *error = NULL; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -952,6 +671,8 @@ EXPORT_API int companion_group_information_create(companion_group_h* group) { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_group_h _group = g_malloc0(sizeof(companion_group_s)); if (NULL == _group) { _ERR("Memory allocation Failed(%d)", errno); @@ -967,10 +688,12 @@ EXPORT_API int companion_group_information_clone(companion_group_h target, { int ret = COMP_ERROR_NONE; - companion_group_s * dst = (companion_group_s *)target; - companion_group_s * src = (companion_group_s *)source; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_group_s * dst = (companion_group_s *)target; companion_check_null_ret_error("target", target, COMP_ERROR_INVALID_PARAMETER); + + companion_group_s * src = (companion_group_s *)source; companion_check_null_ret_error("source", source, COMP_ERROR_INVALID_PARAMETER); if (src->uri_path) @@ -997,6 +720,8 @@ EXPORT_API int companion_group_information_destroy(companion_group_h data) { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_group_s * group = (companion_group_s *)data; companion_check_null_ret_error("group", group, COMP_ERROR_INVALID_PARAMETER); @@ -1031,6 +756,8 @@ EXPORT_API int companion_group_information_get_type(companion_group_h group, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *type = ((companion_group_s *)group)->type; return ret; @@ -1041,6 +768,8 @@ EXPORT_API int companion_group_information_get_resource_type( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *resource_type = g_strdup(((companion_group_s *)group)->resource_type); return ret; @@ -1051,6 +780,8 @@ EXPORT_API int companion_group_information_get_uri_path( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *uri_path = g_strdup(((companion_group_s *)group)->uri_path); return ret; @@ -1061,6 +792,8 @@ EXPORT_API int companion_group_information_get_name( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *name = g_strdup(((companion_group_s *)group)->group_name); return ret; @@ -1071,6 +804,8 @@ EXPORT_API int companion_group_information_get_host_addr { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *host_addr = g_strdup(((companion_group_s *)group)->host_addr); return ret; @@ -1080,6 +815,8 @@ EXPORT_API int companion_device_information_create(companion_device_h* device) { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_device_h _device = g_malloc0(sizeof(companion_device_s)); if (NULL == device) { _ERR("Memory allocation Failed(%d)", errno); @@ -1095,10 +832,12 @@ EXPORT_API int companion_device_information_clone(companion_device_h target, { int ret = COMP_ERROR_NONE; - companion_device_s * dst = (companion_device_s *)target; - companion_device_s * src = (companion_device_s *)source; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_device_s * dst = (companion_device_s *)target; companion_check_null_ret_error("target", target, COMP_ERROR_INVALID_PARAMETER); + + companion_device_s * src = (companion_device_s *)source; companion_check_null_ret_error("source", source, COMP_ERROR_INVALID_PARAMETER); if (src->device_id) @@ -1114,6 +853,9 @@ EXPORT_API int companion_device_information_clone(companion_device_h target, EXPORT_API int companion_device_information_destroy(companion_device_h data) { int ret = COMP_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + companion_device_s * device = (companion_device_s *)data; companion_check_null_ret_error("data", data, COMP_ERROR_INVALID_PARAMETER); @@ -1141,6 +883,8 @@ EXPORT_API int companion_device_information_get_device_id( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *device_id = g_strdup(((companion_device_s *)device)->device_id); return ret; @@ -1151,6 +895,8 @@ EXPORT_API int companion_device_information_get_ip( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *ip = g_strdup(((companion_device_s *)device)->ip); return ret; @@ -1161,6 +907,8 @@ EXPORT_API int companion_device_information_get_device_type( { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + *device_type = g_strdup(((companion_device_s *)device)->device_type); return ret; @@ -1171,8 +919,15 @@ EXPORT_API int companion_request_create_group(companion_h handle, { int ret = COMP_ERROR_NONE; GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; + companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); + companion_device_s *dev = (companion_device_s *)device; + companion_check_null_ret_error("device", device, COMP_ERROR_INVALID_PARAMETER); + companion_check_null_ret_error("group_name", group_name, COMP_ERROR_INVALID_PARAMETER); _DBG("Device id : %s", dev->device_id); @@ -1187,28 +942,23 @@ EXPORT_API int companion_request_invite(companion_h handle, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); companion_group_s *grp = (companion_group_s *)group; - if (!grp) { - _ERR("grp is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("group", group, COMP_ERROR_INVALID_PARAMETER); companion_device_s *dev = (companion_device_s *)device; - if (!dev) { - _ERR("dev is null"); - return COMP_ERROR_INVALID_PARAMETER; - } + companion_check_null_ret_error("device", device, COMP_ERROR_INVALID_PARAMETER); _DBG("%s", grp->device_id); _DBG("%s", grp->group_name); _DBG("%s", dev->device_id); group_call_request_invite(_handle->group_proxy, grp->device_id, grp->group_name, - dev->device_id, PIN, - NULL, NULL, NULL); + dev->device_id, PIN, NULL, NULL, NULL); return ret; } @@ -1218,6 +968,8 @@ EXPORT_API int companion_request_eject(companion_h handle, { int ret = COMP_ERROR_NONE; + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); @@ -1241,6 +993,9 @@ EXPORT_API int companion_request_delete_group(companion_h handle, companion_group_h group) { int ret = COMP_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + NOTUSED(handle); NOTUSED(group); return ret; @@ -1250,6 +1005,9 @@ EXPORT_API int companion_request_result_callback(companion_h handle, companion_request_result_cb result_cb, void *user_data) { int ret = COMP_ERROR_NONE; + + CHECK_FEATURE_SUPPORTED(COMPANION_FEATURE); + comp_manager_s *_handle = handle; companion_check_null_ret_error("handle", handle, COMP_ERROR_INVALID_PARAMETER); diff --git a/capi/src/companion_dbus.c b/capi/src/companion_dbus.c new file mode 100644 index 0000000..7a7a43f --- /dev/null +++ b/capi/src/companion_dbus.c @@ -0,0 +1,391 @@ +/* + * 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 +#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 */ + +/* 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.h b/capi/src/companion_dbus.h new file mode 100644 index 0000000..b77b296 --- /dev/null +++ b/capi/src/companion_dbus.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_dbus_internal.c b/capi/src/companion_dbus_internal.c deleted file mode 100644 index ebf8feb..0000000 --- a/capi/src/companion_dbus_internal.c +++ /dev/null @@ -1,494 +0,0 @@ -/* - * 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 deleted file mode 100644 index 7c7f267..0000000 --- a/capi/src/companion_dbus_internal.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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_debug.h b/capi/src/companion_debug.h index 10f10dc..7ab8293 100644 --- a/capi/src/companion_debug.h +++ b/capi/src/companion_debug.h @@ -18,7 +18,7 @@ #ifndef __TIZEN_NETWORK_COMMON_COMPANION_DEBUG_H__ #define __TIZEN_NETWORK_COMMON_COMPANION_DEBUG_H__ -#include +#include #define NOTUSED(var) (var = var) diff --git a/capi/src/companion_internal.h b/capi/src/companion_internal.h deleted file mode 100644 index b121db9..0000000 --- a/capi/src/companion_internal.h +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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/src/companion_private.h b/capi/src/companion_private.h new file mode 100644 index 0000000..c0f8256 --- /dev/null +++ b/capi/src/companion_private.h @@ -0,0 +1,193 @@ +/* + * 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_PRIVATE_H__ +#define __TIZEN_NETWORK_COMMON_COMPANION_PRIVATE_H__ + +#include +#include +#include +#include +#include + +#include + +#define COMPANION_FEATURE "http://tizen.org/feature/network.companion" + +#define CHECK_INPUT_PARAMETER(arg) \ + if (arg == NULL) { \ + comp_supported("INVALID_PARAMETER"); \ + return COMP_ERROR_INVALID_PARAMETER; \ + } + +#if 1 +#define CHECK_FEATURE_SUPPORTED(feature_name) { \ + bool comp_supported = FALSE; \ + if (!system_info_get_platform_bool(feature_name, &comp_supported)) { \ + if (comp_supported == FALSE) { \ + _ERR("companion feature is disabled"); \ + return COMP_ERROR_NOT_SUPPORTED; \ + } \ + } else { \ + _ERR("Error - Feature getting from System Info"); \ + return COMP_ERROR_NOT_SUPPORTED; \ + } \ +} +#else +#define CHECK_FEATURE_SUPPORTED(feature_name) { \ + _ERR("[Feature] Should be check !"); \ + } +#endif + +/** + * @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_PRIVATE_H__ */ diff --git a/capi/src/companion_util.c b/capi/src/companion_util.c new file mode 100644 index 0000000..497735d --- /dev/null +++ b/capi/src/companion_util.c @@ -0,0 +1,125 @@ +/* + * 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 + +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 */ +} + +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 */ +} diff --git a/capi/src/companion_util.h b/capi/src/companion_util.h new file mode 100644 index 0000000..db7314e --- /dev/null +++ b/capi/src/companion_util.h @@ -0,0 +1,28 @@ +/* + * 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_UTIL_H__ +#define __TIZEN_NETWORK_COMMON_COMPANION_UTIL_H__ + +#include + +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_device_s *create_device_handle(char *device_id, char *ip, + char *device_type, int port); + +#endif /* __TIZEN_NETWORK_COMMON_COMPANION_UTIL_H__ */ diff --git a/capi/unittest/companion_unit_test.cpp b/capi/unittest/companion_unit_test.cpp old mode 100755 new mode 100644