/*
 ============================================================================
 Name        : ipmiToolTest.c
 Author      : nick
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "ipmitool/ipmi_intf.h"
#include "ipmitool/log.h"

int verbose = 0;
int csv_output = 0;

int main(void)
{
	static struct ipmi_intf * ipmi_main_intf = NULL;

	int privlvl = IPMI_SESSION_PRIV_ADMIN;

	int retry = 1;
	uint32_t timeout = 15;
	int authtype = IPMI_SESSION_AUTHTYPE_MD5;

	char * hostname = "192.168.1.135";
	char * username = "admin";
	char * password = "admin";
	char * intfname = "lan";
	char * progname = NULL;
	char * oemtype  = NULL;

	int cipher_suite_id = 4; /* See table 22-19 of the IPMIv2 spec */



	/* load interface */
	ipmi_main_intf = ipmi_intf_load(intfname);
	if (ipmi_main_intf == NULL) {
		lprintf(LOG_ERR, "Error loading interface %s", intfname);
		goto out_free;
	}

	/* setup log */
	log_init(progname, 0, verbose);

	/* run OEM setup if found */
	if (oemtype != NULL &&
	    ipmi_oem_setup(ipmi_main_intf, oemtype) < 0) {
		lprintf(LOG_ERR, "OEM setup for \"%s\" failed", oemtype);
		goto out_free;
	}

	/* set session variables */
	if (hostname != NULL)
		ipmi_intf_session_set_hostname(ipmi_main_intf, hostname);
	if (username != NULL)
		ipmi_intf_session_set_username(ipmi_main_intf, username);
	if (password != NULL)
		ipmi_intf_session_set_password(ipmi_main_intf, password);


	if (authtype >= 0)
		ipmi_intf_session_set_authtype(ipmi_main_intf, (uint8_t)authtype);
	if (privlvl > 0)
		ipmi_intf_session_set_privlvl(ipmi_main_intf, (uint8_t)privlvl);
	else
		ipmi_intf_session_set_privlvl(ipmi_main_intf,
				IPMI_SESSION_PRIV_ADMIN);	/* default */
	/* Adding retry and timeout for interface that support it */
	if (retry > 0)
		ipmi_intf_session_set_retry(ipmi_main_intf, retry);
	if (timeout > 0)
		ipmi_intf_session_set_timeout(ipmi_main_intf, timeout);

	ipmi_intf_session_set_cipher_suite_id(ipmi_main_intf, cipher_suite_id);


	struct ipmi_rs * rsp;
	struct ipmi_rq req;

	uint8_t data[256];


	memset(data, 0, sizeof(data));
	memset(&req, 0, sizeof(req));
	req.msg.netfn = IPMI_NETFN_APP;
	req.msg.lun = 0;
	req.msg.cmd = 0x01;
	req.msg.data = data;

	rsp = ipmi_main_intf->sendrecv(ipmi_main_intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "Unable to send RAW command "
			"(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x)",
			ipmi_main_intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd);
		goto out_free;
	}
	if (rsp->ccode > 0) {
		lprintf(LOG_ERR, "Unable to send RAW command "
			"(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x rsp=0x%x): %s",
			ipmi_main_intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd, rsp->ccode,
			val2str(rsp->ccode, completion_code_vals));
		goto out_free;
	}

	lprintf(LOG_INFO, "RAW RSP (%d bytes)", rsp->data_len);

	/* print the raw response buffer */
	int i = 0;
	for (i=0; i<rsp->data_len; i++) {
		if (((i%16) == 0) && (i != 0))
			printf("\n");
		printf(" %2.2x", rsp->data[i]);
	}
	printf("\n");

	out_free:
	/* clean repository caches */
	ipmi_cleanup(ipmi_main_intf);

	/* call interface close function if available */
	if (ipmi_main_intf->opened > 0 && ipmi_main_intf->close != NULL)
		ipmi_main_intf->close(ipmi_main_intf);

	log_halt();
	return 0;

}
