blob: 473dc5365d3c38b9759b2109b319c18add99dd38 [file] [log] [blame]
#include <endian.h>
#include <string.h>
#include "base.h"
int pack_pldm_header(const struct pldm_header_info *hdr,
struct pldm_msg_hdr *msg)
{
if (msg == NULL || hdr == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (hdr->msg_type != PLDM_RESPONSE && hdr->msg_type != PLDM_REQUEST &&
hdr->msg_type != PLDM_ASYNC_REQUEST_NOTIFY) {
return PLDM_ERROR_INVALID_DATA;
}
if (hdr->instance > PLDM_INSTANCE_MAX) {
return PLDM_ERROR_INVALID_DATA;
}
if (hdr->pldm_type > (PLDM_MAX_TYPES - 1)) {
return PLDM_ERROR_INVALID_PLDM_TYPE;
}
uint8_t datagram = (hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) ? 1 : 0;
if (hdr->msg_type == PLDM_RESPONSE) {
msg->request = PLDM_RESPONSE;
} else if (hdr->msg_type == PLDM_REQUEST ||
hdr->msg_type == PLDM_ASYNC_REQUEST_NOTIFY) {
msg->request = PLDM_REQUEST;
}
msg->datagram = datagram;
msg->reserved = 0;
msg->instance_id = hdr->instance;
msg->header_ver = 0;
msg->type = hdr->pldm_type;
msg->command = hdr->command;
return PLDM_SUCCESS;
}
int unpack_pldm_header(const struct pldm_msg_hdr *msg,
struct pldm_header_info *hdr)
{
if (msg == NULL) {
return PLDM_ERROR_INVALID_DATA;
}
if (msg->request == PLDM_RESPONSE) {
hdr->msg_type = PLDM_RESPONSE;
} else {
hdr->msg_type =
msg->datagram ? PLDM_ASYNC_REQUEST_NOTIFY : PLDM_REQUEST;
}
hdr->instance = msg->instance_id;
hdr->pldm_type = msg->type;
hdr->command = msg->command;
return PLDM_SUCCESS;
}