oliverschmidt | 595b971 | 2006-05-31 19:20:06 +0000 | [diff] [blame] | 1 | #define WIN32_LEAN_AND_MEAN |
| 2 | #include <windows.h> |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "ppp.h" |
| 6 | #include "ctk.h" |
| 7 | |
| 8 | static HANDLE port = INVALID_HANDLE_VALUE; |
| 9 | |
| 10 | /*-----------------------------------------------------------------------------------*/ |
| 11 | static void |
| 12 | error_exit(char *message, int value) |
| 13 | { |
| 14 | console_exit(); |
| 15 | cprintf(message, value); |
| 16 | exit(EXIT_FAILURE); |
| 17 | } |
| 18 | /*-----------------------------------------------------------------------------------*/ |
| 19 | void |
| 20 | ppp_arch_init(void) |
| 21 | { |
| 22 | int i; |
| 23 | char buf[256] = ""; |
| 24 | DCB dcb; |
| 25 | COMMTIMEOUTS commtimeouts = {MAXDWORD, 0, 0, 0, 0}; |
| 26 | DWORD written; |
| 27 | DWORD read; |
| 28 | |
| 29 | port = CreateFile(__argv[1], GENERIC_READ | GENERIC_WRITE, |
| 30 | 0, NULL, OPEN_EXISTING, 0, NULL); |
| 31 | if(port == INVALID_HANDLE_VALUE) { |
| 32 | error_exit("CreateFile() error: %d\n", GetLastError()); |
| 33 | } |
| 34 | |
| 35 | if(!GetCommState(port, &dcb)) { |
| 36 | error_exit("GetCommState() error: %d\n", GetLastError()); |
| 37 | } |
| 38 | |
| 39 | for(i = 2; i < __argc; i++) { |
| 40 | strcat(buf, __argv[i]); |
| 41 | strcat(buf, " "); |
| 42 | } |
| 43 | if(!BuildCommDCB(buf, &dcb)) { |
| 44 | error_exit("BuildCommDCB() error: %d\n", GetLastError()); |
| 45 | } |
| 46 | |
| 47 | if(!SetCommState(port, &dcb)) { |
| 48 | error_exit("SetCommState() error: %d\n", GetLastError()); |
| 49 | } |
| 50 | |
| 51 | if(!SetCommTimeouts(port, &commtimeouts)) { |
| 52 | error_exit("SetCommTimeouts() error: %d\n", GetLastError()); |
| 53 | } |
| 54 | |
| 55 | if(!WriteFile(port, "CLIENTCLIENT", strlen("CLIENTCLIENT"), &written, NULL) || written != strlen("CLIENTCLIENT")) { |
| 56 | error_exit("WriteFile(CLIENTCLIENT) error: %d\n", GetLastError()); |
| 57 | } |
| 58 | |
| 59 | Sleep(500); |
| 60 | |
| 61 | if(!ReadFile(port, buf, strlen("CLIENTSERVER"), &read, NULL) || read != strlen("CLIENTSERVER")) { |
| 62 | error_exit("ReadFile(CLIENTSERVER) error: %d\n", GetLastError()); |
| 63 | } |
| 64 | } |
| 65 | /*-----------------------------------------------------------------------------------*/ |
| 66 | void |
| 67 | ppp_arch_putchar(u8_t c) |
| 68 | { |
| 69 | DWORD written; |
| 70 | |
| 71 | if(!WriteFile(port, &c, 1, &written, NULL)) { |
| 72 | error_exit("WriteFile() error: %d\n", GetLastError()); |
| 73 | } |
| 74 | |
| 75 | if(written != 1) { |
| 76 | error_exit("WriteFile() bytes written: %d\n", written); |
| 77 | } |
| 78 | } |
| 79 | /*-----------------------------------------------------------------------------------*/ |
| 80 | u8_t |
| 81 | ppp_arch_getchar(u8_t *c) |
| 82 | { |
| 83 | DWORD read; |
| 84 | |
| 85 | if (port == INVALID_HANDLE_VALUE) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | if(!ReadFile(port, (LPVOID)c, 1, &read, NULL)) { |
| 90 | error_exit("ReadFile() error: %d\n", GetLastError()); |
| 91 | } |
| 92 | |
| 93 | return (u8_t)read; |
| 94 | } |
| 95 | /*-----------------------------------------------------------------------------------*/ |