1 #pragma once
2 
3 #include "errors.hpp"
4 
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;
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.");
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 
isValidDbusPath(const std::string & path)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 
verifyIdCharacters(std::string_view id)53 inline void verifyIdCharacters(std::string_view id)
54 {
55     if (id.find_first_not_of(utils::constants::allowedCharactersInPath) !=
56         std::string::npos)
57     {
58         throw errors::InvalidArgument("Id", "Invalid character.");
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