Sridevi Ramesh | 9857643 | 2019-11-27 10:10:28 -0600 | [diff] [blame] | 1 | #include "pldm_bios_cmd.hpp" |
| 2 | |
| 3 | #include "pldm_cmd_helper.hpp" |
| 4 | |
Sridevi Ramesh | 9857643 | 2019-11-27 10:10:28 -0600 | [diff] [blame] | 5 | #include "libpldm/utils.h" |
| 6 | |
| 7 | namespace pldmtool |
| 8 | { |
| 9 | |
| 10 | namespace bios |
| 11 | { |
| 12 | |
| 13 | namespace |
| 14 | { |
| 15 | |
| 16 | using namespace pldmtool::helper; |
| 17 | |
| 18 | std::vector<std::unique_ptr<CommandInterface>> commands; |
| 19 | |
| 20 | } // namespace |
| 21 | |
| 22 | class 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; |
| 59 | std::cout << "DD-MM-YYYY HH:MM:SS - "; |
| 60 | setWidth(day); |
| 61 | std::cout << "-"; |
| 62 | setWidth(month); |
| 63 | std::cout << "-" << bcd2dec16(year) << " "; |
| 64 | setWidth(hours); |
| 65 | std::cout << ":"; |
| 66 | setWidth(minutes); |
| 67 | std::cout << ":"; |
| 68 | setWidth(seconds); |
| 69 | std::cout << std::endl; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | void setWidth(uint8_t data) |
| 74 | { |
| 75 | std::cout << std::setfill('0') << std::setw(2) |
| 76 | << static_cast<uint32_t>(bcd2dec8(data)); |
| 77 | } |
| 78 | }; |
| 79 | |
George Liu | d664936 | 2019-11-27 19:06:51 +0800 | [diff] [blame] | 80 | class SetDateTime : public CommandInterface |
| 81 | { |
| 82 | public: |
| 83 | ~SetDateTime() = default; |
| 84 | SetDateTime() = delete; |
| 85 | SetDateTime(const SetDateTime&) = delete; |
| 86 | SetDateTime(SetDateTime&&) = default; |
| 87 | SetDateTime& operator=(const SetDateTime&) = delete; |
| 88 | SetDateTime& operator=(SetDateTime&&) = default; |
| 89 | |
| 90 | explicit SetDateTime(const char* type, const char* name, CLI::App* app) : |
| 91 | CommandInterface(type, name, app) |
| 92 | { |
| 93 | app->add_option("-d,--data", tmData, |
| 94 | "set date time data\n" |
| 95 | "eg: YYYYMMDDHHMMSS") |
| 96 | ->required(); |
| 97 | } |
| 98 | |
| 99 | std::pair<int, std::vector<uint8_t>> createRequestMsg() override |
| 100 | { |
| 101 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 102 | sizeof(struct pldm_set_date_time_req)); |
| 103 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 104 | uint16_t year = 0; |
| 105 | uint8_t month = 0; |
| 106 | uint8_t day = 0; |
| 107 | uint8_t hours = 0; |
| 108 | uint8_t minutes = 0; |
| 109 | uint8_t seconds = 0; |
| 110 | |
| 111 | if (!uintToDate(tmData, &year, &month, &day, &hours, &minutes, |
| 112 | &seconds)) |
| 113 | { |
| 114 | std::cerr << "decode date Error: " |
| 115 | << "tmData=" << tmData << std::endl; |
| 116 | |
| 117 | return {PLDM_ERROR_INVALID_DATA, requestMsg}; |
| 118 | } |
| 119 | |
| 120 | auto rc = encode_set_date_time_req( |
| 121 | PLDM_LOCAL_INSTANCE_ID, seconds, minutes, hours, day, month, year, |
| 122 | request, sizeof(struct pldm_set_date_time_req)); |
| 123 | |
| 124 | return {rc, requestMsg}; |
| 125 | } |
| 126 | |
| 127 | void parseResponseMsg(pldm_msg* responsePtr, size_t payloadLength) override |
| 128 | { |
| 129 | uint8_t completionCode = 0; |
| 130 | auto rc = decode_set_date_time_resp(responsePtr, payloadLength, |
| 131 | &completionCode); |
| 132 | |
| 133 | if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS) |
| 134 | { |
| 135 | std::cerr << "Response Message Error: " |
| 136 | << "rc=" << rc << ",cc=" << (int)completionCode |
| 137 | << std::endl; |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | std::cout << "SetDateTime: SUCCESS" << std::endl; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | uint64_t tmData; |
| 146 | }; |
| 147 | |
Sridevi Ramesh | 9857643 | 2019-11-27 10:10:28 -0600 | [diff] [blame] | 148 | void registerCommand(CLI::App& app) |
| 149 | { |
| 150 | auto bios = app.add_subcommand("bios", "bios type command"); |
| 151 | bios->require_subcommand(1); |
| 152 | auto getDateTime = bios->add_subcommand("GetDateTime", "get date time"); |
| 153 | commands.push_back( |
| 154 | std::make_unique<GetDateTime>("bios", "GetDateTime", getDateTime)); |
George Liu | d664936 | 2019-11-27 19:06:51 +0800 | [diff] [blame] | 155 | |
| 156 | auto setDateTime = |
| 157 | bios->add_subcommand("SetDateTime", "set host date time"); |
| 158 | commands.push_back( |
| 159 | std::make_unique<SetDateTime>("bios", "setDateTime", setDateTime)); |
Sridevi Ramesh | 9857643 | 2019-11-27 10:10:28 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace bios |
| 163 | |
| 164 | } // namespace pldmtool |