blob: c1bda62c668de95571bc76ed1bb311fdbd0ece9d [file] [log] [blame]
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02001#pragma once
2
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +02003#include "errors.hpp"
4
Szymon Dompke1cdd7e42022-06-08 14:43:13 +02005#include <sdbusplus/message.hpp>
6
7#include <algorithm>
8#include <ranges>
9#include <string_view>
10
11namespace utils
12{
13
14namespace constants
15{
16constexpr std::string_view triggerDirStr =
17 "/xyz/openbmc_project/Telemetry/Triggers/";
18constexpr std::string_view reportDirStr =
19 "/xyz/openbmc_project/Telemetry/Reports/";
20
21constexpr std::string_view allowedCharactersInPath =
22 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/";
23constexpr size_t maxPrefixesInId = 1;
Szymon Dompke32305f12022-07-05 15:37:21 +020024constexpr size_t maxPrefixLength{TELEMETRY_MAX_PREFIX_LENGTH};
25constexpr size_t maxIdNameLength{TELEMETRY_MAX_ID_NAME_LENGTH};
26constexpr size_t maxDbusPathLength{TELEMETRY_MAX_DBUS_PATH_LENGTH};
27
28constexpr size_t maxTriggeFullIdLength{maxDbusPathLength -
29 triggerDirStr.length()};
30constexpr size_t maxReportFullIdLength{maxDbusPathLength -
31 reportDirStr.length()};
32
33static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
34 maxTriggeFullIdLength,
35 "Misconfigured prefix/id/name lengths.");
36static_assert(maxPrefixesInId * (maxPrefixLength + 1) + maxIdNameLength <=
37 maxReportFullIdLength,
38 "Misconfigured prefix/id/name lengths.");
Szymon Dompke1cdd7e42022-06-08 14:43:13 +020039
40const sdbusplus::message::object_path triggerDirPath =
41 sdbusplus::message::object_path(std::string(triggerDirStr));
42const sdbusplus::message::object_path reportDirPath =
43 sdbusplus::message::object_path(std::string(reportDirStr));
44} // namespace constants
45
46inline 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 Dompke32305f12022-07-05 15:37:21 +020053inline void verifyIdCharacters(std::string_view id)
54{
55 if (id.find_first_not_of(utils::constants::allowedCharactersInPath) !=
56 std::string::npos)
57 {
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +020058 throw errors::InvalidArgument("Id", "Invalid character.");
Szymon Dompke32305f12022-07-05 15:37:21 +020059 }
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