From: Jihoon Jung Date: Thu, 26 Jul 2018 09:19:47 +0000 (+0900) Subject: Add send file implmentation X-Git-Tag: submit/tizen/20190131.065036~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2055e051fc04b59e0cf5d9acd7bd0dd319b34d8b;p=platform%2Fcore%2Fapi%2Fmulti-device-group.git Add send file implmentation Change-Id: Id9e379896ef3ae2f73f915eab87d213d5931288e Signed-off-by: Jihoon Jung --- diff --git a/include/mdg.h b/include/mdg.h index e491f82..3b437c0 100755 --- a/include/mdg.h +++ b/include/mdg.h @@ -801,9 +801,6 @@ int mdg_device_regist_channel(mdg_h handle, char *channel_id, int mdg_device_unregist_channel(mdg_h handle, char *channel_id); /* Client Side */ -//int mdg_device_get_channels(mdg_h handle, mdg_device_h device, -// mdg_device_get_channels_cb get_channels_cb, void *user_data); - /** * @brief Sends data to the remote device. * @details We can send data to devices included in group. @@ -835,6 +832,13 @@ int mdg_device_send_data(mdg_h handle, mdg_device_h device, char *channel_id, unsigned char *data, int len, mdg_device_send_data_finish_cb finish_cb, void *user_data); +typedef void (*mdg_send_file_progress_cb)(const char *file_path, long send_size, + long total_size, int percent, void *user_data); +typedef void (*mdg_send_file_finish_cb)(int result, mdg_device_h device, void *user_data); + +int mdg_device_send_file(mdg_h handle, mdg_device_h device, char *file_path, + mdg_send_file_progress_cb progress_cb, mdg_send_file_finish_cb finish_cb, void *user_data); + /** * @} */ diff --git a/src/mdg.c b/src/mdg.c index 5ac3e5b..ef1a477 100755 --- a/src/mdg.c +++ b/src/mdg.c @@ -324,6 +324,41 @@ EXPORT_API int mdg_device_send_data(mdg_h handle, return ret; } +EXPORT_API int mdg_device_send_file(mdg_h handle, mdg_device_h device, + char *file_path, mdg_send_file_progress_cb progress_cb, mdg_send_file_finish_cb finish_cb, void *user_data) +{ + int ret = MDG_ERROR_NONE; + GError *error = NULL; + + CHECK_FEATURE_SUPPORTED(MDG_FEATURE); + + mdg_manager_s *_handle = handle; + mdg_check_null_ret_error("handle", handle, MDG_ERROR_INVALID_PARAMETER); + CHECK_HANDLE_IS_VALID(handle); + + mdg_device_s *dev = (mdg_device_s *)device; + mdg_check_null_ret_error("dev", dev, MDG_ERROR_INVALID_PARAMETER); + mdg_check_null_ret_error("file_path", file_path, MDG_ERROR_INVALID_PARAMETER); + + mdg_device_h cloned_device = NULL; + mdg_device_info_clone(&cloned_device, device); + + _handle->send_file_cb.finish_cb = finish_cb; + _handle->send_file_cb.progress_cb = progress_cb; + _handle->send_file_cb.device = cloned_device; + _handle->send_file_cb.user_data = user_data; + + group_call_send_file_sync(_handle->group_proxy, dev->device_id, dev->addr, + dev->secure_port, file_path, &ret, NULL, &error); + if (error) { + _ERR("Failed DBus call [%s]", error->message); + g_error_free(error); + ret = MDG_ERROR_IO_ERROR; + } + + return ret; +} + EXPORT_API int mdg_device_get_local_device(mdg_h handle, mdg_device_h *device) { diff --git a/src/mdg_dbus.c b/src/mdg_dbus.c index bd304de..154f6c1 100755 --- a/src/mdg_dbus.c +++ b/src/mdg_dbus.c @@ -41,7 +41,9 @@ static void __event_cb(Group *object, _DBG("Event occured : %d", event_type); - if (event_type == MDG_EVENT_GROUP_FOUND) { + switch (event_type) { + case MDG_EVENT_GROUP_FOUND: + { mdg_group_s *group = NULL; group = mdg_get_group_from_variant(va); @@ -51,14 +53,20 @@ static void __event_cb(Group *object, } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_GROUP_FIND_FINISH) { + break; + } + case MDG_EVENT_GROUP_FIND_FINISH: + { if (handle->group_find_finish_cb.finish_cb) { handle->group_find_finish_cb.finish_cb(ret, handle->group_find_finish_cb.user_data); } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_DEVICE_FOUND) { + break; + } + case MDG_EVENT_DEVICE_FOUND: + { mdg_device_s *device = NULL; device = mdg_get_device_from_variant(va); @@ -68,14 +76,20 @@ static void __event_cb(Group *object, } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_DEVICE_FIND_FINISH) { + break; + } + case MDG_EVENT_DEVICE_FIND_FINISH: + { if (handle->device_find_finish_cb.finish_cb) { handle->device_find_finish_cb.finish_cb(ret, handle->device_find_finish_cb.user_data); } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_INVITE_DEVICE_FINISH) { + break; + } + case MDG_EVENT_INVITE_DEVICE_FINISH: + { if (handle->device_invite_finish_cb.finish_cb) { mdg_device_s *device = (mdg_device_s *)(handle->device_invite_finish_cb.device); if (device != NULL) { @@ -86,21 +100,30 @@ static void __event_cb(Group *object, } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_EJECT_DEVICE_FINISH) { + break; + } + case MDG_EVENT_EJECT_DEVICE_FINISH: + { if (handle->device_eject_finish_cb.finish_cb) { handle->device_eject_finish_cb.finish_cb(ret, handle->device_eject_finish_cb.user_data); } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_SEND_DATA_FINISH) { + break; + } + case MDG_EVENT_SEND_DATA_FINISH: + { if (handle->send_data_finish_cb.finish_cb) { handle->send_data_finish_cb.finish_cb(ret, handle->send_data_finish_cb.user_data); } else { _ERR("The callback not exists"); } - } else if (event_type == MDG_EVENT_REQ_CHANNEL_LIST_FINISH) { + break; + } + case MDG_EVENT_REQ_CHANNEL_LIST_FINISH: + { char *device_id; char *channel_id; @@ -111,7 +134,10 @@ static void __event_cb(Group *object, } else { _ERR("The callback not exists"); } - } else if (event_type == MDGD_EVENT_RECEIVE_DATA) { + break; + } + case MDG_EVENT_RECEIVE_DATA: + { char *device_id; char *channel_id; unsigned char *data; @@ -130,9 +156,35 @@ static void __event_cb(Group *object, if (channel != NULL) channel->cb(0, device_id, channel_id, data, data_len, channel->user_data); - - } else { + break; + } + case MDG_EVENT_SEND_FILE_PROGRESS: + { + if (handle->send_file_cb.progress_cb) { + int percent = 0; + long send_size, total_size; + char *file_path; + mdg_get_progress_from_variant(va, &file_path, &send_size, &total_size, &percent); + handle->send_file_cb.progress_cb(file_path, send_size, total_size, percent, + handle->send_file_cb.user_data); + } else { + _ERR("The callback not exists"); + } + break; + } + case MDG_EVENT_SEND_FILE_FINISH: + { + if (handle->send_file_cb.finish_cb) { + mdg_device_s *device = (mdg_device_s *)(handle->send_file_cb.device); + handle->send_file_cb.finish_cb(ret, device, handle->send_file_cb.user_data); + } else { + _ERR("The callback not exists"); + } + break; + } + default: _ERR("Unknown Event"); + break; } _END(); diff --git a/src/mdg_gdbus.xml b/src/mdg_gdbus.xml index a2c466e..4424848 100755 --- a/src/mdg_gdbus.xml +++ b/src/mdg_gdbus.xml @@ -65,6 +65,13 @@ + + + + + + + diff --git a/src/mdg_private.h b/src/mdg_private.h index 1ea0eae..8f0774e 100755 --- a/src/mdg_private.h +++ b/src/mdg_private.h @@ -119,6 +119,17 @@ typedef struct _send_data_finish_cb_t { } send_data_finish_cb_t; /** + * @brief Sending data to the device done callback structure + * @since_tizen 5.0 + */ +typedef struct _send_file_cb_t { + mdg_send_file_progress_cb progress_cb; /**< User callback to be called */ + mdg_send_file_finish_cb finish_cb; /**< User callback to be called */ + mdg_device_h device; + void *user_data; /**< User data pointer */ +} send_file_cb_t; + +/** * @brief Sending internal commands to the device done callback structure * @since_tizen 5.0 */ @@ -149,6 +160,7 @@ typedef struct _mdg_manager_s { device_invite_finish_cb_t device_invite_finish_cb; /**< When it called after invinting a device done or timeout */ device_eject_finish_cb_t device_eject_finish_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 */ + send_file_cb_t send_file_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 */ request_channel_list_finish_cb_t request_channel_list_finish_cb; /**< When it called after sending private commands or timeout */ } mdg_manager_s; @@ -204,7 +216,10 @@ typedef enum { MDG_EVENT_REQUEST_FINISH, MDG_EVENT_SEND_DATA_FINISH, MDG_EVENT_REQ_CHANNEL_LIST_FINISH, - MDGD_EVENT_RECEIVE_DATA, + MDG_EVENT_RECEIVE_DATA, + MDG_EVENT_RECEIVE_FILE = 10, + MDG_EVENT_SEND_FILE_PROGRESS, + MDG_EVENT_SEND_FILE_FINISH, } mdg_event_type_e; #endif /* __TIZEN_NETWORK_COMMON_MDG_PRIVATE_H__ */ diff --git a/src/mdg_util.c b/src/mdg_util.c index fcbf729..3291cc5 100755 --- a/src/mdg_util.c +++ b/src/mdg_util.c @@ -148,6 +148,7 @@ void mdg_get_channel_from_variant(GVariant *va, char **device_id, char **channel g_variant_iter_free(iter); } + void mdg_get_data_from_variant(GVariant *va, char **device_id, char **channel_id, unsigned char **data, int *data_len) { @@ -180,6 +181,28 @@ void mdg_get_data_from_variant(GVariant *va, char **device_id, char **channel_id g_variant_iter_free(iter); } +void mdg_get_progress_from_variant(GVariant *va, char **file_path, + long *send_size, long *total_size, int *percent) +{ + GVariantIter *iter = NULL; + const gchar *key; + GVariant *key_value = NULL; + + g_variant_get(va, "a{sv}", &iter); + while (g_variant_iter_loop(iter, "{sv}", &key, &key_value)) { + if (g_strcmp0(key, "FilePath") == 0) + *file_path = (char *)g_variant_get_string(key_value, NULL); + if (g_strcmp0(key, "SendSize") == 0) + *send_size = g_variant_get_int64(key_value); + if (g_strcmp0(key, "TotalSize") == 0) + *total_size = g_variant_get_int64(key_value); + if (g_strcmp0(key, "Percent") == 0) + *percent = g_variant_get_int32(key_value); + } + + g_variant_iter_free(iter); +} + GVariant *mdg_create_variant_device(mdg_device_s *device) { GVariant *va; diff --git a/src/mdg_util.h b/src/mdg_util.h index 0a5e141..df46e2e 100755 --- a/src/mdg_util.h +++ b/src/mdg_util.h @@ -31,6 +31,8 @@ mdg_group_s *mdg_get_group_from_variant(GVariant *va); void mdg_get_channel_from_variant(GVariant *va, char **device_id, char **channel_id); void mdg_get_data_from_variant(GVariant *va, char **device_id, char **channel_id, unsigned char **data, int *data_len); +void mdg_get_progress_from_variant(GVariant *va, char **file_path, + long *send_size, long *total_size, int *percent); #ifdef __cplusplus } diff --git a/test/mdg-manager.c b/test/mdg-manager.c index a131433..9637951 100755 --- a/test/mdg-manager.c +++ b/test/mdg-manager.c @@ -848,13 +848,27 @@ static int run_send_data(MManager *mm, struct menu_data *menu) return __send_data(devidx, chaidx); } -static int __send_file(int devidx, int chaidx) +void __send_file_progress_cb(const char *file_path, long send_size, + long total_size, int percent, void *user_data) +{ + msgp("%s %d%% [%ld/%ld]", file_path, percent, send_size, total_size); +} +void __send_file_finish_cb(int result, mdg_device_h device, void *user_data) +{ + char *addr; + + mdg_device_info_get_addr(device, &addr); + + msgp("Send file to %s Finished [%s]", addr, mdg_error_to_string(result)); +} + +static int __send_file(int devidx) { int ret = 0; char *deviceid = NULL; char *address = NULL; mdg_device_h device = NULL; - channel_t *channel = NULL; + if (found_invited_device_list) { device = g_list_nth_data(found_invited_device_list, devidx - 1); if (NULL == device) { @@ -863,17 +877,9 @@ static int __send_file(int devidx, int chaidx) } } - if (found_channel_list) { - channel = g_list_nth_data(found_channel_list, chaidx - 1); - if (NULL == channel) { - msgr("Find local device first"); - return RET_FAILURE; - } - } - mdg_device_info_get_device_id(device, &deviceid); mdg_device_info_get_addr(device, &address); - msgp("Sent to [ID] %s [ADDR] %s [CHANNEL ID] %s", deviceid, address, channel->channel_id); + msgp("Sent to [ID] %s [ADDR] %s", deviceid, address); if (deviceid) { free(deviceid); deviceid = NULL; @@ -883,8 +889,8 @@ static int __send_file(int devidx, int chaidx) address = NULL; } - ret = mdg_device_send_data(handle, device, channel->channel_id, (unsigned char *)file_path, - strlen(file_path), _send_data_finish_cb, NULL); + ret = mdg_device_send_file(handle, device, file_path, + __send_file_progress_cb, __send_file_finish_cb, NULL); if (MDG_ERROR_NONE != ret) msgr("Failed to Send Data: [ID] %s [IP] %s", deviceid, address); @@ -894,7 +900,6 @@ static int __send_file(int devidx, int chaidx) static int run_send_file(MManager *mm, struct menu_data *menu) { int devidx = 0; - int chaidx = 0; int count = g_list_length(found_invited_device_list); if (0 >= count) { @@ -909,14 +914,8 @@ static int run_send_file(MManager *mm, struct menu_data *menu) devidx = 1; } } - if (strlen(channel_idx)) { - chaidx = (unsigned short)strtol(channel_idx, NULL, 10); - if (0 >= chaidx) { - msgp("Invalid index. set to 1"); - chaidx = 1; - } - } - return __send_file(devidx, chaidx); + + return __send_file(devidx); } void _invited_device_finish_cb(int result, void *user_data) @@ -1294,11 +1293,9 @@ static struct menu_data menu_send_data[] = { static struct menu_data menu_send_file[] = { { "0", "Show Found Invited Device(s)", NULL, run_invited_devices_show, NULL }, - { "1", "Show Channel List", NULL, run_channels_show, NULL }, - { "2", "File Path", NULL, NULL, file_path }, - { "3", "Device Index", NULL, NULL, device_idx }, - { "4", "Channel Index", NULL, NULL, channel_idx }, - { "5", "Send", NULL, run_send_file, NULL }, + { "1", "File Path", NULL, NULL, file_path }, + { "2", "Device Index", NULL, NULL, device_idx }, + { "3", "Send", NULL, run_send_file, NULL }, { NULL, NULL, }, };