Hi,
I need to call the following dll(source + header) from my c# code.
further i want to do each operationin this dll by clicking a button.
i would really appreciate all the help in this regard...
/*--------------------Header
File-------------------------------------------------------*/
#ifndef _A664IO_DRIVER_
#define _A664IO_DRIVER_
/*==============================================================================
== Macros
==============================================================================*/
#ifdef WIN32
#define DllExport __declspec( dllexport )
#else
#define DllExport
#endif
/*==============================================================================
== Types
==============================================================================*/
typedef void * BOARD_HDL; // Handle to a board. Opaque because
different
// vendors require different structures
typedef void * PORT_HDL; // Handle to a port. Opaque because
different
// vendors require different structures
typedef enum
{
LINK_AUTO,
LINK_10MBPS,
LINK_100MBPS
}LINK_SPEED;
typedef enum
{
TX,
RX
}DIRECTION;
typedef enum // Command/function validity matrix: Board |
ES | VL | Port | Log
{
//---------------------------------------------------------------
STOP, // X |
X | X | X | X
START, // X |
X | X | X |
START_LOG_IN_NORMAL, // |
| | | X
START_LOG_LINK_DROPS, // |
| | | X
START_LOG_IC_DROPS, // |
| | | X
START_LOG_RM_DROPS, // |
| | | X
START_LOG_IP_DROPS, // |
| | | X
START_LOG_PROTO_DROPS, // |
| | | X
START_LOG_OUT, // |
| | | X
START_LOG_ALL // |
| | | X
// X: Command valid for function
}CONTROL;
typedef enum
{
API_FAILURE,
API_SUCCESS
}API_STATUS;
/*==============================================================================
== Manage API Internal Objects
==============================================================================*/
DllExport
void Open_API( void );
DllExport
void Close_API( void );
/*==============================================================================
== Load the configuration file
==============================================================================*/
DllExport
BOARD_HDL Init_Board( char * File_Name,
unsigned int Group_ID,
unsigned int Board_ID,
unsigned int PC_Num,
LINK_SPEED Link_Speed );
/*==============================================================================
== Obtain access to a port previously defined during configuration file
load
==============================================================================*/
DllExport
PORT_HDL Connect_Port( BOARD_HDL Board,
unsigned short ES_ID,
unsigned char Partition_ID,
char * Port_Name,
unsigned int Timeout,
DIRECTION Direction );
#if 0
/*==============================================================================
== Obtain access to a port previously defined during configuration
file load
==============================================================================*/
DllExport
PORT_HDL Connect_Port( BOARD_HDL Board,
unsigned int Port_unique_ID,
unsigned int Flags,
unsigned int Timeout,
unsigned int Direction);
#endif
/*==============================================================================
== Activate / De-activate a
== - Board
== - End System
== - Partition
== - Virtual Link
== - Port
== - Traffic Logging
==============================================================================*/
DllExport
API_STATUS Control_Board( BOARD_HDL Board,
CONTROL Ctrl );
DllExport
API_STATUS Control_ES( BOARD_HDL Board,
unsigned int ES_ID,
CONTROL Ctrl );
DllExport
API_STATUS Control_Partition( BOARD_HDL Board,
unsigned int ES_ID,
unsigned char Partition_ID,
CONTROL Ctrl );
DllExport
API_STATUS Control_VL( BOARD_HDL Board,
unsigned short VL_ID,
CONTROL Ctrl );
DllExport
API_STATUS Control_Port( BOARD_HDL Board,
PORT_HDL Port,
CONTROL Ctrl );
DllExport
API_STATUS Control_Log( BOARD_HDL Board,
CONTROL Ctrl,
char * Filter,
char * File_Name );
/*==============================================================================
== Refresh a sampling port or
== Transmit a new message in queuing port
==============================================================================*/
DllExport
API_STATUS Transmit( BOARD_HDL Board,
PORT_HDL Port,
char * Payload,
unsigned int * Size );
/*==============================================================================
== Obtain latest a sampling message or
== Received last queuing message
==============================================================================*/
DllExport
API_STATUS Receive( BOARD_HDL Board,
PORT_HDL Port,
char * Payload,
unsigned int * Size );
/*==============================================================================
== Obtain a string containing statistic counters
==============================================================================*/
DllExport
API_STATUS Get_Stats_Str( BOARD_HDL Board,
unsigned short VL_ID,
PORT_HDL Port,
char * Stats_Str );
/*==============================================================================
== Release a port or a board
==============================================================================*/
DllExport
API_STATUS Close_Port( PORT_HDL Port );
DllExport
API_STATUS Close_Board( BOARD_HDL Board );
/*==============================================================================
== Execute command string
==============================================================================*/
DllExport
API_STATUS Execute( char * Cmd_Str );
#endif
/*------------------Source
file------------------------------------------------*/
/*==============================================================================
== Includes
==============================================================================*/
#include <math.h>
#include < stdio.h>
#include "A664IO_Driver.h"
#include "cfdx.h"
/*==============================================================================
== Internal Type Definitions
==============================================================================*/
typedef struct
{
cfdx_t * Handle;
}BOARD_TYPE;
typedef struct
{
pdesc_t * Handle;
u_int32 Unique_ID;
}PORT_TYPE;
/*==============================================================================
== Internal Global Data
==============================================================================*/
// None
/*==============================================================================
== Error_Callback
==============================================================================*/
void Error_Callback( char * ebuf,
void * user_param )
{
printf( "Error on board %i\n", *(unsigned int*)user_param );
printf( "%s\n", ebuf );
return;
}
/*==============================================================================
== Init_API
==============================================================================*/
void Open_API( void )
{
return;
}
/*==============================================================================
== Init_Card
==============================================================================*/
BOARD_HDL Init_Board( char * File_Name,
unsigned int Group_ID,
unsigned int Board_ID,
unsigned int PC_Num,
LINK_SPEED Link_Speed )
{
int32 CFDX_Status = CFDX_FAILURE;
u_int8 * Config_Buffer = NULL;
u_int32 Buffer_Size = 0;
cfdx_t * Handle = NULL;
BOARD_TYPE * Board = NULL;
char Error_Buffer[CFDX_ERRBUF_SIZE];
u_int32 Option_Code = 0;
u_int32 Option_Value = 0;
u_int32 Option_Size = 0;
/*---------------------------------------------------------------------------
-- Load XML file in memory
---------------------------------------------------------------------------*/
CFDX_Status = cfdxcfg_load_buffer_from_file( File_Name,
Config_Buffer,
&Buffer_Size,
Error_Buffer );
Config_Buffer = (u_int8 *) malloc( Buffer_Size * sizeof(u_int8) );
CFDX_Status = cfdxcfg_load_buffer_from_file( File_Name,
Config_Buffer,
&Buffer_Size,
Error_Buffer );
if( CFDX_Status != CFDX_SUCCESS )
{
printf( "cfdxcfg_load_buffer_from_file(): %s\n", Error_Buffer );
return NULL;
}
/*---------------------------------------------------------------------------
-- Load configuration buffer into the driver
---------------------------------------------------------------------------*/
Handle = cfdx_open_board( Config_Buffer,
Buffer_Size,
Group_ID,
Board_ID,
CFDX_OPEN_MINIMIZE_LATENCY,
Error_Callback,
&Board_ID,
Error_Buffer );
if( Handle == NULL )
{
printf( "cfdx_open_board(): %s\n", Error_Buffer );
return NULL;
}
/*---------------------------------------------------------------------------
-- Set link type and speed
---------------------------------------------------------------------------*/
switch( Link_Speed )
{
case LINK_AUTO:
{
Option_Code = CFDX_ENABLE_AUTONEGOTIATION;
Option_Value = 0;
Option_Size = 0;
break;
}
case LINK_10MBPS:
{
Option_Code = CFDX_SET_LINKSPEED;
Option_Value = 10;
Option_Size = sizeof(Option_Value);
break;
}
case LINK_100MBPS:
{
Option_Code = CFDX_SET_LINKSPEED;
Option_Value = 100;
Option_Size = sizeof(Option_Value);
break;
}
default:
{
printf( "Init_Card(): Invalid link speed\n" );
}
}
CFDX_Status = cfdx_set_option( Handle,
ADAPTER_A,
Option_Code,
&Option_Value,
Option_Size );
if( CFDX_Status != CFDX_SUCCESS )
{
sprintf( Error_Buffer, "cfdx_set_option(): %s\n",
cfdx_get_last_error( Handle) );
return NULL;
}
CFDX_Status = cfdx_set_option( Handle,
ADAPTER_B,
Option_Code,
&Option_Value,
Option_Size );
if( CFDX_Status != CFDX_SUCCESS )
{
sprintf( Error_Buffer, "cfdx_set_option(): %s\n",
cfdx_get_last_error( Handle) );
return NULL;
}
/*---------------------------------------------------------------------------
-- Board successfuly initialized, return handle via opaque structure
---------------------------------------------------------------------------*/
Board = malloc( sizeof(BOARD_TYPE) );
Board->Handle = Handle;
/*---------------------------------------------------------------------------
-- The config buffer is not needed anymore
---------------------------------------------------------------------------*/
free( Config_Buffer );
return Board;
}
/*---------------------------------------------------------------------------
-- Convert Unique_ID to Handle
---------------------------------------------------------------------------*/
if( Unique_ID == 0 )
{
printf( "cfdx_get_port_unique_id():%s\n", cfdx_get_last_error(
B->Handle ) );
return NULL;
}
else
{
Handle = cfdx_open_port( B->Handle, Unique_ID, 0, 0 );
if( Handle == NULL )
{
printf( "cfdx_open_port(): %s\n", cfdx_get_last_error(
B->Handle ) );
return NULL;
}
}
/*---------------------------------------------------------------------------
-- Operation completed successfuly
-- -return handle and unique ID via opaque structure
---------------------------------------------------------------------------*/
Port = malloc( sizeof( PORT_TYPE ) );
Port->Handle = Handle;
Port->Unique_ID = Unique_ID;
return Port;
}
#if 0
/*==============================================================================
== Connect_Port
==============================================================================*/
PORT_HDL Connect_Port( BOARD_HDL Board,
unsigned int Port_unique_ID,
unsigned int Flags,
unsigned int Timeout,
unsigned int Direction)
{
BOARD_TYPE * B = Board;
pdesc_t * Handle = NULL;
PORT_TYPE * Port = NULL;
u_int32 Unique_ID = 0;
Handle = cfdx_open_port( B->Handle, Port_unique_ID, Flags, Timeout
);
if( Handle == NULL )
{
printf( "cfdx_open_port(): %s\n", cfdx_get_last_error(
B->Handle ) );
return NULL;
}
/*---------------------------------------------------------------------------
-- Operation completed successfuly
-- -return handle and unique ID via opaque structure
---------------------------------------------------------------------------*/
Port = malloc( sizeof( PORT_TYPE ) );
Port->Handle = Handle;
Port->Unique_ID = Unique_ID;
return Port;
}
#endif
/*==============================================================================
== Transmit
==============================================================================*/
API_STATUS Transmit( BOARD_HDL Board,
PORT_HDL Port,
char * Payload,
unsigned int * Size )
{
int32 CFDX_Status = CFDX_FAILURE;
BOARD_TYPE * B = Board;
PORT_TYPE * P = Port;
char Error_Buffer[CFDX_ERRBUF_SIZE];
CFDX_Status = cfdx_send_msg( P->Handle,
Payload,
Size,
NULL );
if( CFDX_Status != CFDX_SUCCESS )
{
printf( "cfdx_send_msg():%s\n", cfdx_get_last_error( B->Handle )
);
sprintf( Error_Buffer, "cfdx_send_msg():%s\n",
cfdx_get_last_error( B->Handle ) );
return API_FAILURE;
}
return API_SUCCESS;
}
/*==============================================================================
== Receive
==============================================================================*/
API_STATUS Receive( BOARD_HDL Board,
PORT_HDL Port,
char * Payload,
unsigned int * Size )
{
int32 CFDX_Status = CFDX_FAILURE;
BOARD_TYPE * B = Board;
PORT_TYPE * P = Port;
u_int32 Flags;
CFDX_Status = cfdx_recv_msg( P->Handle,
Payload,
Size,
&Flags );
if( CFDX_Status != CFDX_SUCCESS )
{
printf( "cfdx_recv_msg():%s\n", cfdx_get_last_error( B->Handle )
);
return API_FAILURE;
}
return API_SUCCESS;
}
if( CFDX_Status != CFDX_SUCCESS )
{
Status = API_FAILURE;
}
else
{
Status = API_SUCCESS;
}
free(Link_Stats);
free(Proto_Stats);
return Status;
}
/*==============================================================================
== Close_Port
==============================================================================*/
API_STATUS Close_Port( PORT_HDL Port )
{
BOARD_TYPE * P = Port;
free( P );
return API_SUCCESS;
}
/*==============================================================================
== Close_Board
==============================================================================*/
API_STATUS Close_Board( BOARD_HDL Board )
{
int32 CFDX_Status = CFDX_FAILURE;
BOARD_TYPE * B = Board;
CFDX_Status = cfdx_close_board( B->Handle );
if( CFDX_Status != CFDX_SUCCESS )
{
printf( "Close_Card(): %s\n", cfdx_get_last_error( B->Handle ) );
return API_FAILURE;
}
free( B );
return API_SUCCESS;
}