1 #pragma once 2 3 #include <sdbusplus/message.hpp> 4 5 #include <algorithm> 6 #include <ranges> 7 #include <string_view> 8 9 namespace utils 10 { 11 12 namespace constants 13 { 14 constexpr std::string_view triggerDirStr = 15 "/xyz/openbmc_project/Telemetry/Triggers/"; 16 constexpr std::string_view reportDirStr = 17 "/xyz/openbmc_project/Telemetry/Reports/"; 18 19 constexpr std::string_view allowedCharactersInPath = 20 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/"; 21 constexpr size_t maxPrefixesInId = 1; 22 constexpr size_t maxPrefixLength{TELEMETRY_MAX_PREFIX_LENGTH}; 23 constexpr size_t maxIdNameLength{TELEMETRY_MAX_ID_NAME_LENGTH}; 24 constexpr size_t maxDbusPathLength{TELEMETRY_MAX_DBUS_PATH_LENGTH}; 25 26 constexpr size_t maxTriggeFullIdLength{maxDbusPathLength - 27 triggerDirStr.length()}; 28 constexpr size_t maxReportFullIdLength{maxDbusPathLength - 29 reportDirStr.length()}; 30 31 static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <= 32 maxTriggeFullIdLength, 33 "Misconfigured prefix/id/name lengths."); 34 static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <= 35 maxReportFullIdLength, 36 "Misconfigured prefix/id/name lengths."); 37 38 const sdbusplus::message::object_path triggerDirPath = 39 sdbusplus::message::object_path(std::string(triggerDirStr)); 40 const sdbusplus::message::object_path reportDirPath = 41 sdbusplus::message::object_path(std::string(reportDirStr)); 42 } // namespace constants 43 44 inline bool isValidDbusPath(const std::string& path) 45 { 46 return (path.find_first_not_of(constants::allowedCharactersInPath) == 47 std::string::npos) && 48 !path.ends_with('/'); 49 } 50 51 inline void verifyIdCharacters(std::string_view id) 52 { 53 if (id.find_first_not_of(utils::constants::allowedCharactersInPath) != 54 std::string::npos) 55 { 56 throw sdbusplus::exception::SdBusError( 57 static_cast<int>(std::errc::invalid_argument), 58 "Invalid character in id"); 59 } 60 } 61 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 67 void verifyIdPrefixes(std::string_view id); 68 69 } // namespace utils 70