blob: ed482e7f0e46904f2d9f879b2e6015da0509932a [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
19#include <unistd.h>
20
21#include <cassert>
22#include <fstream>
23#include <functional>
24#include <sstream>
25#include <string>
26#include <vector>
27
28extern SensorSnapshot* g_sensor_snapshot;
29extern DBusConnectionSnapshot* g_connection_snapshot;
30
31std::vector<std::string> MySplit(const std::string& s)
32{
33 int idx = 0, prev_idx = 0;
34 std::vector<std::string> ret;
35 while (idx <= static_cast<int>(s.size()))
36 {
37 if (idx == static_cast<int>(s.size()) || s[idx] == '/')
38 {
39 if (idx > prev_idx)
40 {
41 ret.push_back(s.substr(prev_idx, idx - prev_idx));
42 }
43 prev_idx = idx + 1;
44 }
45 idx++;
46 }
47 return ret;
48}
49
50// Example: /xyz/openbmc_project/sensors/temperature/powerseq_temp
51bool IsSensorObjectPath(const std::string& s)
52{
53 std::vector<std::string> sections = MySplit(s);
54 if (sections.size() == 5 && sections[0] == "xyz" &&
55 sections[1] == "openbmc_project" && sections[2] == "sensors")
56 {
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63}
64
65// Example: /xyz/openbmc_project/sensors/temperature/powerseq_temp/chassis
nitroglycerine49dcde12023-03-14 07:24:39 -070066bool IsSensorObjectPathWithAssociation(
67 const std::string& s, [[maybe_unused]] std::string* sensor_obj_path)
Adedeji Adebisi684ec912021-07-22 18:07:52 +000068{
69 std::vector<std::string> sections = MySplit(s);
70 if (sections.size() == 6)
71 {
72 size_t idx = s.rfind('/');
73 return IsSensorObjectPath(s.substr(0, idx));
74 }
75 else
76 {
77 return false;
78 }
79}
80
81bool IsUniqueName(const std::string& x)
82{
83 if (x.empty())
84 return false;
85 if (x[0] != ':')
86 return false;
87 if (x[0] == ':')
88 {
89 for (int i = 1; i < int(x.size()); i++)
90 {
91 const char ch = x[i];
92 if (ch >= '0' || ch <= '9')
93 continue;
94 else if (ch == '.')
95 continue;
96 else
97 return false;
98 }
99 }
100 return true;
101}
102
103std::vector<std::string> FindAllObjectPathsForService(
nitroglycerine49dcde12023-03-14 07:24:39 -0700104 [[maybe_unused]] const std::string& service,
105 [[maybe_unused]] std::function<void(const std::string&,
106 const std::vector<std::string>&)>
Adedeji Adebisi684ec912021-07-22 18:07:52 +0000107 on_interface_cb)
108{
109 // Not available for PCAP replay, only valid with actual DBus capture
110 assert(false);
111}
112
113bool IsWhitespace(const char c)
114{
115 if (c == ' ' || c == '\r' || c == '\n')
116 return true;
117 else
118 return false;
119}
120
121std::string Trim(const std::string& s)
122{
123 const int N = int(s.size());
124 int idx0 = 0, idx1 = int(N - 1);
125 while (idx0 < N && IsWhitespace(s[idx0]))
126 idx0++;
127 while (idx1 >= 0 && IsWhitespace(s[idx1]))
128 idx1--;
129 if (idx0 >= N || idx1 < 0)
130 return "";
131 return s.substr(idx0, idx1 - idx0 + 1);
132}