blob: 021c6808a914bd40016d76faf1a7413e7d0b4da7 [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 header.msg_type = PLDM_RESPONSE;
35 header.instance = instance_id;
36 header.pldm_type = PLDM_BIOS;
37 header.command = PLDM_GET_DATE_TIME;
Zahed Hossain43264522019-06-04 02:21:03 -050038
Sampa Misra032bd502019-03-06 05:03:22 -060039 if ((rc = pack_pldm_header(&header, &(msg->hdr))) > PLDM_SUCCESS) {
40 return rc;
41 }
42
Zahed Hossain43264522019-06-04 02:21:03 -050043 if (msg->payload[0] == PLDM_SUCCESS) {
44 uint8_t *dst = msg->payload + sizeof(msg->payload[0]);
Sampa Misra032bd502019-03-06 05:03:22 -060045
Zahed Hossain43264522019-06-04 02:21:03 -050046 memcpy(dst, &seconds, sizeof(seconds));
47 dst += sizeof(seconds);
48 memcpy(dst, &minutes, sizeof(minutes));
49 dst += sizeof(minutes);
50 memcpy(dst, &hours, sizeof(hours));
51 dst += sizeof(hours);
52 memcpy(dst, &day, sizeof(day));
53 dst += sizeof(day);
54 memcpy(dst, &month, sizeof(month));
55 dst += sizeof(month);
56 uint16_t local_year = htole16(year);
57 memcpy(dst, &local_year, sizeof(local_year));
58 }
Sampa Misra032bd502019-03-06 05:03:22 -060059
60 return PLDM_SUCCESS;
61}
62
vkaverapa6575b82019-04-03 05:33:52 -050063int decode_get_date_time_resp(const uint8_t *msg, size_t payload_length,
Sampa Misra032bd502019-03-06 05:03:22 -060064 uint8_t *completion_code, uint8_t *seconds,
65 uint8_t *minutes, uint8_t *hours, uint8_t *day,
66 uint8_t *month, uint16_t *year)
67{
68 if (msg == NULL || seconds == NULL || minutes == NULL ||
69 hours == NULL || day == NULL || month == NULL || year == NULL ||
70 completion_code == NULL) {
71 return PLDM_ERROR_INVALID_DATA;
72 }
73
vkaverapa6575b82019-04-03 05:33:52 -050074 if (payload_length != PLDM_GET_DATE_TIME_RESP_BYTES) {
75 return PLDM_ERROR_INVALID_LENGTH;
76 }
77
78 *completion_code = msg[0];
Sampa Misra032bd502019-03-06 05:03:22 -060079 if (PLDM_SUCCESS != *completion_code) {
80 return PLDM_SUCCESS;
81 }
vkaverapa6575b82019-04-03 05:33:52 -050082 *seconds = *(msg + sizeof(*completion_code));
83 *minutes = *(msg + sizeof(*completion_code) + sizeof(*seconds));
84 *hours = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
85 sizeof(*minutes));
86 *day = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
87 sizeof(*minutes) + sizeof(*hours));
88 *month = *(msg + sizeof(*completion_code) + sizeof(*seconds) +
89 sizeof(*minutes) + sizeof(*hours) + sizeof(*day));
Sampa Misra032bd502019-03-06 05:03:22 -060090 *year = le16toh(
vkaverapa6575b82019-04-03 05:33:52 -050091 *((uint16_t *)(msg + sizeof(*completion_code) + sizeof(*seconds) +
92 sizeof(*minutes) + sizeof(*hours) + sizeof(*day) +
93 sizeof(*month))));
Sampa Misra032bd502019-03-06 05:03:22 -060094
95 return PLDM_SUCCESS;
96}