| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <windows.h> |
| 31 |
#include <winioctl.h> |
| 32 |
|
| 33 |
#include "rwp.h" |
| 34 |
#include "./rwp/rwports.h" |
| 35 |
|
| 36 |
#define RWP_DRV_NAME "RwPorts" |
| 37 |
|
| 38 |
#if defined(__WIN32__) || defined(_WIN64) |
| 39 |
|
| 40 |
#if !defined(__MINGW32__) || defined(_WIN64) |
| 41 |
|
| 42 |
|
| 43 |
static void print_last_error(const char *s) |
| 44 |
{ |
| 45 |
LPTSTR lpMsgBuf; |
| 46 |
|
| 47 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 48 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 49 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 50 |
NULL, GetLastError(), |
| 51 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 52 |
(LPTSTR) & lpMsgBuf, 0, NULL); |
| 53 |
|
| 54 |
lpMsgBuf[strlen(lpMsgBuf)-2] = '\0'; |
| 55 |
|
| 56 |
printf("%s (%lu -> %s)\n", s, (unsigned long)GetLastError(), lpMsgBuf); |
| 57 |
} |
| 58 |
#endif |
| 59 |
|
| 60 |
int rwp_detect(int* result) |
| 61 |
{ |
| 62 |
HANDLE hDriver; |
| 63 |
*result = 0; |
| 64 |
|
| 65 |
hDriver = CreateFile("\\\\.\\RwPorts", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 66 |
if(hDriver == INVALID_HANDLE_VALUE) |
| 67 |
{ |
| 68 |
hDriver = CreateFile("\\\\.\\RwPorts", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 69 |
if(hDriver != INVALID_HANDLE_VALUE) |
| 70 |
{ |
| 71 |
CloseHandle(hDriver); |
| 72 |
hDriver = INVALID_HANDLE_VALUE; |
| 73 |
*result = 1; |
| 74 |
} |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
CloseHandle(hDriver); |
| 79 |
hDriver = INVALID_HANDLE_VALUE; |
| 80 |
*result = 1; |
| 81 |
} |
| 82 |
|
| 83 |
printf(RWP_DRV_NAME "%sfound.\n", *result ? " " : " not "); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static HANDLE hDriver = INVALID_HANDLE_VALUE; |
| 88 |
|
| 89 |
int rwp_open(void) |
| 90 |
{ |
| 91 |
hDriver = CreateFile("\\\\.\\RwPorts", |
| 92 |
GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 93 |
if(hDriver == INVALID_HANDLE_VALUE) |
| 94 |
{ |
| 95 |
printf("Couldn't access " RWP_DRV_NAME " driver; Please ensure driver is installed/loaded."); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
int rwp_close(void) |
| 103 |
{ |
| 104 |
CloseHandle(hDriver); |
| 105 |
hDriver = INVALID_HANDLE_VALUE; |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
int rwp_read_byte(unsigned short address, unsigned char *data) |
| 111 |
{ |
| 112 |
DWORD BytesReturned; |
| 113 |
int iError; |
| 114 |
int result = 0; |
| 115 |
unsigned char buf[3]; |
| 116 |
|
| 117 |
*((unsigned short *)buf) = (unsigned short)address; |
| 118 |
iError = DeviceIoControl(hDriver, IOCTL_RWPORTS_READ_BYTE, &buf, 2, &buf, 1, &BytesReturned, NULL); |
| 119 |
if(!iError) |
| 120 |
{ |
| 121 |
print_last_error("RwPorts: error occurred in IOCTL_RWPORTS_READ_BYTE"); |
| 122 |
result = -1; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
*data = buf[0]; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
int rwp_write_byte(unsigned short address, unsigned char data) |
| 134 |
{ |
| 135 |
DWORD BytesReturned; |
| 136 |
int iError; |
| 137 |
int result = 0; |
| 138 |
unsigned char buf[3]; |
| 139 |
|
| 140 |
*((unsigned short *)buf) = (unsigned short)address; buf[2] = data; |
| 141 |
iError = DeviceIoControl(hDriver, IOCTL_RWPORTS_WRITE_BYTE, &buf, 3, &buf, 1, &BytesReturned, NULL); |
| 142 |
if(!iError) |
| 143 |
{ |
| 144 |
print_last_error("RwPorts: error occurred in IOCTL_RWPORTS_WRITE_BYTE"); |
| 145 |
result = -1; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
return result; |
| 150 |
} |
| 151 |
|
| 152 |
#endif |