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 | |
| 33 | msg->body.payload[0] = completion_code; |
| 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 | |
| 43 | uint8_t *dst = msg->body.payload + sizeof(msg->body.payload[0]); |
| 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 | |
| 61 | int decode_get_date_time_resp(const struct pldm_msg_payload *msg, |
| 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 | |
| 72 | *completion_code = msg->payload[0]; |
| 73 | if (PLDM_SUCCESS != *completion_code) { |
| 74 | return PLDM_SUCCESS; |
| 75 | } |
| 76 | const uint8_t *start = msg->payload + sizeof(uint8_t); |
| 77 | *seconds = *start; |
| 78 | *minutes = *(start + sizeof(*seconds)); |
| 79 | *hours = *(start + sizeof(*seconds) + sizeof(*minutes)); |
| 80 | *day = *(start + sizeof(*seconds) + sizeof(*minutes) + sizeof(*hours)); |
| 81 | *month = *(start + sizeof(*seconds) + sizeof(*minutes) + |
| 82 | sizeof(*hours) + sizeof(*day)); |
| 83 | *year = le16toh( |
| 84 | *((uint16_t *)(start + sizeof(*seconds) + sizeof(*minutes) + |
| 85 | sizeof(*hours) + sizeof(*day) + sizeof(*month)))); |
| 86 | |
| 87 | return PLDM_SUCCESS; |
| 88 | } |