blob: 3798d980d4ef117cb99b53fb1bf62b8d612682b6 [file] [log] [blame]
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02001#pragma once
2
3#include <sdbusplus/message.hpp>
4
5#include <algorithm>
6#include <ranges>
7#include <string_view>
8
9namespace utils
10{
11
12namespace constants
13{
14constexpr std::string_view triggerDirStr =
15 "/xyz/openbmc_project/Telemetry/Triggers/";
16constexpr std::string_view reportDirStr =
17 "/xyz/openbmc_project/Telemetry/Reports/";
18
19constexpr std::string_view allowedCharactersInPath =
20 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/";
21constexpr size_t maxPrefixesInId = 1;
Szymon Dompke32305f12022-07-05 15:37:21 +020022constexpr size_t maxPrefixLength{TELEMETRY_MAX_PREFIX_LENGTH};
23constexpr size_t maxIdNameLength{TELEMETRY_MAX_ID_NAME_LENGTH};
24constexpr size_t maxDbusPathLength{TELEMETRY_MAX_DBUS_PATH_LENGTH};
25
26constexpr size_t maxTriggeFullIdLength{maxDbusPathLength -
27 triggerDirStr.length()};
28constexpr size_t maxReportFullIdLength{maxDbusPathLength -
29 reportDirStr.length()};
30
31static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
32 maxTriggeFullIdLength,
33 "Misconfigured prefix/id/name lengths.");
34static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
35 maxReportFullIdLength,
36 "Misconfigured prefix/id/name lengths.");
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020037
38const sdbusplus::message::object_path triggerDirPath =
39 sdbusplus::message::object_path(std::string(triggerDirStr));
40const sdbusplus::message::object_path reportDirPath =
41 sdbusplus::message::object_path(std::string(reportDirStr));
42} // namespace constants
43
44inline 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
Szymon Dompke32305f12022-07-05 15:37:21 +020051inline 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
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020062sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
63 const std::string& appended);
64
65std::string reportPathToId(const sdbusplus::message::object_path& path);
66
Szymon Dompke32305f12022-07-05 15:37:21 +020067void verifyIdPrefixes(std::string_view id);
68
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020069} // namespace utils