/*
 * Please do not edit this file.
 * It was generated using rpcgen.
 */

#include "vxi11.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif

/* VXI-11 RPCL definitions. Taken from appendix C of the VXI-11 specfication.
 * See http://www.vxi.org 
 */
#include <strings.h>
#include <stdlib.h>

/* VXI-11 flags */
#define VXI11_FLAG_WAITLOCK 0x01
#define VXI11_FLAG_ENDW 0x08
#define VXI11_FLAG_TERMCHRSET 0x80

/* VXI-11 device_docmd 'cmd' values */
#define VXI11_DOCMD_SEND_COMMAND 0x020000
#define VXI11_DOCMD_BUS_STATUS 0x020001
#define VXI11_DOCMD_ATN_CONTROL 0x020002
#define VXI11_DOCMD_REN_CONTROL 0x020003
#define VXI11_DOCMD_PASS_CONTROL 0x020004
#define VXI11_DOCMD_BUS_ADDRESS 0x02000A
#define VXI11_DOCMD_IFC_CONTROL 0x020010

/* VXI-11 device_docmd Bus Status values */
#define VXI11_DOCMD_STAT_REMOTE 1
#define VXI11_DOCMD_STAT_SRQ 2
#define VXI11_DOCMD_STAT_NDAC 3
#define VXI11_DOCMD_STAT_SYS_CTRLR 4
#define VXI11_DOCMD_STAT_CTRLR_CHRG 5
#define VXI11_DOCMD_STAT_TALKER 6
#define VXI11_DOCMD_STAT_LISTENER 7
#define VXI11_DOCMD_STAT_BUSADDR 8

/* VXI-11 device_read 'reason' bits */
#define VXI11_REASON_REQCNT 1 /* requested number of bytes read */
#define VXI11_REASON_CHR 2 /* read terminated by eos character */
#define VXI11_REASON_END 4 /* read terminated by EOI */

/* VXI-11 errors */
#define VXI11_ERR_SUCCESS 0
#define VXI11_ERR_SYNTAX 1
#define VXI11_ERR_NODEVICE 3
#define VXI11_ERR_LINKINVAL 4
#define VXI11_ERR_PARAMETER 5
#define VXI11_ERR_NOCHAN 6
#define VXI11_ERR_NOTSUPP 8
#define VXI11_ERR_RESOURCES 9
#define VXI11_ERR_LOCKED 11
#define VXI11_ERR_NOLOCK 12
#define VXI11_ERR_IOTIMEOUT 15
#define VXI11_ERR_IOERROR 17
#define VXI11_ERR_ADDRINVAL 21
#define VXI11_ERR_ABORT 23
#define VXI11_ERR_CHANEST 29

static void
device_async_1(struct svc_req *rqstp, register SVCXPRT *transp)
{
	union {
		Device_Link device_abort_1_arg;
	} argument;
	union {
		Device_Error device_abort_1_res;
	} result;
	bool_t retval;
	xdrproc_t _xdr_argument, _xdr_result;
	bool_t (*local)(char *, void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
		return;

	case device_abort:
		_xdr_argument = (xdrproc_t) xdr_Device_Link;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_abort_1_svc;
		break;

	default:
		svcerr_noproc (transp);
		return;
	}
	memset ((char *)&argument, 0, sizeof (argument));
	if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode (transp);
		return;
	}
	retval = (bool_t) (*local)((char *)&argument, (void *)&result, rqstp);
	if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *)&result)) {
		svcerr_systemerr (transp);
	}
	if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf (stderr, "%s", "unable to free arguments");
		exit (1);
	}
	if (!device_async_1_freeresult (transp, _xdr_result, (caddr_t) &result))
		fprintf (stderr, "%s", "unable to free results");

	return;
}

static void
device_core_1(struct svc_req *rqstp, register SVCXPRT *transp)
{
	union {
		Create_LinkParms create_link_1_arg;
		Device_WriteParms device_write_1_arg;
		Device_ReadParms device_read_1_arg;
		Device_GenericParms device_readstb_1_arg;
		Device_GenericParms device_trigger_1_arg;
		Device_GenericParms device_clear_1_arg;
		Device_GenericParms device_remote_1_arg;
		Device_GenericParms device_local_1_arg;
		Device_LockParms device_lock_1_arg;
		Device_Link device_unlock_1_arg;
		Device_EnableSrqParms device_enable_srq_1_arg;
		Device_DocmdParms device_docmd_1_arg;
		Device_Link destroy_link_1_arg;
		Device_RemoteFunc create_intr_chan_1_arg;
	} argument;
	union {
		Create_LinkResp create_link_1_res;
		Device_WriteResp device_write_1_res;
		Device_ReadResp device_read_1_res;
		Device_ReadStbResp device_readstb_1_res;
		Device_Error device_trigger_1_res;
		Device_Error device_clear_1_res;
		Device_Error device_remote_1_res;
		Device_Error device_local_1_res;
		Device_Error device_lock_1_res;
		Device_Error device_unlock_1_res;
		Device_Error device_enable_srq_1_res;
		Device_DocmdResp device_docmd_1_res;
		Device_Error destroy_link_1_res;
		Device_Error create_intr_chan_1_res;
		Device_Error destroy_intr_chan_1_res;
	} result;
	bool_t retval;
	xdrproc_t _xdr_argument, _xdr_result;
	bool_t (*local)(char *, void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
		return;

	case create_link:
		_xdr_argument = (xdrproc_t) xdr_Create_LinkParms;
		_xdr_result = (xdrproc_t) xdr_Create_LinkResp;
		local = (bool_t (*) (char *, void *,  struct svc_req *))create_link_1_svc;
		break;

	case device_write:
		_xdr_argument = (xdrproc_t) xdr_Device_WriteParms;
		_xdr_result = (xdrproc_t) xdr_Device_WriteResp;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_write_1_svc;
		break;

	case device_read:
		_xdr_argument = (xdrproc_t) xdr_Device_ReadParms;
		_xdr_result = (xdrproc_t) xdr_Device_ReadResp;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_read_1_svc;
		break;

	case device_readstb:
		_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
		_xdr_result = (xdrproc_t) xdr_Device_ReadStbResp;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_readstb_1_svc;
		break;

	case device_trigger:
		_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_trigger_1_svc;
		break;

	case device_clear:
		_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_clear_1_svc;
		break;

	case device_remote:
		_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_remote_1_svc;
		break;

	case device_local:
		_xdr_argument = (xdrproc_t) xdr_Device_GenericParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_local_1_svc;
		break;

	case device_lock:
		_xdr_argument = (xdrproc_t) xdr_Device_LockParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_lock_1_svc;
		break;

	case device_unlock:
		_xdr_argument = (xdrproc_t) xdr_Device_Link;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_unlock_1_svc;
		break;

	case device_enable_srq:
		_xdr_argument = (xdrproc_t) xdr_Device_EnableSrqParms;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_enable_srq_1_svc;
		break;

	case device_docmd:
		_xdr_argument = (xdrproc_t) xdr_Device_DocmdParms;
		_xdr_result = (xdrproc_t) xdr_Device_DocmdResp;
		local = (bool_t (*) (char *, void *,  struct svc_req *))device_docmd_1_svc;
		break;

	case destroy_link:
		_xdr_argument = (xdrproc_t) xdr_Device_Link;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))destroy_link_1_svc;
		break;

	case create_intr_chan:
		_xdr_argument = (xdrproc_t) xdr_Device_RemoteFunc;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))create_intr_chan_1_svc;
		break;

	case destroy_intr_chan:
		_xdr_argument = (xdrproc_t) xdr_void;
		_xdr_result = (xdrproc_t) xdr_Device_Error;
		local = (bool_t (*) (char *, void *,  struct svc_req *))destroy_intr_chan_1_svc;
		break;

	default:
		svcerr_noproc (transp);
		return;
	}
	memset ((char *)&argument, 0, sizeof (argument));
	if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		svcerr_decode (transp);
		return;
	}
	retval = (bool_t) (*local)((char *)&argument, (void *)&result, rqstp);
	if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *)&result)) {
		svcerr_systemerr (transp);
	}
	if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
		fprintf (stderr, "%s", "unable to free arguments");
		exit (1);
	}
	if (!device_core_1_freeresult (transp, _xdr_result, (caddr_t) &result))
		fprintf (stderr, "%s", "unable to free results");

	return;
}

int
main (int argc, char **argv)
{
	register SVCXPRT *transp;

	pmap_unset (DEVICE_ASYNC, DEVICE_ASYNC_VERSION);
	pmap_unset (DEVICE_CORE, DEVICE_CORE_VERSION);

	transp = svcudp_create(RPC_ANYSOCK);
	if (transp == NULL) {
		fprintf (stderr, "%s", "cannot create udp service.");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_UDP)) {
		fprintf (stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, udp).");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_UDP)) {
		fprintf (stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, udp).");
		exit(1);
	}

	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
	if (transp == NULL) {
		fprintf (stderr, "%s", "cannot create tcp service.");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_ASYNC, DEVICE_ASYNC_VERSION, device_async_1, IPPROTO_TCP)) {
		fprintf (stderr, "%s", "unable to register (DEVICE_ASYNC, DEVICE_ASYNC_VERSION, tcp).");
		exit(1);
	}
	if (!svc_register(transp, DEVICE_CORE, DEVICE_CORE_VERSION, device_core_1, IPPROTO_TCP)) {
		fprintf (stderr, "%s", "unable to register (DEVICE_CORE, DEVICE_CORE_VERSION, tcp).");
		exit(1);
	}

	svc_run ();
	fprintf (stderr, "%s", "svc_run returned");
	exit (1);
	/* NOTREACHED */
}
