uint8_t mctp_pldm_cmd_handler()

in common/pldm/pldm.c [133:203]


uint8_t mctp_pldm_cmd_handler(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params ext_params)
{
	if (!mctp_p || !buf || !len)
		return PLDM_ERROR;

	mctp *mctp_inst = (mctp *)mctp_p;
	pldm_hdr *hdr = (pldm_hdr *)buf;
	LOG_DBG("msg_type = %d", hdr->msg_type);
	LOG_DBG("req_d_id = 0x%x", hdr->req_d_id);
	LOG_DBG("pldm_type = 0x%x", hdr->pldm_type);
	LOG_DBG("cmd = 0x%x", hdr->cmd);

	/*
* The message is a response, check if any callback function should be
* invoked
*/
	if (!hdr->rq)
		return pldm_resp_msg_process(mctp_inst, buf, len, ext_params);

	/* the message is a request, find the proper handler to handle it */

	/* initial response data */
	uint8_t resp_buf[PLDM_MAX_DATA_SIZE] = { 0 };
	/*
* Default without header length, the header length will be added before
* sending.
*/
	uint16_t resp_len = 1;

	/* make response header */
	hdr->rq = 0;
	memcpy(resp_buf, hdr, sizeof(*hdr));

	/* default one byte response data - completion code */
	uint8_t *comp = resp_buf + sizeof(*hdr);

	pldm_cmd_proc_fn handler = NULL;
	uint8_t (*handler_query)(uint8_t, void **) = NULL;

	uint8_t i;
	for (i = 0; i < ARRAY_SIZE(query_tbl); i++) {
		if (hdr->pldm_type == query_tbl[i].type) {
			handler_query = query_tbl[i].handler_query;
			break;
		}
	}

	if (!handler_query) {
		*comp = PLDM_BASE_CODES_ERROR_UNSUPPORT_PLDM_TYPE;
		goto send_msg;
	}

	uint8_t rc = PLDM_ERROR;
	/* found the proper cmd handler in the pldm_type_cmd table */
	rc = handler_query(hdr->cmd, (void **)&handler);
	if (rc == PLDM_ERROR || !handler) {
		*comp = PLDM_BASE_CODES_ERROR_UNSUPPORT_PLDM_CMD;
		goto send_msg;
	}

	/* invoke the cmd handler to process */
	rc = handler(mctp_inst, buf + sizeof(*hdr), len - sizeof(*hdr), resp_buf + sizeof(*hdr),
		     &resp_len, &ext_params);
	if (rc == PLDM_LATER_RESP)
		return PLDM_SUCCESS;

send_msg:
	/* send the pldm response data */
	resp_len = sizeof(*hdr) + resp_len;
	return mctp_send_msg(mctp_inst, resp_buf, resp_len, ext_params);
}