// HAL.c : Defines the entry point for the DLL application. // //#include "stdafx.h" #include #include #include #include #include "inp_HAL_HAL.h" #define MAXN 10 // global WinDriver handle HANDLE hWD; WD_VERSION verBuf; WD_LICENSE lic; HANDLE globalEvent; // interrupt structure typedef struct { WD_CARD_REGISTER cardReg; WD_INTERRUPT Intrp; HANDLE thread_handle; HANDLE event_handle; BOOLEAN active; } HW_OBJECT; HW_OBJECT hwo[MAXN]; BOOL APIENTRY DllMain( HANDLE hModule, DWORD reason, LPVOID lpReserved ) { if(reason == DLL_PROCESS_ATTACH ) { //printf("Loading!!!"); hWD = INVALID_HANDLE_VALUE; hWD = WD_Open(); if (hWD==INVALID_HANDLE_VALUE) { printf("Invalid handle"); return FALSE; } BZERO(verBuf); WD_Version (hWD, &verBuf); if (verBuf.dwVer=MAXN) return -1001; if(hwo[cardN].event_handle==NULL) return -1002; switch ( WaitForSingleObject(hwo[cardN].event_handle, (timeout>=0) ? (DWORD)timeout : INFINITE ) ) { case WAIT_OBJECT_0: return 0; case WAIT_TIMEOUT: return 1; default: return -1000; } } /* * Class: inp_HAL_HAL * Method: init * Signature: (III)I */ JNIEXPORT jint JNICALL Java_inp_HAL_HAL_init ( JNIEnv * env, jobject o, jint MY_IO_BASE, jint MY_IO_SIZE, jint MY_IRQ ){ DWORD cnt=0; int handle; for(handle=0;handle0) { hwo[handle].cardReg.Card.Item[cnt].item = ITEM_INTERRUPT; hwo[handle].cardReg.Card.Item[cnt].fNotSharable = TRUE; hwo[handle].cardReg.Card.Item[cnt].I.Int.dwInterrupt = MY_IRQ; hwo[handle].cardReg.Card.Item[cnt].I.Int.dwOptions = 0; cnt++; } hwo[handle].cardReg.Card.dwItems = cnt; WD_CardRegister (hWD, &hwo[handle].cardReg); if (hwo[handle].cardReg.hCard==0) return -1; if(MY_IRQ>0) { hwo[handle].Intrp.hInterrupt = hwo[handle].cardReg.Card.Item[1].I.Int.hInterrupt; hwo[handle].Intrp.Cmd = NULL; hwo[handle].Intrp.dwCmds = 0; hwo[handle].Intrp.dwOptions = 0; hwo[handle].event_handle = CreateEvent( NULL, // no security attributes FALSE, // auto-reset event FALSE, // initial state is notsignaled NULL); // object not named if (!InterruptThreadEnable(&hwo[handle].thread_handle, hWD, &hwo[handle].Intrp, interrupt_handler, &hwo[handle])) { return -1; } } hwo[handle].active=TRUE; return handle; } /* * Class: inp_HAL_HAL * Method: waitInt * Signature: ()V */ /* * Class: inp_HAL_HAL * Method: inByte * Signature: (I)B */ JNIEXPORT jbyte JNICALL Java_inp_HAL_HAL_inByte (JNIEnv * env, jobject o, jint dwIOAddr) { WD_TRANSFER trns; BZERO(trns); trns.cmdTrans = RP_BYTE; // R-Read P-Port BYTE trns.dwPort = (DWORD)dwIOAddr; WD_Transfer( hWD, &trns); // Perform read return (jbyte)(trns.Data.Byte); } /* * Class: inp_HAL_HAL * Method: inWord * Signature: (I)S */ JNIEXPORT jshort JNICALL Java_inp_HAL_HAL_inWord (JNIEnv * env, jobject o, jint dwIOAddr){ WD_TRANSFER trns; BZERO(trns); trns.cmdTrans = RP_WORD; // R-Read P-Port BYTE trns.dwPort = (DWORD)dwIOAddr; WD_Transfer( hWD, &trns); // Perform read return (jshort)(trns.Data.Word); } /* * Class: inp_HAL_HAL * Method: outByte * Signature: (IB)V */ JNIEXPORT void JNICALL Java_inp_HAL_HAL_outByte (JNIEnv * env, jobject o , jint dwIOAddr, jbyte bData){ WD_TRANSFER trns; BZERO(trns); trns.cmdTrans = WP_BYTE; // R-Write P-Port BYTE trns.dwPort = (DWORD)dwIOAddr; trns.Data.Byte = (BYTE)bData; WD_Transfer( hWD, &trns); // Perform write } /* * Class: inp_HAL_HAL * Method: outWord * Signature: (IS)V */ JNIEXPORT void JNICALL Java_inp_HAL_HAL_outWord (JNIEnv * env, jobject o, jint dwIOAddr, jshort wData) { WD_TRANSFER trns; BZERO(trns); trns.cmdTrans = WP_WORD; // R-Write P-Port Wod trns.dwPort = dwIOAddr; trns.Data.Word = (USHORT)wData; WD_Transfer( hWD, &trns); // Perform write } /* * Class: inp_HAL_HAL * Method: release * Signature: (I)V */ JNIEXPORT void JNICALL Java_inp_HAL_HAL_release (JNIEnv * env, jobject o, jint handle) { if(handle >= 0) { if(hwo[handle].thread_handle!=NULL) { InterruptThreadDisable(hwo[handle].thread_handle); hwo[handle].thread_handle=NULL; } WD_CardUnregister(hWD, &hwo[handle].cardReg); hwo[handle].active=FALSE; } }