| 1 |
/* Hey EMACS -*- win32-c -*- */ |
| 2 |
/* $Id$ */ |
| 3 |
|
| 4 |
/* libticables2 - link cable library, a part of the TiLP project |
| 5 |
* Copyright (C) 1999-2005 Romain Lievin |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software Foundation, |
| 19 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/* "VTi" virtual link cable unit */ |
| 23 |
|
| 24 |
/* |
| 25 |
* This unit uses two circular buffer implemented as shared memory. |
| 26 |
* Names of shm are exchanged thru a messaging system. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <windows.h> |
| 31 |
|
| 32 |
#include "../ticables.h" |
| 33 |
#include "../logging.h" |
| 34 |
#include "../error.h" |
| 35 |
#include "../gettext.h" |
| 36 |
#include "../internal.h" |
| 37 |
#include "detect.h" |
| 38 |
|
| 39 |
// VTi messages |
| 40 |
#define WM_HELLO WM_USER+101 |
| 41 |
#define WM_GOODBYE WM_USER+102 |
| 42 |
#define WM_ENABLE_LINK WM_USER+110 // used |
| 43 |
#define WM_DISABLE_LINK WM_USER+111 |
| 44 |
#define WM_SEND_BUFFER WM_USER+120 // used |
| 45 |
#define WM_ENTER_DEBUG WM_USER+130 |
| 46 |
#define WM_EXIT_DEBUG WM_USER+131 |
| 47 |
|
| 48 |
#define BUFSIZE 256 |
| 49 |
|
| 50 |
/* VTi's LinkBuffer structure */ |
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
uint8_t buf[BUFSIZE]; |
| 54 |
int start; |
| 55 |
int end; |
| 56 |
} LinkBuffer; |
| 57 |
|
| 58 |
static LinkBuffer* vSendBuf = NULL; |
| 59 |
static LinkBuffer* vRecvBuf = NULL; |
| 60 |
static HANDLE hMap = NULL; // Handle on file-mapping object |
| 61 |
static HWND otherWnd = NULL; // Handle on the VTi window |
| 62 |
|
| 63 |
static int vti_prepare(CableHandle *h) |
| 64 |
{ |
| 65 |
// in fact, address & device are unused |
| 66 |
switch(h->port) |
| 67 |
{ |
| 68 |
case PORT_0: // automatic setting |
| 69 |
h->address = 1; h->device = strdup("TiLP"); |
| 70 |
break; |
| 71 |
case PORT_1: // reserved because used by VTi |
| 72 |
h->address = 0; h->device = strdup("VTi"); |
| 73 |
break; |
| 74 |
case PORT_2: // |
| 75 |
h->address = 1; h->device = strdup("TiLP"); |
| 76 |
break; |
| 77 |
default: return ERR_ILLEGAL_ARG; |
| 78 |
} |
| 79 |
|
| 80 |
return 0; |
| 81 |
} |
| 82 |
|
| 83 |
static int vti_open(CableHandle *h) |
| 84 |
{ |
| 85 |
int i; |
| 86 |
char vLinkFileName[32]; |
| 87 |
char name[32]; |
| 88 |
HANDLE hVLinkFileMap = NULL; |
| 89 |
HANDLE Handle; |
| 90 |
ATOM a; |
| 91 |
|
| 92 |
/* Get an handle on the VTi window */ |
| 93 |
otherWnd = FindWindow("TEmuWnd", NULL); |
| 94 |
if (!otherWnd) |
| 95 |
{ |
| 96 |
return ERR_VTI_FINDWINDOW; |
| 97 |
} |
| 98 |
|
| 99 |
/* Get the current DLL handle */ |
| 100 |
Handle = GetModuleHandle("libticables2.dll"); |
| 101 |
if (!Handle) |
| 102 |
{ |
| 103 |
Handle = GetModuleHandle("libticables2-8.dll"); |
| 104 |
} |
| 105 |
|
| 106 |
if (!Handle) |
| 107 |
{ |
| 108 |
ticables_critical("%s", _("FATAL ERROR: unable to get an handle on the ticables-2 library.")); |
| 109 |
ticables_critical("%s", _("Did you rename the library ?!")); |
| 110 |
return ERR_NO_LIBRARY; |
| 111 |
} |
| 112 |
|
| 113 |
/* Create a file mapping handle for the 'lib->VTi' communication channel */ |
| 114 |
for (i = 0; ; i++) |
| 115 |
{ |
| 116 |
sprintf(vLinkFileName, "Virtual Link %d", i); |
| 117 |
hVLinkFileMap = CreateFileMapping((HANDLE) - 1, NULL, |
| 118 |
PAGE_READWRITE, 0, |
| 119 |
sizeof(LinkBuffer), vLinkFileName); |
| 120 |
|
| 121 |
if (GetLastError() != ERROR_ALREADY_EXISTS) |
| 122 |
{ |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
ticables_info("Virtual Link L->V %i", i); |
| 128 |
vSendBuf = (LinkBuffer *)MapViewOfFile(hVLinkFileMap, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkBuffer)); |
| 129 |
|
| 130 |
// Inform VTI of our virtual link so that it can enable it. It should return |
| 131 |
// its virtual link name in a message. |
| 132 |
SendMessage(otherWnd, WM_HELLO, 0, (LPARAM) Handle); |
| 133 |
|
| 134 |
/* Retrieve the VTi virtual link name */ |
| 135 |
//b = GetMessage(&msg, NULL, WM_HELLO, WM_SEND_BUFFER); |
| 136 |
//WaitMessage(); // Waits VTi answer |
| 137 |
|
| 138 |
/* Create a file mapping handle for the 'Vti->lib' communication channel */ |
| 139 |
ticables_info("Virtual Link V->L %i", i-1); |
| 140 |
sprintf(name, "Virtual Link %d", i - 1); |
| 141 |
hMap = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name); |
| 142 |
if (hMap) |
| 143 |
{ |
| 144 |
ticables_info(_("Opened %s"), name); |
| 145 |
vRecvBuf = (LinkBuffer *)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkBuffer)); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
return ERR_VTI_OPENFILEMAPPING; |
| 150 |
} |
| 151 |
|
| 152 |
/* Send to VTi the name of our virtual link. VTi should open it (lib -> Vti) */ |
| 153 |
a = GlobalAddAtom(vLinkFileName); |
| 154 |
SendMessage(otherWnd, WM_SEND_BUFFER, 0, (LPARAM) a); |
| 155 |
GlobalDeleteAtom(a); |
| 156 |
|
| 157 |
/* Enable linking (check the VTi's Virtual Link|Enable cable link' item) */ |
| 158 |
if (otherWnd) |
| 159 |
{ |
| 160 |
SendMessage(otherWnd, WM_ENABLE_LINK, 0, 0); |
| 161 |
} |
| 162 |
|
| 163 |
vSendBuf->start = vSendBuf->end = 0; |
| 164 |
vRecvBuf->start = vRecvBuf->end = 0; |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int vti_close(CableHandle *h) |
| 170 |
{ |
| 171 |
if (otherWnd) |
| 172 |
{ |
| 173 |
SendMessage(otherWnd, WM_DISABLE_LINK, 0, 0); |
| 174 |
SendMessage(otherWnd, WM_GOODBYE, 0, 0); |
| 175 |
|
| 176 |
otherWnd = NULL; |
| 177 |
} |
| 178 |
|
| 179 |
/* Close the shared buffer */ |
| 180 |
if (hMap) |
| 181 |
{ |
| 182 |
UnmapViewOfFile(vSendBuf); |
| 183 |
vSendBuf = NULL; |
| 184 |
|
| 185 |
UnmapViewOfFile(vRecvBuf); |
| 186 |
vRecvBuf = NULL; |
| 187 |
|
| 188 |
CloseHandle(hMap); |
| 189 |
} |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |
| 193 |
|
| 194 |
static int vti_reset(CableHandle *h) |
| 195 |
{ |
| 196 |
if (!hMap) |
| 197 |
{ |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
if (vSendBuf) |
| 202 |
{ |
| 203 |
vSendBuf->start = vSendBuf->end = 0; |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
ticables_critical("vti_reset(): send buffer busted !\n"); |
| 208 |
} |
| 209 |
if (vRecvBuf) |
| 210 |
{ |
| 211 |
vRecvBuf->start = vRecvBuf->end = 0; |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
ticables_critical("vti_reset(): receive buffer busted !\n"); |
| 216 |
} |
| 217 |
|
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
static int vti_probe(CableHandle *h) |
| 222 |
{ |
| 223 |
/* Get an handle on the VTi window */ |
| 224 |
otherWnd = FindWindow("TEmuWnd", NULL); |
| 225 |
if (!otherWnd) |
| 226 |
{ |
| 227 |
return ERR_VTI_FINDWINDOW; |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
otherWnd = NULL; |
| 232 |
} |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static int vti_put(CableHandle *h, uint8_t *data, uint32_t len) |
| 238 |
{ |
| 239 |
unsigned int i; |
| 240 |
tiTIME clk; |
| 241 |
|
| 242 |
if (!hMap) |
| 243 |
{ |
| 244 |
return 0; |
| 245 |
} |
| 246 |
if (vSendBuf) |
| 247 |
{ |
| 248 |
for(i = 0; i < len; i++) |
| 249 |
{ |
| 250 |
TO_START(clk); |
| 251 |
do |
| 252 |
{ |
| 253 |
if (TO_ELAPSED(clk, h->timeout)) |
| 254 |
{ |
| 255 |
return ERR_WRITE_TIMEOUT; |
| 256 |
} |
| 257 |
} |
| 258 |
while (((vSendBuf->end + 1) & (BUFSIZE-1)) == vSendBuf->start); |
| 259 |
|
| 260 |
vSendBuf->buf[vSendBuf->end] = data[i]; |
| 261 |
vSendBuf->end = (vSendBuf->end + 1) & (BUFSIZE-1); |
| 262 |
} |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
ticables_critical("vti_put(): send buffer busted !\n"); |
| 267 |
} |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
static int vti_get(CableHandle *h, uint8_t *data, uint32_t len) |
| 273 |
{ |
| 274 |
unsigned int i; |
| 275 |
tiTIME clk; |
| 276 |
|
| 277 |
if (!hMap) |
| 278 |
{ |
| 279 |
return 0; |
| 280 |
} |
| 281 |
if (vRecvBuf) |
| 282 |
{ |
| 283 |
/* Wait that the buffer has been filled */ |
| 284 |
for(i = 0; i < len; i++) |
| 285 |
{ |
| 286 |
TO_START(clk); |
| 287 |
do |
| 288 |
{ |
| 289 |
if (TO_ELAPSED(clk, h->timeout)) |
| 290 |
{ |
| 291 |
return ERR_READ_TIMEOUT; |
| 292 |
} |
| 293 |
} |
| 294 |
while (vRecvBuf->start == vRecvBuf->end); |
| 295 |
|
| 296 |
/* And retrieve the data from the circular buffer */ |
| 297 |
data[i] = vRecvBuf->buf[vRecvBuf->start]; |
| 298 |
vRecvBuf->start = (vRecvBuf->start + 1) & (BUFSIZE-1); |
| 299 |
} |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
ticables_critical("vti_get(): receive buffer busted !\n"); |
| 304 |
} |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
static int vti_check(CableHandle *h, int *status) |
| 310 |
{ |
| 311 |
if (!hMap) |
| 312 |
{ |
| 313 |
return 0; |
| 314 |
} |
| 315 |
if (vRecvBuf) |
| 316 |
{ |
| 317 |
*status = !(vRecvBuf->start == vRecvBuf->end); |
| 318 |
} |
| 319 |
else |
| 320 |
{ |
| 321 |
ticables_critical("vti_check(): receive buffer busted !\n"); |
| 322 |
} |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
extern const CableFncts cable_vti = |
| 328 |
{ |
| 329 |
CABLE_VTI, |
| 330 |
"VTI", |
| 331 |
N_("Virtual TI"), |
| 332 |
N_("Virtual link for VTi"), |
| 333 |
0, |
| 334 |
&vti_prepare, |
| 335 |
&vti_open, &vti_close, &vti_reset, &vti_probe, NULL, |
| 336 |
&vti_put, &vti_get, &vti_check, |
| 337 |
&noop_set_red_wire, &noop_set_white_wire, |
| 338 |
&noop_get_red_wire, &noop_get_white_wire, |
| 339 |
NULL, NULL, |
| 340 |
&noop_set_device, |
| 341 |
NULL |
| 342 |
}; |