root/wconsd.c @ 695889f3
62045e98 | Hamish Coleman | /*
|
|
* wconsd.c - serial port server service for Windows NT
|
|||
*
|
|||
* Copyright (c) 2003 Benjamin Schweizer <gopher at h07 dot org>
|
|||
* 1998 Stephen Early <Stephen.Early@cl.cam.ac.uk>
|
|||
*
|
|||
*
|
|||
* This program is free software; you can redistribute it and/or modify
|
|||
* it under the terms of the GNU General Public License as published by
|
|||
* the Free Software Foundation; either version 2 of the License, or
|
|||
* (at your option) any later version.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful,
|
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
* GNU General Public License for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU General Public License
|
|||
* along with this program; if not, write to the Free Software
|
|||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|||
*/
|
|||
78d4c0b0 | Hamish Coleman | ||
b6970c98 | Hamish Coleman | /* Note: winsock2.h MUST be included before windows.h */
|
|
#include <winsock2.h>
|
|||
#include <windows.h>
|
|||
#include <winsvc.h>
|
|||
#include <stdio.h>
|
|||
#include <stdlib.h>
|
|||
84d7e6f5 | Hamish Coleman | #define VERSION "0.1.2"
|
|
62045e98 | Hamish Coleman | ||
84d7e6f5 | Hamish Coleman | #define HEADER "wconsd " VERSION " a serial port server\r\n\r\n"
|
|
61b53eda | Hamish Coleman | #define HELP "available commands:\r\n\n port, speed, data, parity, stop\r\n help, status, copyright\r\n open, close, autoclose\r\n"
|
|
78d4c0b0 | Hamish Coleman | ||
/* Size of buffers for send and receive */
|
|||
#define BUFSIZE 1024
|
|||
62045e98 | Hamish Coleman | #define MAXLEN 1024
|
|
78d4c0b0 | Hamish Coleman | ||
/* Sockets for listening and communicating */
|
|||
SOCKET ls=INVALID_SOCKET,cs=INVALID_SOCKET;
|
|||
/* Event objects */
|
|||
HANDLE stopEvent, connectionCloseEvent, threadTermEvent;
|
|||
HANDLE readEvent, writeEvent;
|
|||
WSAEVENT listenSocketEvent;
|
|||
/* COM port */
|
|||
HANDLE hCom;
|
|||
DCB dcb;
|
|||
62045e98 | Hamish Coleman | COMMTIMEOUTS timeouts;
|
|
61b53eda | Hamish Coleman | ||
/* Port default settings are here */
|
|||
int com_port=1;
|
|||
DWORD com_speed=9600;
|
|||
BYTE com_data=8;
|
|||
BYTE com_parity=NOPARITY;
|
|||
BYTE com_stop=ONESTOPBIT;
|
|||
BOOL com_autoclose=TRUE;
|
|||
ff03d660 | Hamish Coleman | BOOL com_state=FALSE; // FALSE=closed,TRUE=open
|
|
78d4c0b0 | Hamish Coleman | ||
61b53eda | Hamish Coleman | #define TCPPORT 9600
|
|
78d4c0b0 | Hamish Coleman | /* Service status: our current status, and handle on service manager */
|
|
SERVICE_STATUS wconsd_status;
|
|||
SERVICE_STATUS_HANDLE wconsd_statusHandle;
|
|||
25fa1d20 | Hamish Coleman | int debug_mode = 0;
|
|
61b53eda | Hamish Coleman | /*
|
|
e6a4efc5 | Hamish Coleman | * output from OutputDebugStringA can be seen using sysinternals debugview
|
|
61b53eda | Hamish Coleman | * http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
|
|
*/
|
|||
e6a4efc5 | Hamish Coleman | ||
/*
|
|||
* log a debug message
|
|||
*/
|
|||
int dprintf_level = 1;
|
|||
int dprintf(unsigned char severity, const char *fmt, ...) {
|
|||
va_list args;
|
|||
char buf[1025];
|
|||
int i;
|
|||
if (severity > dprintf_level)
|
|||
return 0;
|
|||
va_start(args,fmt);
|
|||
i=vsprintf(buf,fmt,args);
|
|||
va_end(args);
|
|||
25fa1d20 | Hamish Coleman | if (debug_mode) {
|
|
printf("%s",buf);
|
|||
} else {
|
|||
OutputDebugStringA(buf);
|
|||
}
|
|||
e6a4efc5 | Hamish Coleman | ||
return i;
|
|||
78d4c0b0 | Hamish Coleman | }
|
|
62045e98 | Hamish Coleman | /* open the com port */
|
|
DWORD open_com_port(DWORD *specificError) {
|
|||
/* Open the COM port */
|
|||
char portstr[12];
|
|||
sprintf(portstr, "\\\\.\\COM%d", com_port);
|
|||
hCom = CreateFile(portstr,
|
|||
GENERIC_READ | GENERIC_WRITE,
|
|||
0, // Exclusive access
|
|||
NULL,
|
|||
OPEN_EXISTING,
|
|||
FILE_FLAG_OVERLAPPED,
|
|||
NULL);
|
|||
if (hCom == INVALID_HANDLE_VALUE) {
|
|||
*specificError=GetLastError();
|
|||
return 13;
|
|||
}
|
|||
if (!GetCommState(hCom, &dcb)) {
|
|||
*specificError=GetLastError();
|
|||
return 14;
|
|||
}
|
|||
// Fill in the device control block
|
|||
dcb.BaudRate=com_speed;
|
|||
dcb.ByteSize=com_data;
|
|||
dcb.Parity=com_parity; // NOPARITY, ODDPARITY, EVENPARITY
|
|||
dcb.StopBits=com_stop; // ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS
|
|||
dcb.fBinary=TRUE;
|
|||
dcb.fOutxCtsFlow=FALSE;
|
|||
dcb.fOutxDsrFlow=FALSE;
|
|||
dcb.fDtrControl=DTR_CONTROL_ENABLE; // Always on
|
|||
dcb.fDsrSensitivity=FALSE;
|
|||
dcb.fTXContinueOnXoff=FALSE;
|
|||
dcb.fOutX=FALSE;
|
|||
dcb.fInX=FALSE;
|
|||
dcb.fErrorChar=FALSE;
|
|||
dcb.fNull=FALSE;
|
|||
dcb.fRtsControl=RTS_CONTROL_ENABLE; // Always on
|
|||
dcb.fAbortOnError=FALSE;
|
|||
if (!SetCommState(hCom, &dcb)) {
|
|||
*specificError=GetLastError();
|
|||
return 15;
|
|||
}
|
|||
timeouts.ReadIntervalTimeout=20;
|
|||
timeouts.ReadTotalTimeoutMultiplier=0;
|
|||
timeouts.ReadTotalTimeoutConstant=50;
|
|||
timeouts.WriteTotalTimeoutMultiplier=0;
|
|||
timeouts.WriteTotalTimeoutConstant=0;
|
|||
if (!SetCommTimeouts(hCom, &timeouts)) {
|
|||
*specificError=GetLastError();
|
|||
return 16;
|
|||
}
|
|||
com_state=TRUE;
|
|||
return 0;
|
|||
}
|
|||
/* close the com port */
|
|||
void close_com_port() {
|
|||
CloseHandle(hCom);
|
|||
com_state=FALSE;
|
|||
}
|
|||
78d4c0b0 | Hamish Coleman | /* Initialise wconsd: open a listening socket and the COM port, and
|
|
* create lots of event objects. */
|
|||
DWORD wconsd_init(DWORD argc, LPSTR *argv, DWORD *specificError)
|
|||
{
|
|||
struct sockaddr_in sin;
|
|||
WORD wVersionRequested;
|
|||
WSADATA wsaData;
|
|||
int err;
|
|||
/* Start up sockets */
|
|||
wVersionRequested = MAKEWORD( 2, 2 );
|
|||
err = WSAStartup( wVersionRequested, &wsaData );
|
|||
if ( err != 0 ) {
|
|||
*specificError=err;
|
|||
/* Tell the user that we could not find a usable */
|
|||
/* WinSock DLL. */
|
|||
return 1;
|
|||
}
|
|||
/* Confirm that the WinSock DLL supports 2.2.*/
|
|||
/* Note that if the DLL supports versions greater */
|
|||
/* than 2.2 in addition to 2.2, it will still return */
|
|||
/* 2.2 in wVersion since that is the version we */
|
|||
/* requested. */
|
|||
if ( LOBYTE( wsaData.wVersion ) != 2 ||
|
|||
HIBYTE( wsaData.wVersion ) != 2 ) {
|
|||
/* Tell the user that we could not find a usable */
|
|||
/* WinSock DLL. */
|
|||
WSACleanup( );
|
|||
return 2;
|
|||
}
|
|||
/* The WinSock DLL is acceptable. Proceed. */
|
|||
// Create the event object used to signal service shutdown
|
|||
stopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
|||
if (stopEvent==NULL) {
|
|||
*specificError=GetLastError();
|
|||
return 3;
|
|||
}
|
|||
// Create the event object used to signal connection close
|
|||
connectionCloseEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
|||
if (connectionCloseEvent==NULL) {
|
|||
*specificError=GetLastError();
|
|||
return 4;
|
|||
}
|
|||
threadTermEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
|||
if (threadTermEvent==NULL) {
|
|||
*specificError=GetLastError();
|
|||
return 5;
|
|||
}
|
|||
// Event objects for overlapped IO
|
|||
readEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
|||
if (readEvent==NULL) {
|
|||
*specificError=GetLastError();
|
|||
return 6;
|
|||
}
|
|||
writeEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
|||
if (writeEvent==NULL) {
|
|||
*specificError=GetLastError();
|
|||
return 7;
|
|||
}
|
|||
// Create the event object for socket operations
|
|||
listenSocketEvent = WSACreateEvent();
|
|||
if (listenSocketEvent==WSA_INVALID_EVENT) {
|
|||
*specificError=WSAGetLastError();
|
|||
return 8;
|
|||
}
|
|||
/* Create a socket to listen for connections. */
|
|||
memset(&sin,0,sizeof(sin));
|
|||
sin.sin_family=AF_INET;
|
|||
61b53eda | Hamish Coleman | sin.sin_port=htons(TCPPORT);
|
|
78d4c0b0 | Hamish Coleman | ls=socket(AF_INET,SOCK_STREAM,0);
|
|
if (ls==INVALID_SOCKET) {
|
|||
*specificError=WSAGetLastError();
|
|||
return 9;
|
|||
}
|
|||
if (bind(ls,(struct sockaddr *)&sin,sizeof(sin))==SOCKET_ERROR) {
|
|||
*specificError=WSAGetLastError();
|
|||
return 10;
|
|||
}
|
|||
if (listen(ls,1)==SOCKET_ERROR) {
|
|||
*specificError=WSAGetLastError();
|
|||
return 11;
|
|||
}
|
|||
/* Mark the socket as non-blocking */
|
|||
if (WSAEventSelect(ls,listenSocketEvent,FD_ACCEPT)==SOCKET_ERROR) {
|
|||
*specificError=WSAGetLastError();
|
|||
return 12;
|
|||
}
|
|||
return 0;
|
|||
}
|
|||
DWORD WINAPI wconsd_net_to_com(LPVOID lpParam)
|
|||
{
|
|||
BYTE buf[BUFSIZE];
|
|||
DWORD size,wsize;
|
|||
unsigned long zero=0;
|
|||
fd_set s;
|
|||
OVERLAPPED o={0};
|
|||
o.hEvent = writeEvent;
|
|||
while (WaitForSingleObject(threadTermEvent,0)!=WAIT_OBJECT_0) {
|
|||
/* There's a bug in some versions of Windows which leads
|
|||
* to recv() returning -1 and indicating error WSAEWOULDBLOCK,
|
|||
* even on a blocking socket. This select() is here to work
|
|||
* around that bug. */
|
|||
FD_SET(cs,&s);
|
|||
select(0,&s,NULL,NULL,NULL);
|
|||
size=recv(cs,buf,BUFSIZE,0);
|
|||
if (size==0) {
|
|||
SetEvent(connectionCloseEvent);
|
|||
return 0;
|
|||
}
|
|||
if (size==SOCKET_ERROR) {
|
|||
/* General paranoia about blocking sockets */
|
|||
ioctlsocket(cs,FIONBIO,&zero);
|
|||
}
|
|||
if (size!=SOCKET_ERROR) {
|
|||
if (!WriteFile(hCom,buf,size,&wsize,&o)) {
|
|||
if (GetLastError()==ERROR_IO_PENDING) {
|
|||
// Wait for it...
|
|||
if (!GetOverlappedResult(hCom,&o,&wsize,TRUE)) {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: Error %d (overlapped) writing to COM port\n",GetLastError());
|
|
78d4c0b0 | Hamish Coleman | }
|
|
} else {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: Error %d writing to COM port\n",GetLastError());
|
|
78d4c0b0 | Hamish Coleman | }
|
|
}
|
|||
if (wsize!=size) {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: Eeek! WriteFile: wrote %d of %d\n",wsize,size);
|
|
78d4c0b0 | Hamish Coleman | }
|
|
}
|
|||
}
|
|||
return 0;
|
|||
}
|
|||
DWORD WINAPI wconsd_com_to_net(LPVOID lpParam)
|
|||
{
|
|||
BYTE buf[BUFSIZE];
|
|||
DWORD size;
|
|||
OVERLAPPED o={0};
|
|||
o.hEvent=readEvent;
|
|||
while (WaitForSingleObject(threadTermEvent,0)!=WAIT_OBJECT_0) {
|
|||
if (!ReadFile(hCom,buf,BUFSIZE,&size,&o)) {
|
|||
if (GetLastError()==ERROR_IO_PENDING) {
|
|||
// Wait for overlapped operation to complete
|
|||
if (!GetOverlappedResult(hCom,&o,&size,TRUE)) {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: Error %d (overlapped) reading from COM port\n",GetLastError());
|
|
78d4c0b0 | Hamish Coleman | }
|
|
} else {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: Error %d reading from COM port\n",GetLastError());
|
|
78d4c0b0 | Hamish Coleman | SetEvent(connectionCloseEvent);
|
|
return 0;
|
|||
}
|
|||
}
|
|||
if (size>0) {
|
|||
send(cs,buf,size,0);
|
|||
}
|
|||
}
|
|||
return 0;
|
|||
62045e98 | Hamish Coleman | }
|
|
int run_menu() {
|
|||
/* no comment */
|
|||
BYTE buf[BUFSIZE], msg[MAXLEN], line[MAXLEN], command[MAXLEN], parameter[MAXLEN];
|
|||
DWORD errcode;
|
|||
DWORD size, linelen=0;
|
|||
char *buf2split=0, *cmdsplit=0;
|
|||
BOOL menu=TRUE;
|
|||
WORD i;
|
|||
d4040099 | Hamish Coleman | char *linep;
|
|
62045e98 | Hamish Coleman | ||
unsigned long zero=0;
|
|||
send(cs,HEADER,strlen(HEADER),0);
|
|||
send(cs,HELP,strlen(HELP),0);
|
|||
send(cs,"> ",2,0);
|
|||
while (menu) {
|
|||
size=recv(cs,buf,BUFSIZE,0);
|
|||
if (size==0) {
|
|||
SetEvent(connectionCloseEvent);
|
|||
return 1;
|
|||
}
|
|||
if (size==SOCKET_ERROR) {
|
|||
/* General paranoia about blocking sockets */
|
|||
ioctlsocket(cs,FIONBIO,&zero);
|
|||
}
|
|||
if (size!=SOCKET_ERROR) {
|
|||
for (i = 0; i < size; i++) {
|
|||
if (buf[i] == 127) { // backspace
|
|||
if (linelen > 0) {
|
|||
send(cs,"\x7f",1,0);
|
|||
linelen--;
|
|||
} else {
|
|||
send(cs,"\x07",1,0); // bell
|
|||
}
|
|||
} else if (buf[i] == 13) { // cr
|
|||
send(cs,HEADER,strlen(HEADER),0);
|
|||
line[linelen]=32; // ensure that there is
|
|||
line[linelen+1]=0; // at least one seperator
|
|||
d4040099 | Hamish Coleman | linep=line;
|
|
memcpy(command,line, strchr(linep, 32) - linep); // seperator
|
|||
memcpy(parameter,strchr(line, 32)+1, linep+linelen-strchr(line, 32)); // seperator
|
|||
command[strchr(line, 32) - linep]=0;
|
|||
if (linep+linelen-strchr(line, 32)-1 < 0) {
|
|||
62045e98 | Hamish Coleman | parameter[0]=0;
|
|
} else {
|
|||
d4040099 | Hamish Coleman | parameter[linep+linelen-strchr(line, 32)-1]=0;
|
|
62045e98 | Hamish Coleman | }
|
|
if (!strcmp(command, "help") || !strcmp(command, "?")) { // help
|
|||
send(cs,HELP,strlen(HELP),0);
|
|||
} else if (!strcmp(command, "status")) { // status
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "copyright")) { // copyright
|
|||
sprintf(msg, " Copyright (c) 2003 by Benjamin Schweizer <gopher at h07 dot org>\r\n 1998 by Stephen Early <Stephen.Early@cl.cam.ac.uk>\r\n\r\n\r\n This program is free software; you can redistribute it and/or modify\r\n it under the terms of the GNU General Public License as published by\r\n the Free Software Foundation; either version 2 of the License, or\r\n (at your option) any later version.\r\n \r\n This program is distributed in the hope that it will be useful,\r\n but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r\n GNU General Public License for more details.\r\n \r\n You should have received a copy of the GNU General Public License\r\n along with this program; if not, write to the Free Software\r\n Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r\n\n");
|
|||
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "port")) { // port
|
|||
if (atoi(parameter) >= 1 && atoi(parameter) <= 16) {
|
|||
com_port=atoi(parameter);
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "speed")) { // speed
|
|||
if (atoi(parameter) > 0) {
|
|||
com_speed=atoi(parameter);
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "data")) { // data
|
|||
if (!strcmp(parameter, "5")) {
|
|||
com_data=5;
|
|||
} else if (!strcmp(parameter, "6")) {
|
|||
com_data=6;
|
|||
} else if (!strcmp(parameter, "7")) {
|
|||
com_data=7;
|
|||
} else if (!strcmp(parameter, "8")) {
|
|||
com_data=8;
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "parity")) { // parity
|
|||
if (!strcmp(parameter, "no") || !strcmp(parameter, "0")) {
|
|||
com_parity=NOPARITY;
|
|||
} else if (!strcmp(parameter, "even") || !strcmp(parameter, "2")) {
|
|||
com_parity=EVENPARITY;
|
|||
} else if (!strcmp(parameter, "odd") || !strcmp(parameter, "1")) {
|
|||
com_parity=ODDPARITY;
|
|||
} else if (!strcmp(parameter, "mark")) {
|
|||
com_parity=MARKPARITY;
|
|||
} else if (!strcmp(parameter, "space")) {
|
|||
com_parity=SPACEPARITY;
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "stop")) {
|
|||
if (!strcmp(parameter, "one") || !strcmp(parameter, "1")) {
|
|||
com_stop=ONESTOPBIT;
|
|||
} else if (!strcmp(parameter, "one5") || !strcmp(parameter, "1.5")) {
|
|||
com_stop=ONE5STOPBITS;
|
|||
} else if (!strcmp(parameter, "two") || !strcmp(parameter, "2")) {
|
|||
com_stop=TWOSTOPBITS;
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else if (!strcmp(command, "open")) { // open
|
|||
if (atoi(parameter) > 0) { // optional port parameter
|
|||
com_port=atoi(parameter);
|
|||
}
|
|||
if (!com_state) {
|
|||
if (!open_com_port(&errcode)) {
|
|||
send(cs,"\r\n\f",3,0);
|
|||
menu=FALSE;
|
|||
return 0;
|
|||
} else {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "error:\r\n can't open port.\r\n\n");
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
}
|
|||
} else { // port ist still open
|
|||
send(cs,"\r\n\f",3,0);
|
|||
menu=FALSE;
|
|||
return 0;
|
|||
}
|
|||
} else if (!strcmp(command, "close")) { // close
|
|||
close_com_port();
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "info:\r\n actual com port closed.\r\n\n");
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
} else if (!strcmp(command, "autoclose")) { // autoclose
|
|||
if (!strcmp(parameter, "true") || !strcmp(parameter, "1") || !strcmp(parameter, "yes")) {
|
|||
com_autoclose=TRUE;
|
|||
} else if (!strcmp(parameter, "false") || !strcmp(parameter, "0") || !strcmp(parameter, "no")) {
|
|||
com_autoclose=FALSE;
|
|||
}
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "status:\r\n port=%d speed=%d data=%d parity=%d stop=%d\r\n\n", com_port, com_speed, com_data, com_parity, com_stop);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
if(com_state) {
|
|||
12cf305b | Hamish Coleman | sprintf(msg, " state=open autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | } else {
|
|
12cf305b | Hamish Coleman | sprintf(msg, " state=closed autoclose=%d\r\n\n", com_autoclose);
|
|
62045e98 | Hamish Coleman | }
|
|
send(cs,msg,strlen(msg),0);
|
|||
} else { // else
|
|||
12cf305b | Hamish Coleman | sprintf(msg, "debug:\r\n command: '%s' parameter: '%s'\r\n\n", command, parameter);
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
12cf305b | Hamish Coleman | sprintf(msg, "\r\n\n");
|
|
62045e98 | Hamish Coleman | send(cs,msg,strlen(msg),0);
|
|
}
|
|||
send(cs,"> ",2,0);
|
|||
linelen=0;
|
|||
} else { // other chars
|
|||
if (linelen < MAXLEN - 1) {
|
|||
line[linelen] = buf[i];
|
|||
linelen++;
|
|||
send(cs,buf+i,1,0);
|
|||
} else {
|
|||
send(cs,"\x07",1,0); // bell
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
}
|
|||
return 0;
|
|||
78d4c0b0 | Hamish Coleman | }
|
|
static void wconsd_main(void)
|
|||
{
|
|||
HANDLE wait_array[3];
|
|||
BOOL run=TRUE;
|
|||
DWORD o;
|
|||
SOCKET as;
|
|||
HANDLE netThread=NULL, comThread=NULL;
|
|||
long zero;
|
|||
/* Main loop: wait for a connection, service it, repeat
|
|||
* until signalled that the service is terminating */
|
|||
wait_array[0]=stopEvent;
|
|||
wait_array[1]=listenSocketEvent;
|
|||
wait_array[2]=connectionCloseEvent;
|
|||
while (run) {
|
|||
o=WaitForMultipleObjects(3,wait_array,FALSE,INFINITE);
|
|||
switch (o-WAIT_OBJECT_0) {
|
|||
case 0:
|
|||
run=FALSE;
|
|||
ResetEvent(stopEvent);
|
|||
break;
|
|||
case 1: /* There is an incoming connection */
|
|||
WSAResetEvent(listenSocketEvent);
|
|||
as=accept(ls,NULL,NULL);
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: accepted new connection\n");
|
|
78d4c0b0 | Hamish Coleman | if (as!=INVALID_SOCKET) {
|
|
if (cs!=INVALID_SOCKET) {
|
|||
/* Close down the existing connection and let the new one through */
|
|||
SetEvent(threadTermEvent);
|
|||
shutdown(cs,SD_BOTH);
|
|||
WaitForSingleObject(netThread,INFINITE);
|
|||
WaitForSingleObject(comThread,INFINITE);
|
|||
CloseHandle(netThread);
|
|||
CloseHandle(comThread);
|
|||
closesocket(cs);
|
|||
ResetEvent(connectionCloseEvent);
|
|||
ResetEvent(threadTermEvent);
|
|||
}
|
|||
cs=as;
|
|||
zero=0;
|
|||
ioctlsocket(cs,FIONBIO,&zero);
|
|||
62045e98 | Hamish Coleman | run_menu();
|
|
PurgeComm(hCom,PURGE_RXCLEAR|PURGE_RXABORT);
|
|||
78d4c0b0 | Hamish Coleman | netThread=CreateThread(NULL,0,wconsd_net_to_com,NULL,0,NULL);
|
|
comThread=CreateThread(NULL,0,wconsd_com_to_net,NULL,0,NULL);
|
|||
}
|
|||
break;
|
|||
case 2: /* The data connection has been broken */
|
|||
SetEvent(threadTermEvent);
|
|||
WaitForSingleObject(netThread,INFINITE);
|
|||
WaitForSingleObject(comThread,INFINITE);
|
|||
CloseHandle(netThread);
|
|||
CloseHandle(comThread);
|
|||
netThread=NULL;
|
|||
comThread=NULL;
|
|||
if (cs!=INVALID_SOCKET) {
|
|||
shutdown(cs,SD_BOTH);
|
|||
closesocket(cs);
|
|||
cs=INVALID_SOCKET;
|
|||
}
|
|||
ResetEvent(connectionCloseEvent);
|
|||
62045e98 | Hamish Coleman | ResetEvent(threadTermEvent);
|
|
if (com_autoclose) {
|
|||
close_com_port();
|
|||
ff03d660 | Hamish Coleman | }
|
|
78d4c0b0 | Hamish Coleman | break;
|
|
default:
|
|||
run=FALSE; // Stop the service - I want to get off!
|
|||
break;
|
|||
}
|
|||
}
|
|||
if (cs!=INVALID_SOCKET) {
|
|||
shutdown(cs,SD_BOTH);
|
|||
closesocket(cs);
|
|||
}
|
|||
if (netThread || comThread) {
|
|||
SetEvent(threadTermEvent);
|
|||
WaitForSingleObject(netThread,INFINITE);
|
|||
WaitForSingleObject(comThread,INFINITE);
|
|||
CloseHandle(netThread);
|
|||
CloseHandle(comThread);
|
|||
}
|
|||
CloseHandle(hCom);
|
|||
closesocket(ls);
|
|||
WSACleanup();
|
|||
}
|
|||
VOID WINAPI MyServiceCtrlHandler(DWORD opcode)
|
|||
{
|
|||
DWORD status;
|
|||
switch(opcode) {
|
|||
case SERVICE_CONTROL_STOP:
|
|||
wconsd_status.dwWin32ExitCode = 0;
|
|||
wconsd_status.dwCurrentState = SERVICE_STOP_PENDING;
|
|||
wconsd_status.dwCheckPoint = 0;
|
|||
wconsd_status.dwWaitHint = 0;
|
|||
if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
|
|||
status = GetLastError();
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
|
|
78d4c0b0 | Hamish Coleman | }
|
|
SetEvent(stopEvent);
|
|||
break;
|
|||
case SERVICE_CONTROL_INTERROGATE:
|
|||
// fall through to send current status
|
|||
break;
|
|||
default:
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: unrecognised opcode %ld\n",opcode);
|
|
78d4c0b0 | Hamish Coleman | break;
|
|
}
|
|||
// Send current status
|
|||
if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
|
|||
status = GetLastError();
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
|
|
78d4c0b0 | Hamish Coleman | }
|
|
return;
|
|||
}
|
|||
VOID WINAPI ServiceStart(DWORD argc, LPSTR *argv)
|
|||
{
|
|||
DWORD status;
|
|||
DWORD specificError;
|
|||
wconsd_status.dwServiceType = SERVICE_WIN32;
|
|||
wconsd_status.dwCurrentState = SERVICE_START_PENDING;
|
|||
wconsd_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
|||
wconsd_status.dwWin32ExitCode = 0;
|
|||
wconsd_status.dwServiceSpecificExitCode = 0;
|
|||
wconsd_status.dwCheckPoint = 0;
|
|||
wconsd_status.dwWaitHint = 0;
|
|||
25fa1d20 | Hamish Coleman | wconsd_statusHandle = RegisterServiceCtrlHandler(TEXT("wconsd"),MyServiceCtrlHandler);
|
|
78d4c0b0 | Hamish Coleman | ||
if (wconsd_statusHandle == (SERVICE_STATUS_HANDLE)0) {
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: RegisterServiceCtrlHandler failed %d\n", GetLastError());
|
|
78d4c0b0 | Hamish Coleman | return;
|
|
}
|
|||
status = wconsd_init(argc, argv, &specificError);
|
|||
if (status != NO_ERROR) {
|
|||
wconsd_status.dwCurrentState = SERVICE_STOPPED;
|
|||
wconsd_status.dwCheckPoint = 0;
|
|||
wconsd_status.dwWaitHint = 0;
|
|||
wconsd_status.dwWin32ExitCode = status;
|
|||
wconsd_status.dwServiceSpecificExitCode = specificError;
|
|||
SetServiceStatus(wconsd_statusHandle, &wconsd_status);
|
|||
return;
|
|||
}
|
|||
/* Initialisation complete - report running status */
|
|||
wconsd_status.dwCurrentState = SERVICE_RUNNING;
|
|||
wconsd_status.dwCheckPoint = 0;
|
|||
wconsd_status.dwWaitHint = 0;
|
|||
if (!SetServiceStatus(wconsd_statusHandle, &wconsd_status)) {
|
|||
status = GetLastError();
|
|||
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: SetServiceStatus error %ld\n",status);
|
|
78d4c0b0 | Hamish Coleman | }
|
|
wconsd_main();
|
|||
wconsd_status.dwCurrentState = SERVICE_STOPPED;
|
|||
wconsd_status.dwCheckPoint = 0;
|
|||
wconsd_status.dwWaitHint = 0;
|
|||
wconsd_status.dwWin32ExitCode = 0;
|
|||
wconsd_status.dwServiceSpecificExitCode = 0;
|
|||
SetServiceStatus(wconsd_statusHandle, &wconsd_status);
|
|||
return;
|
|||
}
|
|||
static void RegisterService(LPSTR path)
|
|||
{
|
|||
SC_HANDLE schSCManager, schService;
|
|||
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
|||
schService = CreateService(
|
|||
schSCManager,
|
|||
TEXT("wconsd"),
|
|||
ff03d660 | Hamish Coleman | TEXT("wconsd - a serial port server"),
|
|
78d4c0b0 | Hamish Coleman | SERVICE_ALL_ACCESS,
|
|
SERVICE_WIN32_OWN_PROCESS,
|
|||
SERVICE_AUTO_START,
|
|||
SERVICE_ERROR_NORMAL,
|
|||
path,
|
|||
NULL, NULL, NULL, NULL, NULL);
|
|||
if (schService == NULL) {
|
|||
printf("CreateService failed\n");
|
|||
} else {
|
|||
printf("Created service 'wconsd', binary path %s\n",path);
|
|||
printf("You should now start the service using the service manager.\n");
|
|||
}
|
|||
CloseServiceHandle(schService);
|
|||
}
|
|||
static void RemoveService(void)
|
|||
{
|
|||
SC_HANDLE schSCManager, schService;
|
|||
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
|
|||
if (schSCManager == NULL) {
|
|||
printf("Couldn't open service manager\n");
|
|||
return;
|
|||
}
|
|||
schService = OpenService(schSCManager, TEXT("wconsd"), DELETE);
|
|||
if (schService == NULL) {
|
|||
printf("Couldn't open wconsd service\n");
|
|||
return;
|
|||
}
|
|||
if (!DeleteService(schService)) {
|
|||
printf("Couldn't delete wconsd service\n");
|
|||
return;
|
|||
}
|
|||
printf("Deleted service 'wconsd'\n");
|
|||
CloseServiceHandle(schService);
|
|||
}
|
|||
static void usage(void)
|
|||
{
|
|||
printf("Usage: wconsd [-i pathname | -r | -d]\n");
|
|||
printf(" -i pathname install service 'wconsd'; pathname\n");
|
|||
printf(" must be the full path to the binary\n");
|
|||
printf(" -r remove service 'wconsd'\n");
|
|||
printf(" -d run wconsd in debug mode (in the foreground)\n");
|
|||
}
|
|||
int main(int argc, char **argv)
|
|||
{
|
|||
DWORD err;
|
|||
SERVICE_TABLE_ENTRY DispatchTable[] =
|
|||
{
|
|||
{ "wconsd", ServiceStart },
|
|||
{ NULL, NULL }
|
|||
62045e98 | Hamish Coleman | };
|
|
78d4c0b0 | Hamish Coleman | ||
9b2f3609 | Hamish Coleman | dprintf(0,"wconsd: start\n");
|
|
e6a4efc5 | Hamish Coleman | ||
62045e98 | Hamish Coleman | if (argc==1 || argc==0) {
|
|
78d4c0b0 | Hamish Coleman | if (!StartServiceCtrlDispatcher(DispatchTable)) {
|
|
9b2f3609 | Hamish Coleman | dprintf(1,"wconsd: StartServiceCtrlDispatcher error = %d\n", GetLastError());
|
|
78d4c0b0 | Hamish Coleman | }
|
|
return 0;
|
|||
}
|
|||
if (strcmp(argv[1],"-i")==0) {
|
|||
if (argc!=3) {
|
|||
usage();
|
|||
return 1;
|
|||
}
|
|||
RegisterService(argv[2]);
|
|||
} else if (strcmp(argv[1],"-r")==0) {
|
|||
RemoveService();
|
|||
} else if (strcmp(argv[1],"-d")==0) {
|
|||
int r;
|
|||
printf("wconsd: running in debug mode\n");
|
|||
r=wconsd_init(argc,argv,&err);
|
|||
if (r!=0) {
|
|||
ff03d660 | Hamish Coleman | printf("wconsd: debug: init failed, return code %d [%s]\n",r, err);
|
|
78d4c0b0 | Hamish Coleman | return 1;
|
|
}
|
|||
wconsd_main();
|
|||
return 0;
|
|||
} else usage();
|
|||
return 1;
|
|||
}
|