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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <stdio.h>
struct RIGPORTINFO
{
char IOBASE[80];
char PTTIOBASE[80]; // Port for Hardware PTT - may be same as control port.
int SPEED;
HANDLE hDevice; // COM device Handle
int ReopenDelay;
SOCKET remoteSock; // Socket for use with WINMORCONROL
struct sockaddr remoteDest; // Dest for above
UCHAR TXBuffer[500]; // Last message sent - saved for Retry
int TXLen; // Len of last sent
int CONNECTED; // for HAMLIB
int CONNECTING;
int Alerted;
};
struct RIGPORTINFO PORTS[4];
char PTTCATPort[4][64] = {"", "", "", ""};
HANDLE PTTCATHandles[4] = {0, 0, 0, 0};
int RealMux[4] = {0, 0, 0, 0}; // BPQ Virtual or Real
VOID PTTCATThread();
VOID ConnecttoHAMLIB(struct RIGPORTINFO * PORT);
char * strlop(char * buf, char delim)
{
// Terminate buf at delim, and return rest of string
char * ptr;
if (buf == NULL) return NULL; // Protect
ptr = strchr(buf, delim);
if (ptr == NULL) return NULL;
*(ptr)++=0;
return ptr;
}
void SendHamLib(struct RIGPORTINFO * PORT, int PTTState)
{
char cmd[32];
int Len = sprintf(cmd, "T %d\n", PTTState);
Len = send(PORT->remoteSock, cmd, Len, 0);
Len = GetLastError();
}
int DecodeHAMLIBAddr(struct RIGPORTINFO * PORT, char * ptr)
{
// Param is IPADDR:PORT. Only Allow numeric addresses
struct sockaddr_in * destaddr = (SOCKADDR_IN *)&PORT->remoteDest;
char * port;
strcpy(PORT->IOBASE, ptr);
port = strlop(ptr, ':');
if (port == NULL)
return 0;
destaddr->sin_family = AF_INET;
destaddr->sin_addr.s_addr = inet_addr(ptr);
destaddr->sin_port = htons(atoi(port));
return 1;
}
void HAMLIBProcessMessage(struct RIGPORTINFO * PORT)
{
// Called from Background thread. I think we just need to read and discard
char RXBuffer[512];
int InputLen = recv(PORT->remoteSock, RXBuffer, 500, 0);
if (InputLen == 0 || InputLen == SOCKET_ERROR)
{
if (PORT->remoteSock)
closesocket(PORT->remoteSock);
PORT->remoteSock = 0;
PORT->CONNECTED = FALSE;
PORT->hDevice = 0;
return;
}
}
int main(int argc, char ** argv)
{
struct RIGPORTINFO * PORT;
int n = 0;
WSADATA WsaData; // receives data from WSAStartup
if (argc < 3)
{
printf ("Missing paramters - you need COM port and IP Address and rigctl port of BPQ, eg \r\n"
" WinRPRHelper com10 192.168.1.64:4532\r\n\r\n"
"Press any key to exit\r\n");
getchar();
exit (0);
}
WSAStartup(MAKEWORD(2, 0), &WsaData);
while (argc > 2)
{
PORT = &PORTS[n];
strcpy(PTTCATPort[n], argv[n + n +1]);
DecodeHAMLIBAddr(PORT, argv[n + n + 2]);
n++;
argc -= 2;
}
_beginthread(PTTCATThread, 0, 0);
for (n = 0; n < 4; n++)
{
if (PTTCATPort[n][0]) // Serial port RTS to HAMLIB PTT
{
PORT = &PORTS[n];
ConnecttoHAMLIB(PORT);
}
}
Sleep(2000);
printf("WinRPRHelper running - Press ctrl/c to exit\r\n");
fflush(stdout);
while(1)
{
Sleep(1000);
for (n = 0; n < 4; n++)
{
if (PTTCATPort[n][0]) // Serial port RTS to HAMLIB PTT
{
PORT = &PORTS[n];
if (PORT->hDevice == 0)
{
// Try to reopen every 15 secs
PORT->ReopenDelay++;
if (PORT->ReopenDelay > 15)
{
PORT->ReopenDelay = 0;
ConnecttoHAMLIB(PORT);
}
}
}
}
}
}
VOID PTTCATThread()
{
DWORD dwLength = 0;
int Length, ret, i;
UCHAR * ptr1;
UCHAR * ptr2;
UCHAR c;
UCHAR Block[4][80];
UCHAR CurrentState[4] = {0};
#define RTS 2
#define DTR 4
HANDLE Event;
HANDLE Handle[4];
DWORD EvtMask[4];
OVERLAPPED Overlapped[4];
char Port[32];
int PIndex = 0;
int HIndex = 0;
int rc;
while (PIndex < 4 && PTTCATPort[PIndex][0])
{
RealMux[HIndex] = 0;
sprintf(Port, "\\\\.\\pipe\\BPQ%s", PTTCATPort[PIndex]);
Handle[HIndex] = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (Handle[HIndex] == (HANDLE) -1)
{
int Err = GetLastError();
printf("PTTMUX port BPQ%s Open failed code %d - trying real com port\r\n", PTTCATPort[PIndex], Err);
// See if real com port
sprintf(Port, "\\\\.\\\\%s", PTTCATPort[PIndex]);
Handle[HIndex] = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
RealMux[HIndex] = 1;
if (Handle[HIndex] == (HANDLE) -1)
{
int Err = GetLastError();
printf("PTTMUX port %s Open failed code %d\r\n", PTTCATPort[PIndex], Err);
}
else
{
rc = SetCommMask(Handle[HIndex], EV_CTS | EV_DSR); // Request notifications
HIndex++;
printf("PTTMUX port %s Opened\r\n", PTTCATPort[PIndex]);
}
}
else
HIndex++;
PIndex++;
}
if (PIndex == 0)
return; // No ports
Event = CreateEvent(NULL, TRUE, FALSE, NULL);
for (i = 0; i < HIndex; i ++)
{
memset(&Overlapped[i], 0, sizeof(OVERLAPPED));
Overlapped[i].hEvent = Event;
if (RealMux[i])
{
// Request Interface change notifications
rc = WaitCommEvent(Handle[i], &EvtMask[i], &Overlapped[i]);
rc = GetLastError();
}
else
{
// Prime a read on each handle
ReadFile(Handle[i], Block[i], 80, &Length, &Overlapped[i]);
}
}
while (1)
{
WaitAgain:
ret = WaitForSingleObject(Event, 1000);
if (ret == WAIT_TIMEOUT)
goto WaitAgain;
ResetEvent(Event);
// See which request(s) have completed
for (i = 0; i < HIndex; i ++)
{
ret = GetOverlappedResult(Handle[i], &Overlapped[i], &Length, FALSE);
if (ret)
{
if (RealMux[i])
{
// Request Interface change notifications
DWORD Mask;
GetCommModemStatus(Handle[i], &Mask);
if (Mask & MS_CTS_ON)
SendHamLib(&PORTS[i], TRUE);
else
SendHamLib(&PORTS[i], FALSE);
memset(&Overlapped[i], 0, sizeof(OVERLAPPED));
Overlapped[i].hEvent = Event;
WaitCommEvent(Handle[i], &EvtMask[i], &Overlapped[i]);
}
else
{
ptr1 = Block[i];
ptr2 = Block[i];
while (Length > 0)
{
c = *(ptr1++);
Length--;
if (c == 0xff)
{
c = *(ptr1++);
Length--;
if (c == 0xff) // ff ff means ff
{
Length--;
}
else
{
// This is connection / RTS/DTR statua from other end
// Convert to CAT Command
if (c == CurrentState[i])
continue;
if (c & RTS)
SendHamLib(&PORTS[i], TRUE);
else
SendHamLib(&PORTS[i], FALSE);
CurrentState[i] = c;
continue;
}
}
}
memset(&Overlapped[i], 0, sizeof(OVERLAPPED));
Overlapped[i].hEvent = Event;
ReadFile(Handle[i], Block[i], 80, &Length, &Overlapped[i]);
}
}
}
}
}
VOID HAMLIBThread(struct RIGPORTINFO * PORT);
VOID ConnecttoHAMLIB(struct RIGPORTINFO * PORT)
{
_beginthread(HAMLIBThread, 0, (void *)PORT);
return ;
}
VOID HAMLIBThread(struct RIGPORTINFO * PORT)
{
// Opens sockets and looks for data
char Msg[255];
int err, i, ret;
u_long param=1;
BOOL bcopt=TRUE;
fd_set readfs;
fd_set errorfs;
struct timeval timeout;
if (PORT->remoteSock)
{
closesocket(PORT->remoteSock);
}
PORT->remoteSock = 0;
PORT->remoteSock = socket(AF_INET,SOCK_STREAM,0);
if (PORT->remoteSock == INVALID_SOCKET)
{
i=sprintf(Msg, "Socket Failed for HAMLIB socket - error code = %d\r\n", WSAGetLastError());
printf(Msg);
PORT->CONNECTING = FALSE;
return;
}
setsockopt(PORT->remoteSock, SOL_SOCKET, SO_REUSEADDR, (const char FAR *)&bcopt, 4);
setsockopt(PORT->remoteSock, IPPROTO_TCP, TCP_NODELAY, (const char FAR *)&bcopt, 4);
if (connect(PORT->remoteSock,(LPSOCKADDR) &PORT->remoteDest,sizeof(PORT->remoteDest)) == 0)
{
//
// Connected successful
//
ioctlsocket(PORT->remoteSock, FIONBIO, ¶m);
printf("Connected to HAMLIB socket Addr %s\r\n", PORT->IOBASE);
}
else
{
if (PORT->Alerted == FALSE)
{
struct sockaddr_in * destaddr = (SOCKADDR_IN * )&PORT->remoteDest;
err = WSAGetLastError();
sprintf(Msg, "Connect Failed for HAMLIB socket - error code = %d Addr %s\r\n", err, PORT->IOBASE);
printf(Msg);
PORT->Alerted = TRUE;
}
closesocket(PORT->remoteSock);
PORT->remoteSock = 0;
PORT->CONNECTING = FALSE;
return;
}
PORT->CONNECTED = TRUE;
PORT->hDevice = (HANDLE)1; // simplifies check code
PORT->Alerted = TRUE;
while (PORT->CONNECTED)
{
FD_ZERO(&readfs);
FD_ZERO(&errorfs);
FD_SET(PORT->remoteSock,&readfs);
FD_SET(PORT->remoteSock,&errorfs);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
ret = select((int)PORT->remoteSock + 1, &readfs, NULL, &errorfs, &timeout);
if (ret == SOCKET_ERROR)
{
printf("HAMLIB Select failed %d r\n", WSAGetLastError());
goto Lost;
}
if (ret > 0)
{
// See what happened
if (FD_ISSET(PORT->remoteSock, &readfs))
{
HAMLIBProcessMessage(PORT);
}
if (FD_ISSET(PORT->remoteSock, &errorfs))
{
Lost:
sprintf(Msg, "HAMLIB Connection lost for Addr %s\r\n", PORT->IOBASE);
printf(Msg);
PORT->CONNECTED = FALSE;
PORT->Alerted = FALSE;
PORT->hDevice = 0; // simplifies check code
closesocket(PORT->remoteSock);
PORT->remoteSock = 0;
return;
}
continue;
}
else
{
}
}
sprintf(Msg, "HAMLIB Thread Terminated Addr %s\r\n", PORT->IOBASE);
printf(Msg);
}
|