Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #include <endian.h> |
| 2 | #include <string.h> |
| 3 | |
| 4 | #include "bios.h" |
| 5 | |
| 6 | int 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 | |
| 21 | int 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 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 33 | msg->payload[0] = completion_code; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 34 | |
| 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 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 43 | uint8_t *dst = msg->payload + sizeof(msg->payload[0]); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 44 | |
| 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 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 61 | int decode_get_date_time_resp(const uint8_t *msg, size_t payload_length, |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 62 | 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 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 72 | if (payload_length != PLDM_GET_DATE_TIME_RESP_BYTES) { |
| 73 | return PLDM_ERROR_INVALID_LENGTH; |
| 74 | } |
| 75 | |
| 76 | *completion_code = msg[0]; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 77 | if (PLDM_SUCCESS != *completion_code) { |
| 78 | return PLDM_SUCCESS; |
| 79 | } |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 80 | *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 Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 88 | *year = le16toh( |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame^] | 89 | *((uint16_t *)(msg + sizeof(*completion_code) + sizeof(*seconds) + |
| 90 | sizeof(*minutes) + sizeof(*hours) + sizeof(*day) + |
| 91 | sizeof(*month)))); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 92 | |
| 93 | return PLDM_SUCCESS; |
| 94 | } |