xref: /openbmc/phosphor-post-code-manager/inc/post_code.hpp (revision b6616cdda24eed66e3e23762eee7857d62ce0b85)
1 /*
2 // Copyright (c) 2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 #include <config.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 
21 #include <nlohmann/json.hpp>
22 #include <phosphor-logging/elog-errors.hpp>
23 #include <sdbusplus/timer.hpp>
24 #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
25 #include <xyz/openbmc_project/Common/error.hpp>
26 #include <xyz/openbmc_project/State/Boot/PostCode/server.hpp>
27 #include <xyz/openbmc_project/State/Host/server.hpp>
28 
29 #include <chrono>
30 #include <filesystem>
31 #include <fstream>
32 #include <iostream>
33 
34 const static constexpr char* CurrentBootCycleCountName =
35     "CurrentBootCycleCount";
36 const static constexpr char* CurrentBootCycleIndexName =
37     "CurrentBootCycleIndex";
38 
39 const static constexpr char* PostCodePath =
40     "/xyz/openbmc_project/state/boot/raw";
41 const static constexpr char* PostCodeListPathPrefix =
42     "/var/lib/phosphor-post-code-manager/host";
43 const static constexpr char* HostStatePathPrefix =
44     "/xyz/openbmc_project/state/host";
45 
46 struct EventDeleter
47 {
operator ()EventDeleter48     void operator()(sd_event* event) const
49     {
50         sd_event_unref(event);
51     }
52 };
53 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
54 using primarycode_t = std::vector<uint8_t>;
55 using secondarycode_t = std::vector<uint8_t>;
56 using postcode_t = std::tuple<primarycode_t, secondarycode_t>;
57 namespace fs = std::filesystem;
58 namespace StateServer = sdbusplus::xyz::openbmc_project::State::server;
59 
60 using post_code =
61     sdbusplus::xyz::openbmc_project::State::Boot::server::PostCode;
62 using delete_all =
63     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll;
64 
65 struct PostCodeEvent
66 {
67     std::string name;
68     nlohmann::json args;
69     void raise() const;
70 };
71 
72 struct PostCodeHandler
73 {
74     primarycode_t primary;
75     std::optional<secondarycode_t> secondary;
76     std::vector<std::string> targets;
77     std::optional<PostCodeEvent> event;
78 };
79 
80 struct PostCodeHandlers
81 {
82     std::vector<PostCodeHandler> handlers;
83     void handle(postcode_t code);
84     const PostCodeHandler* find(postcode_t code);
85     void load(const std::string& path);
86 };
87 
88 struct PostCode : sdbusplus::server::object_t<post_code, delete_all>
89 {
PostCodePostCode90     PostCode(sdbusplus::bus_t& bus, const char* path, EventPtr& event,
91              int nodeIndex, PostCodeHandlers& handlers) :
92         sdbusplus::server::object_t<post_code, delete_all>(bus, path), bus(bus),
93         event(event), node(nodeIndex),
94         postCodeListPath(PostCodeListPathPrefix + std::to_string(node)),
95         propertiesChangedSignalRaw(
96             bus,
97             sdbusplus::bus::match::rules::propertiesChanged(
98                 PostCodePath + std::to_string(node),
99                 "xyz.openbmc_project.State.Boot.Raw"),
100             [this](sdbusplus::message_t& msg) {
101                 std::string intfName;
102                 std::map<std::string, std::variant<postcode_t>> msgData;
103                 msg.read(intfName, msgData);
104                 // Check if it was the Value property that changed.
105                 auto valPropMap = msgData.find("Value");
106                 if (valPropMap != msgData.end())
107                 {
108                     this->savePostCodes(
109                         std::get<postcode_t>(valPropMap->second));
110                 }
111             }),
112         propertiesChangedSignalCurrentHostState(
113             bus,
114             sdbusplus::bus::match::rules::propertiesChanged(
115                 HostStatePathPrefix + std::to_string(node),
116                 "xyz.openbmc_project.State.Host"),
__anon43c04d960202PostCode117             [this](sdbusplus::message_t& msg) {
118                 std::string intfName;
119                 std::map<std::string, std::variant<std::string>> msgData;
120                 msg.read(intfName, msgData);
121                 // Check if it was the Value property that changed.
122                 auto valPropMap = msgData.find("CurrentHostState");
123                 if (valPropMap != msgData.end())
124                 {
125                     StateServer::Host::HostState currentHostState =
126                         StateServer::Host::convertHostStateFromString(
127                             std::get<std::string>(valPropMap->second));
128                     if (currentHostState == StateServer::Host::HostState::Off)
129                     {
130                         if (this->postCodes.empty())
131                         {
132                             std::cerr
133                                 << "HostState changed to OFF. Empty "
134                                    "postcode log, keep boot cycle at "
135                                 << this->currentBootCycleIndex << std::endl;
136                         }
137                         else
138                         {
139                             this->postCodes.clear();
140                         }
141                     }
142                 }
143             }),
144         postCodeHandlers(std::move(handlers))
145     {
146         phosphor::logging::log<phosphor::logging::level::INFO>(
147             "PostCode is created");
148         fs::create_directories(postCodeListPath);
149         deserialize(postCodeListPath / CurrentBootCycleIndexName,
150                     currentBootCycleIndex);
151         uint16_t count = 0;
152         deserialize(postCodeListPath / CurrentBootCycleCountName, count);
153         currentBootCycleCount(count);
154         maxBootCycleNum(MAX_BOOT_CYCLE_COUNT);
155     }
~PostCodePostCode156     ~PostCode() {}
157 
158     std::vector<postcode_t> getPostCodes(uint16_t index) override;
159     std::map<uint64_t, postcode_t> getPostCodesWithTimeStamp(
160         uint16_t index) override;
161     void deleteAll() override;
162 
163   private:
164     void incrBootCycle();
165     uint16_t getBootNum(const uint16_t index) const;
166 
167     std::unique_ptr<sdbusplus::Timer> timer;
168     sdbusplus::bus_t& bus;
169     EventPtr& event;
170     int node;
171     std::chrono::time_point<std::chrono::steady_clock> firstPostCodeTimeSteady;
172     uint64_t firstPostCodeUsSinceEpoch;
173     std::map<uint64_t, postcode_t> postCodes;
174     fs::path postCodeListPath;
175     uint16_t currentBootCycleIndex = 0;
176     sdbusplus::bus::match_t propertiesChangedSignalRaw;
177     sdbusplus::bus::match_t propertiesChangedSignalCurrentHostState;
178 
179     void savePostCodes(postcode_t code);
180     fs::path serialize(const fs::path& path);
181     bool deserialize(const fs::path& path, uint16_t& index);
182     bool deserializePostCodes(const fs::path& path,
183                               std::map<uint64_t, postcode_t>& codes);
184     PostCodeHandlers postCodeHandlers;
185 };
186