blob: 2e09b60f1a3cdae00b49b398ca302ebf3a4db4fc [file] [log] [blame]
Adedeji Adebisi684ec912021-07-22 18:07:52 +00001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "sensorhelper.hpp"
nitroglycerine49dcde12023-03-14 07:24:39 -070016
Adedeji Adebisi684ec912021-07-22 18:07:52 +000017#include "main.hpp"
18
Sui Chen8c5208f2023-04-21 14:10:05 -070019#include <systemd/sd-bus.h>
Adedeji Adebisi684ec912021-07-22 18:07:52 +000020#include <unistd.h>
21
22#include <cassert>
23#include <fstream>
24#include <functional>
Sui Chen8c5208f2023-04-21 14:10:05 -070025#include <map>
Adedeji Adebisi684ec912021-07-22 18:07:52 +000026#include <sstream>
27#include <string>
28#include <vector>
29
30extern SensorSnapshot* g_sensor_snapshot;
31extern DBusConnectionSnapshot* g_connection_snapshot;
32
33std::vector<std::string> MySplit(const std::string& s)
34{
35 int idx = 0, prev_idx = 0;
36 std::vector<std::string> ret;
37 while (idx <= static_cast<int>(s.size()))
38 {
39 if (idx == static_cast<int>(s.size()) || s[idx] == '/')
40 {
41 if (idx > prev_idx)
42 {
43 ret.push_back(s.substr(prev_idx, idx - prev_idx));
44 }
45 prev_idx = idx + 1;
46 }
47 idx++;
48 }
49 return ret;
50}
51
52// Example: /xyz/openbmc_project/sensors/temperature/powerseq_temp
53bool IsSensorObjectPath(const std::string& s)
54{
55 std::vector<std::string> sections = MySplit(s);
56 if (sections.size() == 5 && sections[0] == "xyz" &&
57 sections[1] == "openbmc_project" && sections[2] == "sensors")
58 {
59 return true;
60 }
61 else
62 {
63 return false;
64 }
65}
66
67// Example: /xyz/openbmc_project/sensors/temperature/powerseq_temp/chassis
nitroglycerine49dcde12023-03-14 07:24:39 -070068bool IsSensorObjectPathWithAssociation(
69 const std::string& s, [[maybe_unused]] std::string* sensor_obj_path)
Adedeji Adebisi684ec912021-07-22 18:07:52 +000070{
71 std::vector<std::string> sections = MySplit(s);
72 if (sections.size() == 6)
73 {
74 size_t idx = s.rfind('/');
75 return IsSensorObjectPath(s.substr(0, idx));
76 }
77 else
78 {
79 return false;
80 }
81}
82
83bool IsUniqueName(const std::string& x)
84{
85 if (x.empty())
86 return false;
87 if (x[0] != ':')
88 return false;
89 if (x[0] == ':')
90 {
91 for (int i = 1; i < int(x.size()); i++)
92 {
93 const char ch = x[i];
94 if (ch >= '0' || ch <= '9')
95 continue;
96 else if (ch == '.')
97 continue;
98 else
99 return false;
100 }
101 }
102 return true;
103}
104
105std::vector<std::string> FindAllObjectPathsForService(
Sui Chen8c5208f2023-04-21 14:10:05 -0700106 [[maybe_unused]] sd_bus* bus, [[maybe_unused]] const std::string& service,
nitroglycerine49dcde12023-03-14 07:24:39 -0700107 [[maybe_unused]] std::function<void(const std::string&,
108 const std::vector<std::string>&)>
Adedeji Adebisi684ec912021-07-22 18:07:52 +0000109 on_interface_cb)
110{
111 // Not available for PCAP replay, only valid with actual DBus capture
112 assert(false);
113}
114
115bool IsWhitespace(const char c)
116{
117 if (c == ' ' || c == '\r' || c == '\n')
118 return true;
119 else
120 return false;
121}
122
123std::string Trim(const std::string& s)
124{
125 const int N = int(s.size());
126 int idx0 = 0, idx1 = int(N - 1);
127 while (idx0 < N && IsWhitespace(s[idx0]))
128 idx0++;
129 while (idx1 >= 0 && IsWhitespace(s[idx1]))
130 idx1--;
131 if (idx0 >= N || idx1 < 0)
132 return "";
133 return s.substr(idx0, idx1 - idx0 + 1);
134}
Sui Chen8c5208f2023-04-21 14:10:05 -0700135
136int GetOrInsertPathID(std::map<std::string, int>* lookup,
137 const std::string& path)
138{
139 if (lookup->find(path) == lookup->end())
140 {
141 (*lookup)[path] = lookup->size();
142 }
143 return lookup->at(path);
144}
145
146std::string SimplifyPath(std::string s)
147{
148 const std::string& k = "/xyz/openbmc_project";
149 if (s != k && s.find(k) == 0)
150 {
151 s = s.substr(k.size());
152 }
153 return s;
154}
155
156std::pair<std::string, std::string> ExtractFileName(std::string x)
157{
158 size_t idx = x.rfind('/');
159 std::string d = "";
160 if (idx != std::string::npos)
161 {
162 d = x.substr(0, idx);
163 x = x.substr(idx);
164 }
165 return {d, x};
Patrick Williamsc3aa2a82023-05-10 07:51:38 -0500166}