Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 1 | // 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 | |
Eric Caruso | e7cd268 | 2017-08-21 22:57:30 | [diff] [blame] | 5 | #include "mtpd/storage_info.h" |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 6 | |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 7 | #include <base/logging.h> |
Lei Zhang | 13b4103 | 2017-04-22 06:00:45 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
Lei Zhang | a3d6d0a | 2012-08-06 20:29:27 | [diff] [blame] | 9 | |
Eric Caruso | e7cd268 | 2017-08-21 22:57:30 | [diff] [blame] | 10 | #include "mtpd/string_helpers.h" |
Eric Caruso | 34eeec5 | 2017-08-21 22:31:19 | [diff] [blame] | 11 | #include "system_api/proto_bindings/mtp_storage_info.pb.h" |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 12 | |
| 13 | namespace mtpd { |
| 14 | |
Lei Zhang | 05dbd32 | 2012-09-11 21:58:13 | [diff] [blame] | 15 | StorageInfo::StorageInfo(const std::string& storage_name, |
| 16 | const LIBMTP_device_entry_t& device, |
Lei Zhang | df1b6b8 | 2012-08-29 02:05:24 | [diff] [blame] | 17 | const LIBMTP_devicestorage_t& storage, |
| 18 | const std::string& fallback_vendor, |
| 19 | const std::string& fallback_product) |
Lei Zhang | 05dbd32 | 2012-09-11 21:58:13 | [diff] [blame] | 20 | : storage_name_(storage_name), |
| 21 | vendor_id_(device.vendor_id), |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 22 | product_id_(device.product_id), |
| 23 | device_flags_(device.device_flags), |
| 24 | storage_type_(storage.StorageType), |
| 25 | filesystem_type_(storage.FilesystemType), |
| 26 | access_capability_(storage.AccessCapability), |
| 27 | max_capacity_(storage.MaxCapacity), |
| 28 | free_space_in_bytes_(storage.FreeSpaceInBytes), |
| 29 | free_space_in_objects_(storage.FreeSpaceInObjects) { |
Lei Zhang | df1b6b8 | 2012-08-29 02:05:24 | [diff] [blame] | 30 | vendor_ = device.vendor ? device.vendor : fallback_vendor; |
| 31 | product_ = device.product ? device.product : fallback_product; |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 32 | if (storage.StorageDescription) |
| 33 | storage_description_ = storage.StorageDescription; |
| 34 | if (storage.VolumeIdentifier) |
| 35 | volume_identifier_ = storage.VolumeIdentifier; |
| 36 | } |
| 37 | |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 38 | StorageInfo::StorageInfo() |
| 39 | : vendor_id_(0), |
| 40 | product_id_(0), |
| 41 | device_flags_(0), |
| 42 | storage_type_(0), |
| 43 | filesystem_type_(0), |
| 44 | access_capability_(0), |
| 45 | max_capacity_(0), |
| 46 | free_space_in_bytes_(0), |
Lei Zhang | dced4eb | 2017-04-18 05:09:12 | [diff] [blame] | 47 | free_space_in_objects_(0) {} |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 48 | |
Lei Zhang | dced4eb | 2017-04-18 05:09:12 | [diff] [blame] | 49 | StorageInfo::~StorageInfo() {} |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 50 | |
Yuki Awano | 7aa0189 | 2015-05-19 07:41:20 | [diff] [blame] | 51 | void StorageInfo::Update(const LIBMTP_devicestorage_t& storage) { |
| 52 | storage_type_ = storage.StorageType; |
| 53 | filesystem_type_ = storage.FilesystemType; |
| 54 | access_capability_ = storage.AccessCapability; |
| 55 | max_capacity_ = storage.MaxCapacity; |
| 56 | free_space_in_bytes_ = storage.FreeSpaceInBytes; |
| 57 | free_space_in_objects_ = storage.FreeSpaceInObjects; |
| 58 | if (storage.StorageDescription) |
| 59 | storage_description_ = storage.StorageDescription; |
| 60 | if (storage.VolumeIdentifier) |
| 61 | volume_identifier_ = storage.VolumeIdentifier; |
| 62 | } |
| 63 | |
Lei Zhang | f770c4d | 2012-09-07 03:18:26 | [diff] [blame] | 64 | std::vector<uint8_t> StorageInfo::ToDBusFormat() const { |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 65 | MtpStorageInfo protobuf; |
Lei Zhang | 05dbd32 | 2012-09-11 21:58:13 | [diff] [blame] | 66 | protobuf.set_storage_name(storage_name_); |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 67 | protobuf.set_vendor(vendor_); |
| 68 | protobuf.set_vendor_id(vendor_id_); |
| 69 | protobuf.set_product(product_); |
| 70 | protobuf.set_product_id(product_id_); |
| 71 | protobuf.set_device_flags(device_flags_); |
| 72 | protobuf.set_storage_type(storage_type_); |
| 73 | protobuf.set_filesystem_type(filesystem_type_); |
| 74 | protobuf.set_access_capability(access_capability_); |
| 75 | protobuf.set_max_capacity(max_capacity_); |
| 76 | protobuf.set_free_space_in_bytes(free_space_in_bytes_); |
| 77 | protobuf.set_free_space_in_objects(free_space_in_objects_); |
| 78 | protobuf.set_storage_description(storage_description_); |
| 79 | protobuf.set_volume_identifier(volume_identifier_); |
| 80 | |
Lei Zhang | f770c4d | 2012-09-07 03:18:26 | [diff] [blame] | 81 | int size = protobuf.ByteSize(); |
| 82 | std::vector<uint8_t> serialized_proto; |
| 83 | serialized_proto.resize(size); |
Lei Zhang | 8721e6d | 2014-01-15 18:06:39 | [diff] [blame] | 84 | CHECK_GT(size, 0); |
| 85 | CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size)); |
Lei Zhang | d31d0b7 | 2012-08-31 21:36:59 | [diff] [blame] | 86 | return serialized_proto; |
Lei Zhang | f0ab3ae | 2012-08-03 18:02:20 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace mtpd |