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