Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 1 | // Copyright 2023 The ChromiumOS Authors |
| 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 LIBSEGMENTATION_FEATURE_MANAGEMENT_UTIL_H_ |
| 6 | #define LIBSEGMENTATION_FEATURE_MANAGEMENT_UTIL_H_ |
| 7 | |
Allen Shih | 87ce1a1 | 2025-04-17 05:22:34 | [diff] [blame] | 8 | #include <optional> |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 9 | #include <string> |
Abhishek Bhardwaj | 64a204b | 2023-05-08 22:23:41 | [diff] [blame] | 10 | |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 11 | #include <base/files/file_path.h> |
Allen Shih | 87ce1a1 | 2025-04-17 05:22:34 | [diff] [blame] | 12 | #include <base/values.h> |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 13 | #include <brillo/brillo_export.h> |
Abhishek Bhardwaj | 64a204b | 2023-05-08 22:23:41 | [diff] [blame] | 14 | |
Abhishek Bhardwaj | 64a204b | 2023-05-08 22:23:41 | [diff] [blame] | 15 | #include "libsegmentation/device_info.pb.h" |
| 16 | #include "libsegmentation/feature_management_interface.h" |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 17 | |
| 18 | namespace segmentation { |
| 19 | |
| 20 | // An implementation that invokes the corresponding functions provided |
| 21 | // in feature_management_interface.h. |
| 22 | class BRILLO_EXPORT FeatureManagementUtil { |
| 23 | public: |
Gwendal Grignou | 9a8d51e | 2024-01-10 17:50:10 | [diff] [blame] | 24 | // Reads device info from string. Returns std::nullopt if the read wasn't |
| 25 | // successful. |
| 26 | static std::optional<libsegmentation::DeviceInfo> ReadDeviceInfo( |
| 27 | const std::string& encoded); |
| 28 | |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 29 | // Reads device info from |file_path|. Returns std::nullopt if the read wasn't |
| 30 | // successful. |
Gwendal Grignou | 9a8d51e | 2024-01-10 17:50:10 | [diff] [blame] | 31 | static std::optional<libsegmentation::DeviceInfo> ReadDeviceInfo( |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 32 | const base::FilePath& file_path); |
| 33 | |
Gwendal Grignou | 9a8d51e | 2024-01-10 17:50:10 | [diff] [blame] | 34 | // Returns |device_info| as base64. |
| 35 | static std::string EncodeDeviceInfo( |
| 36 | const libsegmentation::DeviceInfo& device_info); |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 37 | |
| 38 | // Converts feature level from the internal proto to the external API. |
| 39 | static FeatureManagementInterface::FeatureLevel ConvertProtoFeatureLevel( |
| 40 | libsegmentation::DeviceInfo_FeatureLevel feature_level); |
Abhishek Bhardwaj | 30d2ee8 | 2023-05-05 01:22:28 | [diff] [blame] | 41 | |
| 42 | // Converts scope level from the internal proto to the external API. |
| 43 | static FeatureManagementInterface::ScopeLevel ConvertProtoScopeLevel( |
| 44 | libsegmentation::DeviceInfo_ScopeLevel scope_level); |
Gwendal Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 45 | |
Gwendal Grignou | 98c9e71 | 2023-06-21 22:48:00 | [diff] [blame] | 46 | // Return the size of a block device. |
| 47 | // dev format is '/dev/sda', '/dev/nvme0n1', '/dev/mmcblk0', ... |
| 48 | // TODO(b:176492189): Move to a common library |
| 49 | static std::optional<int64_t> GetDiskSpace(const base::FilePath& dev); |
| 50 | |
| 51 | // Find the fixed block device on the device. |
| 52 | // It may not be the device the rootfs is when we run ChromeOS from a |
| 53 | // removable device. |
| 54 | // The block device will be for example /dev/sda, /dev/mmcblk1, ... |
| 55 | // TODO(b:176492189): Move to a common library |
| 56 | // root is the usual '/', unless we are unit testing. In that case. |
| 57 | // root points to a temporary directory set up for testing. |
| 58 | static std::optional<base::FilePath> GetDefaultRoot( |
| 59 | const base::FilePath& root); |
Abhishek Bhardwaj | 0249a2c | 2023-05-01 23:39:11 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace segmentation |
| 63 | |
| 64 | #endif // LIBSEGMENTATION_FEATURE_MANAGEMENT_UTIL_H_ |