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