blob: daec2a7e2168dde0448be72eb4e4fe705eee58a9 [file] [log] [blame]
Lei Zhangf0ab3ae2012-08-03 18:02:201// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MTPD_SERVER_IMPL_H_
6#define MTPD_SERVER_IMPL_H_
7
8#include <string>
9#include <vector>
10
Lei Zhangf0ab3ae2012-08-03 18:02:2011#include <base/compiler_specific.h>
Alex Vakulenkoef2d18d2016-01-23 03:20:1412#include <base/macros.h>
Eric Caruso2e3c3ca2017-08-23 00:18:4013#include <brillo/dbus/async_event_sequencer.h>
14#include <brillo/dbus/dbus_object.h>
15#include <brillo/errors/error.h>
16#include <dbus/bus.h>
Lei Zhangf0ab3ae2012-08-03 18:02:2017
Eric Caruso34eeec52017-08-21 22:31:1918#include "mtpd/dbus_adaptors/org.chromium.Mtpd.h"
Eric Carusoe7cd2682017-08-21 22:57:3019#include "mtpd/device_event_delegate.h"
20#include "mtpd/device_manager.h"
21#include "mtpd/file_entry.h"
Lei Zhangf0ab3ae2012-08-03 18:02:2022
23namespace mtpd {
24
25class DeviceManager;
26
27// The D-bus server for the mtpd daemon.
Eric Caruso2e3c3ca2017-08-23 00:18:4028class MtpdServer : public org::chromium::MtpdInterface,
29 public org::chromium::MtpdAdaptor,
Lei Zhang0e5015f2012-08-07 00:05:3330 public DeviceEventDelegate {
Lei Zhangf0ab3ae2012-08-03 18:02:2031 public:
Eric Caruso2e3c3ca2017-08-23 00:18:4032 explicit MtpdServer(scoped_refptr<dbus::Bus> bus);
Lei Zhangf0ab3ae2012-08-03 18:02:2033 virtual ~MtpdServer();
34
Eric Caruso2e3c3ca2017-08-23 00:18:4035 // org::chromium::MtpdAdaptor implementation.
36 std::vector<std::string> EnumerateStorages() override;
37 std::vector<uint8_t> GetStorageInfo(const std::string& storage_name) override;
38 std::vector<uint8_t> GetStorageInfoFromDevice(
39 const std::string& storage_name) override;
40 bool OpenStorage(brillo::ErrorPtr* error,
41 const std::string& storage_name,
42 const std::string& mode,
43 std::string* id) override;
44 bool CloseStorage(brillo::ErrorPtr* error,
45 const std::string& handle) override;
46 bool ReadDirectoryEntryIds(brillo::ErrorPtr* error,
47 const std::string& handle,
48 uint32_t file_id,
49 std::vector<uint32_t>* directory_listing) override;
50 bool GetFileInfo(brillo::ErrorPtr* error,
51 const std::string& handle,
52 const std::vector<uint32_t>& file_ids,
53 std::vector<uint8_t>* serialized_file_entries) override;
54 bool ReadFileChunk(brillo::ErrorPtr* error,
55 const std::string& handle,
56 uint32_t file_id,
57 uint32_t offset,
58 uint32_t count,
59 std::vector<uint8_t>* file_contents) override;
60 bool CopyFileFromLocal(brillo::ErrorPtr* error,
61 const std::string& handle,
62 const dbus::FileDescriptor& file_descriptor,
63 uint32_t parent_id,
64 const std::string& file_name) override;
65 bool DeleteObject(brillo::ErrorPtr* error,
66 const std::string& handle,
67 uint32_t object_id) override;
68 bool RenameObject(brillo::ErrorPtr* error,
69 const std::string& handle,
70 uint32_t object_id,
71 const std::string& new_name) override;
72 bool CreateDirectory(brillo::ErrorPtr* error,
73 const std::string& handle,
74 uint32_t parent_id,
75 const std::string& directory_name) override;
76 bool IsAlive() override;
Lei Zhangf0ab3ae2012-08-03 18:02:2077
Lei Zhang0e5015f2012-08-07 00:05:3378 // DeviceEventDelegate implementation.
Alex Vakulenkocd0d8a42014-12-11 15:53:4479 void StorageAttached(const std::string& storage_name) override;
80 void StorageDetached(const std::string& storage_name) override;
Lei Zhang0e5015f2012-08-07 00:05:3381
Lei Zhangf0ab3ae2012-08-03 18:02:2082 // Returns a file descriptor for monitoring device events.
83 int GetDeviceEventDescriptor() const;
84
85 // Processes the available device events.
86 void ProcessDeviceEvents();
87
Eric Caruso2e3c3ca2017-08-23 00:18:4088 // Register D-Bus object.
89 void RegisterAsync(
90 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
91
Lei Zhangf0ab3ae2012-08-03 18:02:2092 private:
Yuki Awano282c81f2015-02-24 04:37:4093 // StorageHandleInfo is a pair of StorageName and Mode.
Lei Zhangcdf3a942017-04-27 20:50:3094 using StorageHandleInfo = std::pair<std::string, std::string>;
Yuki Awano282c81f2015-02-24 04:37:4095
96 // Handle to StorageHandleInfo map.
Lei Zhangcdf3a942017-04-27 20:50:3097 using HandleMap = std::map<std::string, StorageHandleInfo>;
Lei Zhang6eaedbf2012-08-07 22:22:0598
Lei Zhang442d0e22012-08-17 05:52:4999 // Returns the StorageName for a handle, or an empty string on failure.
100 std::string LookupHandle(const std::string& handle);
101
Yuki Awano282c81f2015-02-24 04:37:40102 // Returns true if the storage is opened with write access.
103 bool IsOpenedWithWrite(const std::string& handle);
104
Lei Zhang6eaedbf2012-08-07 22:22:05105 HandleMap handle_map_;
106
Eric Caruso2e3c3ca2017-08-23 00:18:40107 // Exported D-Bus object.
108 brillo::dbus_utils::DBusObject dbus_object_;
109
Lei Zhang6eaedbf2012-08-07 22:22:05110 // Device manager needs to be last, so it is the first to be destroyed.
Lei Zhang0e5015f2012-08-07 00:05:33111 DeviceManager device_manager_;
112
Lei Zhangf0ab3ae2012-08-03 18:02:20113 DISALLOW_COPY_AND_ASSIGN(MtpdServer);
114};
115
116} // namespace mtpd
117
118#endif // MTPD_SERVER_IMPL_H_