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