Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 3 | #include "errors.hpp" |
| 4 | |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 5 | #include <sdbusplus/message.hpp> |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <ranges> |
| 9 | #include <string_view> |
| 10 | |
| 11 | namespace utils |
| 12 | { |
| 13 | |
| 14 | namespace constants |
| 15 | { |
| 16 | constexpr std::string_view triggerDirStr = |
| 17 | "/xyz/openbmc_project/Telemetry/Triggers/"; |
| 18 | constexpr std::string_view reportDirStr = |
| 19 | "/xyz/openbmc_project/Telemetry/Reports/"; |
| 20 | |
| 21 | constexpr std::string_view allowedCharactersInPath = |
| 22 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/"; |
| 23 | constexpr size_t maxPrefixesInId = 1; |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 24 | constexpr size_t maxPrefixLength{TELEMETRY_MAX_PREFIX_LENGTH}; |
| 25 | constexpr size_t maxIdNameLength{TELEMETRY_MAX_ID_NAME_LENGTH}; |
| 26 | constexpr size_t maxDbusPathLength{TELEMETRY_MAX_DBUS_PATH_LENGTH}; |
| 27 | |
| 28 | constexpr size_t maxTriggeFullIdLength{maxDbusPathLength - |
| 29 | triggerDirStr.length()}; |
| 30 | constexpr size_t maxReportFullIdLength{maxDbusPathLength - |
| 31 | reportDirStr.length()}; |
| 32 | |
| 33 | static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <= |
| 34 | maxTriggeFullIdLength, |
| 35 | "Misconfigured prefix/id/name lengths."); |
| 36 | static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <= |
| 37 | maxReportFullIdLength, |
| 38 | "Misconfigured prefix/id/name lengths."); |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 39 | |
| 40 | const sdbusplus::message::object_path triggerDirPath = |
| 41 | sdbusplus::message::object_path(std::string(triggerDirStr)); |
| 42 | const sdbusplus::message::object_path reportDirPath = |
| 43 | sdbusplus::message::object_path(std::string(reportDirStr)); |
| 44 | } // namespace constants |
| 45 | |
| 46 | inline bool isValidDbusPath(const std::string& path) |
| 47 | { |
| 48 | return (path.find_first_not_of(constants::allowedCharactersInPath) == |
| 49 | std::string::npos) && |
| 50 | !path.ends_with('/'); |
| 51 | } |
| 52 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 53 | inline void verifyIdCharacters(std::string_view id) |
| 54 | { |
| 55 | if (id.find_first_not_of(utils::constants::allowedCharactersInPath) != |
| 56 | std::string::npos) |
| 57 | { |
Krzysztof Grobelny | 62c08e9 | 2022-09-16 10:28:53 +0200 | [diff] [blame] | 58 | throw errors::InvalidArgument("Id", "Invalid character."); |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 62 | sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path, |
| 63 | const std::string& appended); |
| 64 | |
| 65 | std::string reportPathToId(const sdbusplus::message::object_path& path); |
| 66 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 67 | void verifyIdPrefixes(std::string_view id); |
| 68 | |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 69 | } // namespace utils |