Add SendFile
authorDaeheyon Jung <darrenh.jung@samsung.com>
Tue, 14 Aug 2018 09:02:15 +0000 (18:02 +0900)
committerDaeheyon Jung <darrenh.jung@samsung.com>
Tue, 14 Aug 2018 09:02:15 +0000 (18:02 +0900)
Change-Id: I41a5ec0079fb549f72b5bc4b2019976d576c4935
Signed-off-by: Daeheyon Jung <darrenh.jung@samsung.com>
src/capmgr/capmgr.cc
src/common/connection_manager.h
src/common/dbus_service.cc
src/common/dbus_service.h
src/common/mdg_manager.cc
src/common/mdg_manager.h
tools/capmgr_test.cc

index b7522780885bf12524e8d2aa8e1d9407df309bd9..8597b155c02667ee64dba03089f737973fc524c4 100644 (file)
@@ -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;
 }
 
index 451443edd51e7669389f21b13d32290e3426caf1..356546de51a48aecfac2827a872f379bb89cb78d 100644 (file)
@@ -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_;
index 61c9dc1819781ed591e1002a15eb7e0fb730325f..e7b1c8ebef2ada98c4876c11b36e8f1a4fd38bd5 100644 (file)
@@ -31,6 +31,10 @@ const char kDBusInstropectionXml[] =
   "      <arg type='u' name='len' direction='in'/>"
   "      <arg type='b' name='result' direction='out'/>"
   "    </method>"
+  "    <method name='SendFile'>"
+  "      <arg type='s' name='device_id' direction='in'/>"
+  "      <arg type='s' name='file_path' direction='in'/>"
+  "    </method>"
   "  </interface>"
   "</node>";
 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<bool(const std::string&, const std::string&)> 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));
index 1ebdc129bf0b67f56e840f1d920c4623b8ad0c62..1f3510d5b87a92b86691d4748327252de68caed1 100644 (file)
@@ -29,6 +29,8 @@ class DBusService {
   static void RegisterSendAppcontrolHandler(const std::string& method,
       std::function<bool(const std::string&, const unsigned char*,
           size_t)> handler);
+  static void RegisterSendFileHandler(const std::string& method,
+      std::function<bool(const std::string&, const std::string&)> handler);
 
  private:
   class DBusMethodHandler {
@@ -37,6 +39,8 @@ class DBusService {
     boost::signals2::signal<void()> on_event2;
     boost::signals2::signal<bool(const std::string&, const unsigned char*,
         size_t)> send_app_control_event;
+    boost::signals2::signal<void(const std::string&,
+        const std::string&)> 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,
index b5a27ae8f591a413df0a64e7cc42fa0a9e7bf27b..98cb0c19804d7604763fe045aa6218c1f8a81e3f 100644 (file)
@@ -6,6 +6,8 @@
 
 #include <glib.h>
 #include <mdg.h>
+#include <mdg_internal.h>
+#include <stdint.h>
 #include <sys/types.h>
 #include <unistd.h>
 
@@ -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<mdg_device_h>(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<char*>(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);
index 793605abe928e001d83abc449ecdfab3f7a81694..a2506a08e19475a34926c34048c6b9cbb94d8528 100644 (file)
@@ -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);
index c518b07ff6d19613a48b51b2b97c60c745fa571a..b60e78ac64b325c4b9b47f379a57611c97d46008 100644 (file)
@@ -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<std::string>(), "device id")
         ("send-appcontrol,s", bpo::value<std::string>(),
             "send remote app-control")
-        ("list-devices,l", "list remote devices");
+        ("list-devices,l", "list remote devices")
+        ("send-file,f", bpo::value<std::string>(), "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<std::string>());
     else if (opt_map.count("list-devices"))
       client.ListDevices();
+    else if (opt_map.count("send-file"))
+      client.SendFile(opt_map["device"].as<std::string>(),
+          opt_map["send-file"].as<std::string>());
+    else
+      std::cout << options << std::endl;
   } catch (...) {
     std::cout << "Exception occured" << std::endl;
   }