From: Daeheyon Jung Date: Tue, 14 Aug 2018 09:02:15 +0000 (+0900) Subject: Add SendFile X-Git-Tag: submit/tizen/20190208.015210~34^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5879e679457047cb01e71d22bb74ea01a7bef95c;p=platform%2Fcore%2Fappfw%2Fcapmgr.git Add SendFile Change-Id: I41a5ec0079fb549f72b5bc4b2019976d576c4935 Signed-off-by: Daeheyon Jung --- diff --git a/src/capmgr/capmgr.cc b/src/capmgr/capmgr.cc index b752278..8597b15 100644 --- a/src/capmgr/capmgr.cc +++ b/src/capmgr/capmgr.cc @@ -79,6 +79,10 @@ bool Capmgr::Initialize() { &ConnectionManager::SendAppControl, connmgr_.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + DBusService::RegisterSendFileHandler("SendFile", std::bind( + &ConnectionManager::SendFile, connmgr_.get(), + std::placeholders::_1, std::placeholders::_2)); + return true; } diff --git a/src/common/connection_manager.h b/src/common/connection_manager.h index 451443e..356546d 100644 --- a/src/common/connection_manager.h +++ b/src/common/connection_manager.h @@ -23,6 +23,8 @@ class ConnectionManager { virtual void ExchangeCapabilities() = 0; virtual bool SendAppControl(const std::string& device_id, const unsigned char* data, size_t len) = 0; + virtual bool SendFile(const std::string& device_id, + const std::string& file_path) = 0; protected: CapabilityManager* capmgr_; diff --git a/src/common/dbus_service.cc b/src/common/dbus_service.cc index 61c9dc1..e7b1c8e 100644 --- a/src/common/dbus_service.cc +++ b/src/common/dbus_service.cc @@ -31,6 +31,10 @@ const char kDBusInstropectionXml[] = " " " " " " + " " + " " + " " + " " " " ""; const char kDBusServiceName[] = "org.tizen.capmgr"; @@ -78,6 +82,12 @@ void DBusService::RegisterSendAppcontrolHandler(const std::string& method, EventHandler().send_app_control_event.connect(handler); } +void DBusService::RegisterSendFileHandler(const std::string& method, + std::function handler) { + if (method == "SendFile") + EventHandler().send_file_event.connect(handler); +} + bool DBusService::HandleDiscoverUnownedDevices(GVariant* params, GDBusMethodInvocation* invocation) { EventHandler().on_event(); @@ -92,7 +102,7 @@ bool DBusService::HandleExchangeCapabilities(GVariant* params, bool DBusService::HandleSendRemoteAppControl(GVariant* params, GDBusMethodInvocation* invocation) { - LOG(INFO) << "HandleSendRemoteCapabilities "; + LOG(INFO) << "HandleSendRemoteAppControl "; gchar* device_id; GVariantIter* iter; guchar* data; @@ -121,6 +131,21 @@ bool DBusService::HandleSendRemoteAppControl(GVariant* params, return true; } +bool DBusService::HandleSendFile(GVariant* params, + GDBusMethodInvocation* invocation) { + LOG(INFO) << "HandleSendFile "; + gchar* device_id; + gchar* file_path; + + g_variant_get(params, "(&s&s)", &device_id, &file_path); + + LOG(INFO) << "To: " << device_id; + LOG(INFO) << "Path: " << file_path; + + EventHandler().send_file_event(device_id, file_path); + return true; +} + void DBusService::HandleMethodCall(GDBusConnection* /* connection */, const gchar* /* sender */, const gchar* /* object_path */, const gchar* /* interface_name */, const gchar* method_name, @@ -137,6 +162,9 @@ void DBusService::HandleMethodCall(GDBusConnection* /* connection */, LOG(INFO) << " method call: " << method_name; HandleSendRemoteAppControl(parameters, invocation); return; + } else if (g_strcmp0("SendFile", method_name) == 0) { + HandleSendFile(parameters, invocation); + return; } g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", r)); diff --git a/src/common/dbus_service.h b/src/common/dbus_service.h index 1ebdc12..1f3510d 100644 --- a/src/common/dbus_service.h +++ b/src/common/dbus_service.h @@ -29,6 +29,8 @@ class DBusService { static void RegisterSendAppcontrolHandler(const std::string& method, std::function handler); + static void RegisterSendFileHandler(const std::string& method, + std::function handler); private: class DBusMethodHandler { @@ -37,6 +39,8 @@ class DBusService { boost::signals2::signal on_event2; boost::signals2::signal send_app_control_event; + boost::signals2::signal send_file_event; }; static DBusMethodHandler& EventHandler() { @@ -50,6 +54,8 @@ class DBusService { GDBusMethodInvocation* invocation); bool HandleSendRemoteAppControl(GVariant* params, GDBusMethodInvocation* invocation); + bool HandleSendFile(GVariant* params, + GDBusMethodInvocation* invocation); void HandleMethodCall(GDBusConnection* connection, const gchar* sender, const gchar* object_path, diff --git a/src/common/mdg_manager.cc b/src/common/mdg_manager.cc index b5a27ae..98cb0c1 100644 --- a/src/common/mdg_manager.cc +++ b/src/common/mdg_manager.cc @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include @@ -63,6 +65,21 @@ void SendDataFinishCb(int result, mdg_device_h device, char* channel_id, << ", msg_id: " << msg_id; } +void SendFileProgressCb(const char* file_path, int64_t send_size, + int64_t total_size, int percent, void* user_data) { + LOG(INFO) << "SendFileProgressCb called. " + << "file_path: " << file_path + << "send_size: " << send_size + << "total_size: " << total_size + << "percent: " << percent; +} + +void SendFileFinishCb(int result, mdg_device_h device, + void* user_data) { + LOG(INFO) << "SendFileFinishCb called. " + << "result: " << result; +} + std::string GetDeviceIdFromHandle(const mdg_device_h device) { char* val; int ret = mdg_device_info_get_device_id(device, &val); @@ -345,10 +362,7 @@ bool MDGManager::AddDevice(const mdg_device_h device) { return true; } -bool MDGManager::SendData(const std::string& device_id, Command cmd, - const unsigned char* data, size_t len) { - LOG(INFO) << "SendData to " << device_id; - +mdg_device_h MDGManager::GetDeviceHandleFromId(const std::string& device_id) { mdg_device_h device = nullptr; for (auto& dev : GListRange(device_list_)) { char* dev_id; @@ -364,6 +378,17 @@ bool MDGManager::SendData(const std::string& device_id, Command cmd, break; } + return device; +} + +bool MDGManager::SendData(const std::string& device_id, Command cmd, + const unsigned char* data, size_t len) { + LOG(INFO) << "SendData to " << device_id; + + mdg_device_h device = nullptr; + + device = GetDeviceHandleFromId(device_id); + if (!device) { LOG(ERROR) << "There is no such device in list!"; return false; @@ -395,6 +420,30 @@ bool MDGManager::SendData(const std::string& device_id, Command cmd, return true; } +bool MDGManager::SendFile(const std::string& device_id, + const std::string& file_path) { + LOG(INFO) << "SendFile to " << device_id; + LOG(INFO) << "File: " << file_path; + + mdg_device_h device = nullptr; + + device = GetDeviceHandleFromId(device_id); + + if (!device) { + LOG(ERROR) << "There is no such device in list!"; + return false; + } + + int ret = mdg_device_send_file(mdg_handle_, device, + const_cast(file_path.c_str()), SendFileProgressCb, + SendFileFinishCb, this); + + if (ret != MDG_ERROR_NONE) + LOG(ERROR) << "Failed to send file: " << MDGErrorToString(ret); + + return true; +} + void MDGManager::FindDevices() { int ret = mdg_device_find(mdg_handle_, kRequestTimeout, false, &MDGManager::DeviceFoundCb, &MDGManager::DeviceFinishCb, this); diff --git a/src/common/mdg_manager.h b/src/common/mdg_manager.h index 793605a..a2506a0 100644 --- a/src/common/mdg_manager.h +++ b/src/common/mdg_manager.h @@ -41,6 +41,8 @@ class MDGManager : public ConnectionManager { bool AddDevice(const mdg_device_h device); bool SendData(const std::string& device_id, Command cmd, const unsigned char* data, size_t len); + bool SendFile(const std::string& device_id, const std::string& file_path); + mdg_device_h GetDeviceHandleFromId(const std::string& device_id); static void ReceiveDataCb(int result, char* device_id, char* channel_id, int msg_id, unsigned char* data, int len, void* user_data); diff --git a/tools/capmgr_test.cc b/tools/capmgr_test.cc index c518b07..b60e78a 100644 --- a/tools/capmgr_test.cc +++ b/tools/capmgr_test.cc @@ -29,9 +29,9 @@ class Client { Client(); ~Client(); - void Discover(GVariant* params); void CapExchange(GVariant* params); void SendAppControl(const std::string& device_id, const std::string& appid); + void SendFile(const std::string& device_id, const std::string& file_path); void ListDevices(); private: @@ -123,14 +123,6 @@ Client::~Client() { g_object_unref(conn_); } -void Client::Discover(GVariant* params) { - GVariant* ret = ProxyCallSync("DiscoverUnownedDevices", params); - if (!ret) - return; - - g_variant_unref(ret); -} - void Client::CapExchange(GVariant* params) { GVariant* ret = ProxyCallSync("ExchangeCapabilities", params); if (!ret) @@ -202,6 +194,33 @@ void Client::ListDevices() { << std::endl; } +void Client::SendFile(const std::string& device_id, + const std::string& file_path) { + if (device_id.empty()) { + std::cout << "Target device is missing!" << std::endl; + return; + } + + if (file_path.empty()) { + std::cout << "File path is missing!" << std::endl; + return; + } + + std::cout << "Send file " << file_path << " to " << device_id << std::endl; + + GVariant* params = g_variant_new("(ss)", device_id.c_str(), + file_path.c_str()); + if (!params) { + std::cout << "out of memory" << std::endl; + return; + } + + GVariant* ret = ProxyCallSync("SendFile", params); + + g_object_unref(params); + g_object_unref(ret); +} + GVariant* Client::ProxyCallSync(const char* method, GVariant* params) { GError* error = nullptr; GVariant* ret = g_dbus_proxy_call_sync(proxy_, method, @@ -223,18 +242,16 @@ int main(int argc, char* argv[]) { try { options.add_options() ("help,h", "help") - ("discovery,f", "discover devices&capabilities") ("capexchange,c", "capability exchange") ("device,d", bpo::value(), "device id") ("send-appcontrol,s", bpo::value(), "send remote app-control") - ("list-devices,l", "list remote devices"); + ("list-devices,l", "list remote devices") + ("send-file,f", bpo::value(), "send file"); bpo::store(bpo::parse_command_line(argc, argv, options), opt_map); if (opt_map.count("help")) std::cout << options << std::endl; - else if (opt_map.count("discovery")) - client.Discover(nullptr); else if (opt_map.count("capexchange")) client.CapExchange(nullptr); else if (opt_map.count("send-appcontrol")) @@ -242,6 +259,11 @@ int main(int argc, char* argv[]) { opt_map["send-appcontrol"].as()); else if (opt_map.count("list-devices")) client.ListDevices(); + else if (opt_map.count("send-file")) + client.SendFile(opt_map["device"].as(), + opt_map["send-file"].as()); + else + std::cout << options << std::endl; } catch (...) { std::cout << "Exception occured" << std::endl; }