blob: fdcc7218efce424d18adcdaa57431a5655332533 [file] [log] [blame]
Sridevi Ramesh98576432019-11-27 10:10:28 -06001#include "pldm_bios_cmd.hpp"
2
3#include "pldm_cmd_helper.hpp"
4
5#include <map>
6#include <memory>
7#include <string>
8#include <typeinfo>
9#include <vector>
10
11#include "libpldm/utils.h"
12
13namespace pldmtool
14{
15
16namespace bios
17{
18
19namespace
20{
21
22using namespace pldmtool::helper;
23
24std::vector<std::unique_ptr<CommandInterface>> commands;
25
26} // namespace
27
28class GetDateTime : public CommandInterface
29{
30 public:
31 ~GetDateTime() = default;
32 GetDateTime() = delete;
33 GetDateTime(const GetDateTime&) = delete;
34 GetDateTime(GetDateTime&&) = default;
35 GetDateTime& operator=(const GetDateTime&) = delete;
36 GetDateTime& operator=(GetDateTime&&) = default;
37
38 using CommandInterface::CommandInterface;
39
40 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
41 {
42 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr));
43 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
44
45 auto rc = encode_get_date_time_req(PLDM_LOCAL_INSTANCE_ID, request);
46 return {rc, requestMsg};
47 }
48
49 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
50 {
51 uint8_t cc = 0;
52
53 uint8_t seconds, minutes, hours, day, month;
54 uint16_t year;
55 auto rc =
56 decode_get_date_time_resp(responsePtr, payloadLength, &cc, &seconds,
57 &minutes, &hours, &day, &month, &year);
58 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
59 {
60 std::cerr << "Response Message Error: "
61 << "rc=" << rc << ",cc=" << (int)cc << std::endl;
62 return;
63 }
64 std::cout << "Date & Time : " << std::endl;
65 std::cout << "DD-MM-YYYY HH:MM:SS - ";
66 setWidth(day);
67 std::cout << "-";
68 setWidth(month);
69 std::cout << "-" << bcd2dec16(year) << " ";
70 setWidth(hours);
71 std::cout << ":";
72 setWidth(minutes);
73 std::cout << ":";
74 setWidth(seconds);
75 std::cout << std::endl;
76 }
77
78 private:
79 void setWidth(uint8_t data)
80 {
81 std::cout << std::setfill('0') << std::setw(2)
82 << static_cast<uint32_t>(bcd2dec8(data));
83 }
84};
85
George Liud6649362019-11-27 19:06:51 +080086class SetDateTime : public CommandInterface
87{
88 public:
89 ~SetDateTime() = default;
90 SetDateTime() = delete;
91 SetDateTime(const SetDateTime&) = delete;
92 SetDateTime(SetDateTime&&) = default;
93 SetDateTime& operator=(const SetDateTime&) = delete;
94 SetDateTime& operator=(SetDateTime&&) = default;
95
96 explicit SetDateTime(const char* type, const char* name, CLI::App* app) :
97 CommandInterface(type, name, app)
98 {
99 app->add_option("-d,--data", tmData,
100 "set date time data\n"
101 "eg: YYYYMMDDHHMMSS")
102 ->required();
103 }
104
105 std::pair<int, std::vector<uint8_t>> createRequestMsg() override
106 {
107 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
108 sizeof(struct pldm_set_date_time_req));
109 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
110 uint16_t year = 0;
111 uint8_t month = 0;
112 uint8_t day = 0;
113 uint8_t hours = 0;
114 uint8_t minutes = 0;
115 uint8_t seconds = 0;
116
117 if (!uintToDate(tmData, &year, &month, &day, &hours, &minutes,
118 &seconds))
119 {
120 std::cerr << "decode date Error: "
121 << "tmData=" << tmData << std::endl;
122
123 return {PLDM_ERROR_INVALID_DATA, requestMsg};
124 }
125
126 auto rc = encode_set_date_time_req(
127 PLDM_LOCAL_INSTANCE_ID, seconds, minutes, hours, day, month, year,
128 request, sizeof(struct pldm_set_date_time_req));
129
130 return {rc, requestMsg};
131 }
132
133 void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override
134 {
135 uint8_t completionCode = 0;
136 auto rc = decode_set_date_time_resp(responsePtr, payloadLength,
137 &completionCode);
138
139 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
140 {
141 std::cerr << "Response Message Error: "
142 << "rc=" << rc << ",cc=" << (int)completionCode
143 << std::endl;
144 return;
145 }
146
147 std::cout << "SetDateTime: SUCCESS" << std::endl;
148 }
149
150 private:
151 uint64_t tmData;
152};
153
Sridevi Ramesh98576432019-11-27 10:10:28 -0600154void registerCommand(CLI::App& app)
155{
156 auto bios = app.add_subcommand("bios", "bios type command");
157 bios->require_subcommand(1);
158 auto getDateTime = bios->add_subcommand("GetDateTime", "get date time");
159 commands.push_back(
160 std::make_unique<GetDateTime>("bios", "GetDateTime", getDateTime));
George Liud6649362019-11-27 19:06:51 +0800161
162 auto setDateTime =
163 bios->add_subcommand("SetDateTime", "set host date time");
164 commands.push_back(
165 std::make_unique<SetDateTime>("bios", "setDateTime", setDateTime));
Sridevi Ramesh98576432019-11-27 10:10:28 -0600166}
167
168} // namespace bios
169
170} // namespace pldmtool