diff options
Diffstat (limited to 'map65/libm65/ptt.c')
| -rw-r--r-- | map65/libm65/ptt.c | 138 |
1 files changed, 97 insertions, 41 deletions
diff --git a/map65/libm65/ptt.c b/map65/libm65/ptt.c index b2c6cb48..3fb06d3c 100644 --- a/map65/libm65/ptt.c +++ b/map65/libm65/ptt.c @@ -1,49 +1,105 @@ #include <windows.h> #include <stdio.h> +#include "ptt.h" + +static HANDLE ptt_handle = INVALID_HANDLE_VALUE; +static enum { PTT_IDLE, PTT_KEYED, PTT_UNCERTAIN } ptt_state = PTT_IDLE; + +static int control_line(DWORD operation) +{ + DWORD error; + if (EscapeCommFunction(ptt_handle, operation)) + return PTT_OK; + error = GetLastError(); + return error == ERROR_DEVICE_NOT_CONNECTED || error == ERROR_NO_SUCH_DEVICE + ? PTT_DEVICE_LOST : PTT_ERROR; +} + +static void close_port(void) +{ + if (ptt_handle != INVALID_HANDLE_VALUE) + CloseHandle(ptt_handle); + ptt_handle = INVALID_HANDLE_VALUE; + ptt_state = PTT_IDLE; +} + +static int release_lines(void) +{ + int rts = control_line(CLRRTS); + int dtr = control_line(CLRDTR); + EscapeCommFunction(ptt_handle, CLRBREAK); + if (rts == PTT_DEVICE_LOST || dtr == PTT_DEVICE_LOST) + return PTT_DEVICE_LOST; + return rts || dtr ? PTT_ERROR : PTT_OK; +} + +static int control_failure(int result, int *iptt) +{ + if (result == PTT_DEVICE_LOST) { + close_port(); + *iptt = 0; + } else { + ptt_state = PTT_UNCERTAIN; + } + return result; +} int ptt_(int *nport, int *ntx, int *iptt) { - static HANDLE hFile; - static int open=0; - char s[64]; - int i3=0,i4=0,i5=0,i6=0,i9=0,i00=0; - - if(*nport==0) { - *iptt=*ntx; - return(0); - } - - if(*ntx && (!open)) { - if (*nport < 10) - snprintf(s, sizeof(s), "COM%d", *nport); - else - snprintf(s, sizeof(s), "\\\\.\\COM%d", *nport); - - hFile = CreateFileA(s, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL,NULL); - - if(hFile==INVALID_HANDLE_VALUE) { - // printf("PTT: Cannot open COM port %d.\n",*nport); - return 1; + int result; + char path[64]; + + if (!*ntx) { + if (ptt_handle != INVALID_HANDLE_VALUE) { + result = release_lines(); + if (result != PTT_OK) + return control_failure(result, iptt); + *iptt = 0; + if (!CloseHandle(ptt_handle)) { + ptt_state = PTT_UNCERTAIN; + return PTT_ERROR; + } + ptt_handle = INVALID_HANDLE_VALUE; + } + ptt_state = PTT_IDLE; + *iptt = 0; + return PTT_OK; + } + + if (ptt_state == PTT_UNCERTAIN) + return PTT_ERROR; + if (ptt_state == PTT_KEYED) { + *iptt = 1; + return PTT_OK; + } + if (!*nport) { + *iptt = *ntx; + return PTT_OK; } - open=1; - } - - if(*ntx && open) { - EscapeCommFunction(hFile,3); - EscapeCommFunction(hFile,5); - *iptt=1; - } - - else { - EscapeCommFunction(hFile,4); - EscapeCommFunction(hFile,6); - EscapeCommFunction(hFile,9); - i00=CloseHandle(hFile); - *iptt=0; - open=0; - } - if((i00+i3+i4+i5+i6+i9)==-99) return -1; //Silence compiler warning - return 0; + if (*nport < 10) + snprintf(path, sizeof(path), "COM%d", *nport); + else + snprintf(path, sizeof(path), "\\\\.\\COM%d", *nport); + ptt_handle = CreateFileA(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + if (ptt_handle == INVALID_HANDLE_VALUE) + return PTT_ERROR; + + result = control_line(SETRTS); + if (result == PTT_OK) + result = control_line(SETDTR); + if (result != PTT_OK) + return control_failure(result, iptt); + ptt_state = PTT_KEYED; + *iptt = 1; + return PTT_OK; +} + +void ptt_close(void) +{ + if (ptt_handle != INVALID_HANDLE_VALUE) { + release_lines(); + close_port(); + } } |
