blob: 6470dc32d0cc36555e484e9ba649c274a81985e1 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#include <endian.h>
2#include <string.h>
3
4#include "bios.h"
5
6int encode_get_date_time_req(uint8_t instance_id, struct pldm_msg *msg)
7{
8 struct pldm_header_info header = {0};
9
10 if (msg == NULL) {
11 return PLDM_ERROR_INVALID_DATA;
12 }
13
14 header.msg_type = PLDM_REQUEST;
15 header.instance = instance_id;
16 header.pldm_type = PLDM_BIOS;
17 header.command = PLDM_GET_DATE_TIME;
18 return pack_pldm_header(&header, &(msg->hdr));
19}
20
21int encode_get_date_time_resp(uint8_t instance_id, uint8_t completion_code,
22 uint8_t seconds, uint8_t minutes, uint8_t hours,
23 uint8_t day, uint8_t month, uint16_t year,
24 struct pldm_msg *msg)
25{
26 struct pldm_header_info header = {0};
27 int rc = PLDM_SUCCESS;
28
29 if (msg == NULL) {
30 return PLDM_ERROR_INVALID_DATA;
31 }
32
vkaverapa6575b82019-04-03 05:33:52 -050033 msg->payload[0] = completion_code;
Sampa Misra032bd502019-03-06 05:03:22 -060034
35 header.msg_type = PLDM_RESPONSE;
36 header.instance = instance_id;
37 header.pldm_type = PLDM_BIOS;
38 header.command = PLDM_GET_DATE_TIME;
39 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
40 return rc;
41 }
42
vkaverapa6575b82019-04-03 05:33:52 -050043 uint8_t *dst = msg->payload + sizeof(msg->payload[0]);
Sampa Misra032bd502019-03-06 05:03:22 -060044
45 memcpy(dst, &seconds, sizeof(seconds));
46 dst += sizeof(seconds);
47 memcpy(dst, &minutes, sizeof(minutes));
48 dst += sizeof(minutes);
49 memcpy(dst, &hours, sizeof(hours));
50 dst += sizeof(hours);
51 memcpy(dst, &day, sizeof(day));
52 dst += sizeof(day);
53 memcpy(dst, &month, sizeof(month));
54 dst += sizeof(month);
55 uint16_t local_year = htole16(year);
56 memcpy(dst, &local_year, sizeof(local_year));
57
58 return PLDM_SUCCESS;
59}
60
vkaverapa6575b82019-04-03 05:33:52 -050061int decode_get_date_time_resp(const uint8_t *msg, size_t payload_length,
Sampa Misra032bd502019-03-06 05:03:22 -060062 uint8_t *completion_code, uint8_t *seconds,
63 uint8_t *minutes, uint8_t *hours, uint8_t *day,
64 uint8_t *month, uint16_t *year)
65{
66 if (msg == NULL || seconds == NULL || minutes == NULL ||
67 hours == NULL || day == NULL || month == NULL || year == NULL ||
68 completion_code == NULL) {
69 return PLDM_ERROR_INVALID_DATA;
70 }
71
vkaverapa6575b82019-04-03 05:33:52 -050072 if (payload_length != PLDM_GET_DATE_TIME_RESP_BYTES) {
73 return PLDM_ERROR_INVALID_LENGTH;
74 }
75
76 *completion_code = msg[0];
Sampa Misra032bd502019-03-06 05:03:22 -060077 if (PLDM_SUCCESS != *completion_code) {
78 return PLDM_SUCCESS;
79 }
vkaverapa6575b82019-04-03 05:33:52 -050080 *seconds = *(msg + sizeof(*completion_code));
81 *minutes = *(msg + sizeof(*completion_code) + sizeof(*seconds));
82 *hours = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
83 sizeof(*minutes));
84 *day = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
85 sizeof(*minutes) + sizeof(*hours));
86 *month = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
87 sizeof(*minutes) + sizeof(*hours) + sizeof(*day));
Sampa Misra032bd502019-03-06 05:03:22 -060088 *year = le16toh(
vkaverapa6575b82019-04-03 05:33:52 -050089 *((uint16_t *)(msg + sizeof(*completion_code) + sizeof(*seconds) +
90 sizeof(*minutes) + sizeof(*hours) + sizeof(*day) +
91 sizeof(*month))));
Sampa Misra032bd502019-03-06 05:03:22 -060092
93 return PLDM_SUCCESS;
94}