/*****************************************************************************
 * Copyright (c) 1990-2000 Digi International Inc., All Rights Reserved
 * 
 * This software contains proprietary and confidential information of Digi
 * International Inc.  By accepting transfer of this copy, Recipient agrees
 * to retain this software in confidence, to prevent disclosure to others,
 * and to make no use of this software other than that for which it was
 * delivered.  This is an unpublished copyrighted work of Digi International
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 * prohibited.
 * 
 * Restricted Rights Legend
 *
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7031 or
 * subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 * Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 *****************************************************************************/

static const char span_c_rcsid[] = "@(#) " "$Id: span.c,v 1.5 2000/04/19 22:45:46 chrism Exp $";

/*

 * span.c -- used to span DLL bridge (by OS)
 *
 */

#include <stdio.h>
#include "span.h"
#include "dbgprintf.h"

typedef uint32(__stdcall * COMMAND_FUNC) (uint32 BoardNumber,		/* 1 to total boards or ignored if not applicable */
														uint16 Command,			/* obvious */
														void *mpkt,					/* msg packet.  This is the data portion of the ioctl. */
														uint16 mpktlen,			/* need to know its length */
														void *rpkt,					/* response packet.  This is the data portion of the ioctl response */
														uint16 rpktlen);			/* I hate access violations. */
COMMAND_FUNC pfnCommand = NULL;
uint32 CallMgmtCommand(uint32 BoardNumber, uint16 Command, void *mpkt, uint16 mpktlen, void *rpkt, uint16 rpktlen)
{
	return (pfnCommand == NULL) ? SPAN_ERROR : pfnCommand(BoardNumber, Command, mpkt, mpktlen, rpkt, rpktlen);
}

typedef uint32(__stdcall * TRACE_FUNC) (uint32 BoardNumber,			/* 1 to total boards */
													uint16 Command,				/* obvious */
													void *mpkt,						/* msg packet.  This is  the data portion of the ioctl. */
													uint16 mpktlen,				/* need to know its length */
													void *rpkt,						/* response packet.  This is the data portion of the ioctl response */
													uint16 rpktlen,				/* I hate access violations. */
													TRC_DATACOPY_CB cb,			/* This is the function I call when traces come in. */
													TRCARG handle);				/* cb functions"handle" */
TRACE_FUNC pfnTrace = NULL;
uint32 CallMgmtTrace(uint32 BoardNumber, uint16 Command, void *mpkt, uint16 mpktlen, void *rpkt, uint16 rpktlen, TRC_DATACOPY_CB cb, TRCARG handle)
{
	return (pfnTrace == NULL) ? SPAN_ERROR : pfnTrace(BoardNumber, Command, mpkt, mpktlen, rpkt, rpktlen, cb, handle);
}


/*================================================================================*/
/*================================================================================*/
#if defined PLATFORM_WINDOWS

#include <windows.h>

#define LIBRARY_FILENAME		"dxbmgt.dll"
#define COMMAND_PROCNAME		"_MgmtCommand@24"
#define TRACE_PROCNAME			"_MgmtTrace@32"

HINSTANCE hDLL = NULL;

void demolish_span(void)
{
	if (hDLL != NULL)
	{
		FreeLibrary(hDLL);
		hDLL = NULL;
	}

	pfnCommand = NULL;
	pfnTrace = NULL;
}

uint32 build_span()
{
	if ((hDLL = LoadLibrary(LIBRARY_FILENAME)) == NULL)
		goto Error;

	if ((pfnCommand = (COMMAND_FUNC) GetProcAddress(hDLL, COMMAND_PROCNAME)) == NULL)
		goto Error;

	if ((pfnTrace = (TRACE_FUNC) GetProcAddress(hDLL, TRACE_PROCNAME)) == NULL)
		goto Error;

	goto Done;

 Error:
	demolish_span();

 Done:
	return (pfnCommand != NULL && pfnTrace != NULL);
}


/*================================================================================*/
/*================================================================================*/
#elif defined PLATFORM_NETWARE

#include <nwthread.h>
#include <nwadv.h>

#define LIBRARY_FILENAME		"dgmngr.nlm"
#define COMMAND_PROCNAME		"MgmtCommand"
#define TRACE_PROCNAME			"MgmtTrace"

unsigned int hNLM = 0;


void demolish_span(void)
{
	if (hNLM != 0)
	{
		if (pfnCommand != NULL)
		{
			UnimportSymbol(hNLM, COMMAND_PROCNAME);
			pfnCommand = NULL;
		}

		if (pfnTrace != NULL)
		{
			UnimportSymbol(hNLM, TRACE_PROCNAME);
			pfnTrace = NULL;
		}

		hNLM = 0;
	}
}


uint32 build_span()
{
	if (FindNLMHandle(LIBRARY_FILENAME) == NULL)
		goto Error;

	hNLM = GetNLMHandle();

	if ((pfnCommand = (COMMAND_FUNC) ImportSymbol(hNLM, COMMAND_PROCNAME)) == NULL)
		goto Error;

	if ((pfnTrace = (TRACE_FUNC) ImportSymbol(hNLM, TRACE_PROCNAME)) == NULL)
		goto Error;

	goto Done;

 Error:
	demolish_span();

 Done:
	return (pfnCommand != NULL && pfnTrace != NULL);
}


/*================================================================================*/
/*================================================================================*/
#else	/* Unknown host platform */

#pragma message("No OS-Platform specified...using simulated management channel")


#include "stub.inc"


void demolish_span(void)
{
	pfnCommand = NULL;
	pfnTrace = NULL;
}


uint32 build_span(void)
{
	pfnCommand = stub_Command;
	pfnTrace = stub_Trace;

	return TRUE;
}


#endif
