blob: 02f1cd187541843d74e206e5ce5668fb459786c8 [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
Eric Carusoe7cd2682017-08-21 22:57:305#include "mtpd/storage_info.h"
Lei Zhangf0ab3ae2012-08-03 18:02:206
Lei Zhangd31d0b72012-08-31 21:36:597#include <base/logging.h>
Lei Zhang13b41032017-04-22 06:00:458#include <chromeos/dbus/service_constants.h>
Lei Zhanga3d6d0a2012-08-06 20:29:279
Eric Carusoe7cd2682017-08-21 22:57:3010#include "mtpd/string_helpers.h"
Eric Caruso34eeec52017-08-21 22:31:1911#include "system_api/proto_bindings/mtp_storage_info.pb.h"
Lei Zhangf0ab3ae2012-08-03 18:02:2012
13namespace mtpd {
14
Lei Zhang05dbd322012-09-11 21:58:1315StorageInfo::StorageInfo(const std::string& storage_name,
16 const LIBMTP_device_entry_t& device,
Lei Zhangdf1b6b82012-08-29 02:05:2417 const LIBMTP_devicestorage_t& storage,
18 const std::string& fallback_vendor,
19 const std::string& fallback_product)
Lei Zhang05dbd322012-09-11 21:58:1320 : storage_name_(storage_name),
21 vendor_id_(device.vendor_id),
Lei Zhangf0ab3ae2012-08-03 18:02:2022 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 Zhangdf1b6b82012-08-29 02:05:2430 vendor_ = device.vendor ? device.vendor : fallback_vendor;
31 product_ = device.product ? device.product : fallback_product;
Lei Zhangf0ab3ae2012-08-03 18:02:2032 if (storage.StorageDescription)
33 storage_description_ = storage.StorageDescription;
34 if (storage.VolumeIdentifier)
35 volume_identifier_ = storage.VolumeIdentifier;
36}
37
Lei Zhangd31d0b72012-08-31 21:36:5938StorageInfo::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 Zhangdced4eb2017-04-18 05:09:1247 free_space_in_objects_(0) {}
Lei Zhangd31d0b72012-08-31 21:36:5948
Lei Zhangdced4eb2017-04-18 05:09:1249StorageInfo::~StorageInfo() {}
Lei Zhangf0ab3ae2012-08-03 18:02:2050
Yuki Awano7aa01892015-05-19 07:41:2051void 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 Zhangf770c4d2012-09-07 03:18:2664std::vector<uint8_t> StorageInfo::ToDBusFormat() const {
Lei Zhangd31d0b72012-08-31 21:36:5965 MtpStorageInfo protobuf;
Lei Zhang05dbd322012-09-11 21:58:1366 protobuf.set_storage_name(storage_name_);
Lei Zhangd31d0b72012-08-31 21:36:5967 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 Zhangf770c4d2012-09-07 03:18:2681 int size = protobuf.ByteSize();
82 std::vector<uint8_t> serialized_proto;
83 serialized_proto.resize(size);
Lei Zhang8721e6d2014-01-15 18:06:3984 CHECK_GT(size, 0);
85 CHECK(protobuf.SerializeToArray(&serialized_proto.front(), size));
Lei Zhangd31d0b72012-08-31 21:36:5986 return serialized_proto;
Lei Zhangf0ab3ae2012-08-03 18:02:2087}
88
89} // namespace mtpd