libpldm: bios: implement en/decode for SetDateTime
Add encode/decode functions.
Add unit-test.
Signed-off-by: Xiaochao Ma <maxiaochao@inspur.com>
Change-Id: Ide5563ea5b6446b7300e0d1a9f2b64275217c820
diff --git a/libpldm/bios.c b/libpldm/bios.c
index abf7df4..d5ba736 100644
--- a/libpldm/bios.c
+++ b/libpldm/bios.c
@@ -1,5 +1,7 @@
#include "bios.h"
+#include "utils.h"
#include <endian.h>
+#include <stdbool.h>
#include <string.h>
int encode_get_date_time_req(uint8_t instance_id, struct pldm_msg *msg)
@@ -86,6 +88,116 @@
return PLDM_SUCCESS;
}
+int encode_set_date_time_req(uint8_t instance_id, uint8_t seconds,
+ uint8_t minutes, uint8_t hours, uint8_t day,
+ uint8_t month, uint16_t year, struct pldm_msg *msg,
+ size_t payload_length)
+{
+ struct pldm_header_info header = {0};
+
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ if (payload_length != sizeof(struct pldm_set_date_time_req)) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ if (!is_time_legal(seconds, minutes, hours, day, month, year)) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ header.instance = instance_id;
+ header.msg_type = PLDM_REQUEST;
+ header.pldm_type = PLDM_BIOS;
+ header.command = PLDM_SET_DATE_TIME;
+ pack_pldm_header(&header, &msg->hdr);
+
+ struct pldm_set_date_time_req *request =
+ (struct pldm_set_date_time_req *)msg->payload;
+ request->seconds = dec2bcd8(seconds);
+ request->minutes = dec2bcd8(minutes);
+ request->hours = dec2bcd8(hours);
+ request->day = dec2bcd8(day);
+ request->month = dec2bcd8(month);
+ request->year = htole16(dec2bcd16(year));
+
+ return PLDM_SUCCESS;
+}
+
+int decode_set_date_time_req(const struct pldm_msg *msg, size_t payload_length,
+ uint8_t *seconds, uint8_t *minutes, uint8_t *hours,
+ uint8_t *day, uint8_t *month, uint16_t *year)
+{
+ if (msg == NULL || seconds == NULL || minutes == NULL ||
+ hours == NULL || day == NULL || month == NULL || year == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ if (payload_length != sizeof(struct pldm_set_date_time_req)) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ const struct pldm_set_date_time_req *request =
+ (struct pldm_set_date_time_req *)msg->payload;
+
+ *seconds = bcd2dec8(request->seconds);
+ *minutes = bcd2dec8(request->minutes);
+ *hours = bcd2dec8(request->hours);
+ *day = bcd2dec8(request->day);
+ *month = bcd2dec8(request->month);
+ *year = bcd2dec16(le16toh(request->year));
+
+ if (!is_time_legal(*seconds, *minutes, *hours, *day, *month, *year)) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ return PLDM_SUCCESS;
+}
+
+int encode_set_date_time_resp(uint8_t instance_id, uint8_t completion_code,
+ struct pldm_msg *msg, size_t payload_length)
+{
+ struct pldm_header_info header = {0};
+
+ if (msg == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+ if (payload_length != sizeof(struct pldm_only_cc_resp)) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ header.instance = instance_id;
+ header.msg_type = PLDM_RESPONSE;
+ header.pldm_type = PLDM_BIOS;
+ header.command = PLDM_SET_DATE_TIME;
+ int rc = pack_pldm_header(&header, &msg->hdr);
+ if (rc != PLDM_SUCCESS) {
+ return rc;
+ }
+
+ struct pldm_only_cc_resp *response =
+ (struct pldm_only_cc_resp *)msg->payload;
+ response->completion_code = completion_code;
+
+ return PLDM_SUCCESS;
+}
+
+int decode_set_date_time_resp(const struct pldm_msg *msg, size_t payload_length,
+ uint8_t *completion_code)
+{
+ if (msg == NULL || completion_code == NULL) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (payload_length != sizeof(struct pldm_only_cc_resp)) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ const struct pldm_only_cc_resp *response =
+ (struct pldm_only_cc_resp *)msg->payload;
+ *completion_code = response->completion_code;
+
+ return PLDM_SUCCESS;
+}
+
int encode_get_bios_table_resp(uint8_t instance_id, uint8_t completion_code,
uint32_t next_transfer_handle,
uint8_t transfer_flag, uint8_t *table_data,
diff --git a/libpldm/bios.h b/libpldm/bios.h
index 270413c..d316b67 100644
--- a/libpldm/bios.h
+++ b/libpldm/bios.h
@@ -30,6 +30,7 @@
PLDM_SET_BIOS_ATTRIBUTE_CURRENT_VALUE = 0x07,
PLDM_GET_BIOS_ATTRIBUTE_CURRENT_VALUE_BY_HANDLE = 0x08,
PLDM_GET_DATE_TIME = 0x0c,
+ PLDM_SET_DATE_TIME = 0x0d,
};
enum pldm_bios_table_types {
@@ -108,6 +109,28 @@
uint16_t year; //!< Year in BCD format
} __attribute__((packed));
+/** @struct pldm_set_date_time_req
+ *
+ * structure representing SetDateTime request packet
+ *
+ */
+struct pldm_set_date_time_req {
+ uint8_t seconds; //!< Seconds in BCD format
+ uint8_t minutes; //!< Minutes in BCD format
+ uint8_t hours; //!< Hours in BCD format
+ uint8_t day; //!< Day of the month in BCD format
+ uint8_t month; //!< Month in BCD format
+ uint16_t year; //!< Year in BCD format
+} __attribute__((packed));
+
+/** @struct pldm_only_cc_resp
+ *
+ * Structure representing PLDM responses only have completion code
+ */
+struct pldm_only_cc_resp {
+ uint8_t completion_code;
+} __attribute__((packed));
+
/** @struct pldm_get_bios_attribute_current_value_by_handle_req
*
* structure representing GetBIOSAttributeCurrentValueByHandle request packet
@@ -358,6 +381,65 @@
uint32_t next_transfer_handle,
struct pldm_msg *msg);
+/** @brief Create a PLDM request message for SetDateTime
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] seconds - Seconds in BCD format
+ * @param[in] minutes - minutes in BCD format
+ * @param[in] hours - hours in BCD format
+ * @param[in] day - day of month in BCD format
+ * @param[in] month - number of month in BCD format
+ * @param[in] year - year in BCD format
+ * @param[out] msg - Message will be written to this
+ * @param[in] payload_length - Length of request message payload
+ * @return pldm_completion_codes
+ * @note Caller is responsible for memory alloc and dealloc of param
+ * 'msg.body.payload'
+ */
+int encode_set_date_time_req(uint8_t instance_id, uint8_t seconds,
+ uint8_t minutes, uint8_t hours, uint8_t day,
+ uint8_t month, uint16_t year, struct pldm_msg *msg,
+ size_t payload_length);
+
+/** @brief Decode a SetDateTime request message
+ *
+ * @param[in] msg - Response message
+ * @param[in] payload_length - Length of request message payload
+ * @param[out] seconds - seconds in BCD format
+ * @param[out] minutes - minutes in BCD format
+ * @param[out] hours - hours in BCD format
+ * @param[out] day - day of the month in BCD format
+ * @param[out] month - number of month in BCD format
+ * @param[out] year - year in BCD format
+ * @return pldm_completion_codes
+ */
+int decode_set_date_time_req(const struct pldm_msg *msg, size_t payload_length,
+ uint8_t *seconds, uint8_t *minutes, uint8_t *hours,
+ uint8_t *day, uint8_t *month, uint16_t *year);
+
+/** @brief Create a PLDM response message for SetDateTime
+ *
+ * @param[in] instance_id - Message's instance id
+ * @param[in] completion_code - PLDM completion code
+ * @param[out] msg - Message will be written to this
+ * @param[in] payload_length - Length of response message payload
+ * @return pldm_completion_codes
+ * @note Caller is responsible for memory alloc and dealloc of param
+ * 'msg.body.payload'
+ */
+int encode_set_date_time_resp(uint8_t instance_id, uint8_t completion_code,
+ struct pldm_msg *msg, size_t payload_length);
+
+/** @brief Decode a SetDateTime response message
+ *
+ * @param[in] msg - Response message
+ * @param[in] payload_length - Length of response message payload
+ * @param[out] completion_code - Pointer to response msg's PLDM completion code
+ * @return pldm_completion_codes
+ */
+int decode_set_date_time_resp(const struct pldm_msg *msg, size_t payload_length,
+ uint8_t *completion_code);
+
#ifdef __cplusplus
}
#endif
diff --git a/libpldm/utils.c b/libpldm/utils.c
index ef49f6a..cd67a47 100644
--- a/libpldm/utils.c
+++ b/libpldm/utils.c
@@ -140,3 +140,23 @@
{
return dec2bcd16(dec % 10000) | dec2bcd16(dec / 10000) << 16;
}
+
+bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
+ uint8_t month, uint16_t year)
+{
+ if (month < 1 || month > 12) {
+ return false;
+ }
+ static const int days[13] = {0, 31, 28, 31, 30, 31, 30,
+ 31, 31, 30, 31, 30, 31};
+ int rday = days[month];
+ if (month == 2 &&
+ ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
+ rday += 1;
+ }
+ if (year < 1970 || day < 1 || day > rday || seconds > 59 ||
+ minutes > 59 || hours > 23) {
+ return false;
+ }
+ return true;
+}
diff --git a/libpldm/utils.h b/libpldm/utils.h
index 73bbf38..133fb4c 100644
--- a/libpldm/utils.h
+++ b/libpldm/utils.h
@@ -6,9 +6,9 @@
#endif
#include "pldm_types.h"
+#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
-
/** @brief Compute Crc32(same as the one used by IEEE802.3)
*
* @param[in] data - Pointer to the target data
@@ -62,6 +62,19 @@
*/
uint32_t dec2bcd32(uint32_t dec);
+/** @brief Check whether the input time is legal
+ *
+ * @param[in] seconds. Value range 0~59
+ * @param[in] minutes. Value range 0~59
+ * @param[in] hours. Value range 0~23
+ * @param[in] day. Value range 1~31
+ * @param[in] month. Value range 1~12
+ * @param[in] year. Value range 1970~
+ * @return true if time is legal,false if time is illegal
+ */
+bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
+ uint8_t month, uint16_t year);
+
#ifdef __cplusplus
}
#endif
diff --git a/test/libpldm_bios_test.cpp b/test/libpldm_bios_test.cpp
index d841da6..3e0938f 100644
--- a/test/libpldm_bios_test.cpp
+++ b/test/libpldm_bios_test.cpp
@@ -4,6 +4,7 @@
#include "libpldm/base.h"
#include "libpldm/bios.h"
+#include "libpldm/utils.h"
#include <gtest/gtest.h>
@@ -113,6 +114,219 @@
ASSERT_EQ(year, retYear);
}
+TEST(SetDateTime, testGoodEncodeResponse)
+{
+ uint8_t instanceId = 0;
+ uint8_t completionCode = PLDM_SUCCESS;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_set_date_time_resp(instanceId, completionCode, response,
+ responseMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+
+ struct pldm_only_cc_resp* resp =
+ reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
+ EXPECT_EQ(completionCode, resp->completion_code);
+}
+
+TEST(SetDateTime, testBadEncodeResponse)
+{
+
+ uint8_t instanceId = 10;
+ uint8_t completionCode = PLDM_SUCCESS;
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto rc = encode_set_date_time_resp(instanceId, completionCode, nullptr,
+ responseMsg.size() - hdrSize);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ rc = encode_set_date_time_resp(instanceId, completionCode, response,
+ responseMsg.size() - hdrSize - 1);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodDecodeResponse)
+{
+ uint8_t completionCode = PLDM_SUCCESS;
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ struct pldm_only_cc_resp* resp =
+ reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
+
+ resp->completion_code = completionCode;
+
+ uint8_t retCompletionCode;
+ auto rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize,
+ &retCompletionCode);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(completionCode, retCompletionCode);
+}
+
+TEST(SetDateTime, testBadDecodeResponse)
+{
+ uint8_t completionCode = PLDM_SUCCESS;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto rc = decode_set_date_time_resp(nullptr, responseMsg.size() - hdrSize,
+ &completionCode);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize - 1,
+ &completionCode);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodEncodeRequset)
+{
+ uint8_t instanceId = 0;
+ uint8_t seconds = 50;
+ uint8_t minutes = 20;
+ uint8_t hours = 10;
+ uint8_t day = 11;
+ uint8_t month = 11;
+ uint16_t year = 2019;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, request,
+ requestMsg.size() - hdrSize);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+
+ struct pldm_set_date_time_req* req =
+ reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
+ EXPECT_EQ(seconds, bcd2dec8(req->seconds));
+ EXPECT_EQ(minutes, bcd2dec8(req->minutes));
+ EXPECT_EQ(hours, bcd2dec8(req->hours));
+ EXPECT_EQ(day, bcd2dec8(req->day));
+ EXPECT_EQ(month, bcd2dec8(req->month));
+ EXPECT_EQ(year, le16toh(bcd2dec16(req->year)));
+}
+
+TEST(SetDateTime, testBadEncodeRequset)
+{
+ uint8_t instanceId = 0;
+
+ uint8_t seconds = 50;
+ uint8_t minutes = 20;
+ uint8_t hours = 10;
+ uint8_t day = 13;
+ uint8_t month = 11;
+ uint16_t year = 2019;
+
+ uint8_t erday = 43;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, nullptr,
+ requestMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, erday,
+ month, year, request,
+ requestMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, request,
+ requestMsg.size() - hdrSize - 4);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodDecodeRequest)
+{
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+ uint8_t seconds = 0x50;
+ uint8_t minutes = 0x20;
+ uint8_t hours = 0x10;
+ uint8_t day = 0x11;
+ uint8_t month = 0x11;
+ uint16_t year = 0x2019;
+
+ auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+ struct pldm_set_date_time_req* req =
+ reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
+ req->seconds = seconds;
+ req->minutes = minutes;
+ req->hours = hours;
+ req->day = day;
+ req->month = month;
+ req->year = htole16(year);
+
+ uint8_t retseconds;
+ uint8_t retminutes;
+ uint8_t rethours;
+ uint8_t retday;
+ uint8_t retmonth;
+ uint16_t retyear;
+
+ auto rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize,
+ &retseconds, &retminutes, &rethours,
+ &retday, &retmonth, &retyear);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(retseconds, 50);
+ EXPECT_EQ(retminutes, 20);
+ EXPECT_EQ(rethours, 10);
+ EXPECT_EQ(retday, 11);
+ EXPECT_EQ(retmonth, 11);
+ EXPECT_EQ(retyear, 2019);
+}
+
+TEST(SetDateTime, testBadDecodeRequest)
+{
+ uint8_t seconds = 0x50;
+ uint8_t minutes = 0x20;
+ uint8_t hours = 0x10;
+ uint8_t day = 0x11;
+ uint8_t month = 0x11;
+ uint16_t year = 0x2019;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+
+ decode_set_date_time_req(request, requestMsg.size() - hdrSize, &seconds,
+ &minutes, &hours, &day, &month, &year);
+
+ auto rc =
+ decode_set_date_time_req(nullptr, requestMsg.size() - hdrSize, &seconds,
+ &minutes, &hours, &day, &month, &year);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize, nullptr,
+ nullptr, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize - 4,
+ &seconds, &minutes, &hours, &day, &month,
+ &year);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
TEST(GetBIOSTable, testGoodEncodeResponse)
{
std::array<uint8_t,
diff --git a/test/libpldm_utils_test.cpp b/test/libpldm_utils_test.cpp
index b391f79..a40aec1 100644
--- a/test/libpldm_utils_test.cpp
+++ b/test/libpldm_utils_test.cpp
@@ -65,4 +65,19 @@
EXPECT_EQ(0x9999, dec2bcd16(9999));
EXPECT_EQ(0x12345678, dec2bcd32(12345678));
EXPECT_EQ(0x99999999, dec2bcd32(99999999));
+}
+
+TEST(TimeLegal, TimeLegal)
+{
+ EXPECT_EQ(true, is_time_legal(30, 25, 16, 18, 8, 2019));
+ EXPECT_EQ(true, is_time_legal(30, 25, 16, 29, 2, 2020)); // leap year
+
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 8, 1960)); // year illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 15, 2019)); // month illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 0, 2019)); // month illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 0, 8, 2019)); // day illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 29, 2, 2019)); // day illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 25, 18, 8, 2019)); // hours illegal
+ EXPECT_EQ(false, is_time_legal(30, 70, 16, 18, 8, 2019)); // minutes illegal
+ EXPECT_EQ(false, is_time_legal(80, 25, 16, 18, 8, 2019)); // seconds illegal
}
\ No newline at end of file