]> icculus.org git repositories - btb/d2x.git/blob - arch/dos/comm/commlib.c
more header cleanup
[btb/d2x.git] / arch / dos / comm / commlib.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4
5 #include "fix.h"
6 #include "timer.h"
7 #include "mono.h"
8 #include "commlib.h"
9
10 #define F_1_1000 (F1_0/1000) // 1/1000
11
12 struct portparam { int base,irq; };
13 struct portparam portparams[4] = {{0x3f8, 4}, {0x2f8, 3}, {0x3e8, 4}, {0x2e8, 3}};
14
15 /* return Data Carrier Detect status */
16 int comm_get_dcd(comm_port *port) {
17     return (inportb(port->MSR) & MSR_DCD) != 0;
18 }
19
20 void comm_printf(PORT *port, char *msg, ...) {
21     char buf[1024];
22     va_list vp;
23
24     va_start(vp, msg);
25     vsprintf(buf, msg, vp);
26     va_end(vp);
27 mprintf((0, "comm_printf: writing \"%s\"\n", buf));
28     WriteBuffer(port, buf, strlen(buf));
29 }
30
31 PORT * PortOpenGreenleafFast(int port, int baud, char parity, int databits, int stopbits)
32 {
33     PORT *com;
34     static int did_init = 0;
35
36     if (!did_init) {
37        dzcomm_init();
38        did_init = 1;
39     }
40
41     if (!(com = comm_port_init(port))) {
42        mprintf((1,"comm_port_init: %s\n", szDZCommErr));
43        return NULL;
44     }
45     com->nBaud = baud;
46     com->nPort = portparams[port].base;
47     com->nIRQ = portparams[port].irq;
48     switch (parity) {
49        case 'N': com->nParity = NO_PARITY; break;
50        case 'E': com->nParity = EVEN_PARITY; break;
51        case 'O': com->nParity = ODD_PARITY; break;
52     }
53     switch (databits) {
54        case 5: com->nData = BITS_5; break;
55        case 6: com->nData = BITS_6; break;
56        case 7: com->nData = BITS_7; break;
57        case 8: com->nData = BITS_8; break;
58     }
59     switch (stopbits) {
60        case 1: com->nStop = STOP_1; break;
61        case 2: com->nStop = STOP_2; break;
62     }
63     com->control_type = NO_CONTROL;
64     if (!comm_port_install_handler(com)) {
65        mprintf((1,"comm_port_install: %s\n", szDZCommErr));
66        comm_port_delete(com);
67        return NULL;
68     }
69    return com;
70 }
71
72 void SetDtr(PORT *port,int state)
73 {
74     return;
75 }
76
77 void SetRts(PORT *port,int state)
78 {
79     return;
80 }
81
82 void UseRtsCts(PORT *port,int state)
83 {
84     #if 0 /* disabled for now */
85     if (state && port->control_type != RTS_CTS) {
86        port->cts=CTS_ON;
87        port->rts=RTS_ON;
88        if (!queue_empty(port->OutBuf))
89            outportb(port->MCR,inportb(port->MCR)|0x02); //setting RTS
90        port->control_type = RTS_CTS;
91     } else if (!state && port->control_type == RTS_CTS) {
92        port->control_type = NO_CONTROL;
93        if (!queue_empty(port->OutBuf))
94            interrupt_on(port, THREINT);
95     }
96     #endif
97     return;
98 }
99
100 void WriteChar(PORT *port, char ch)
101 {
102     //mprintf((0, "(%d,%d)", port->OutBuf->head, port->OutBuf->tail));
103     //mprintf((0, ">%02x ", (unsigned char)ch));
104     comm_port_out(port, ch);
105     return;
106 }
107
108 void ClearRXBuffer(PORT *port)
109 {
110     queue_reset(port->InBuf);
111 }
112
113 int ReadBufferTimed(PORT *port, char *buf, int length, int timeout)
114 {
115     fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
116     int c;
117
118     if (!length)
119        return 1; // ok
120     while (stop >= timer_get_fixed_seconds()) {
121        if ((c = comm_port_test(port)) >= 0) {
122            mprintf((0, "<%02x ", c));
123            *(buf++) = c;
124            if (!(--length))
125                return 1; // ok
126        }
127
128     }
129     return 0; // timeout
130 }
131
132 int Change8259Priority(int irq)
133 {
134     return ASSUCCESS;
135 }
136
137 int FastSetPortHardware(int port, int IRQ, int baseaddr)
138 {
139     portparams[port].irq = IRQ;
140     portparams[port].base = baseaddr;
141     return 0;
142 }
143
144 int FastGetPortHardware(int port, int *IRQ, int *baseaddr)
145 {
146     *IRQ = portparams[port].irq;
147     *baseaddr = portparams[port].base;
148     return 0;
149 }
150
151 void FastSet16550TriggerLevel(int level)
152 {
153     return;
154 }
155 void FastSet16550UseTXFifos(int on)
156 {
157     return;
158 }
159
160 void FastSavePortParameters(int port)
161 {
162 }
163
164 int PortClose(PORT *port)
165 {
166     comm_port_delete(port);
167     return 0;
168 }
169
170 void FastRestorePortParameters(int port)
171 {
172 }
173
174 int GetCd(PORT *port)
175 {
176     return comm_get_dcd(port);
177 }
178
179 int ReadCharTimed(PORT *port, int timeout)
180 {
181     fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
182     int c = -1;
183
184     while (stop >= timer_get_fixed_seconds() && (c = comm_port_test(port)) < 0)
185        ;
186     if (c >= 0)
187        mprintf((0, "<%02x ", c));
188     return c;
189 }
190
191 int ReadChar(PORT *port)
192 {
193     int c = comm_port_test(port);
194     //if (c >= 0)
195     //   mprintf((0, "<%02x ", c));
196     return c;
197 }
198
199 void ClearLineStatus(PORT *port)
200 {
201 }
202
203 void WriteBuffer(PORT *port, char *pbuff, int len)
204 {
205     mprintf((0, ">x%d ", len));
206     while (len--)
207        comm_port_out(port, *(pbuff++));
208 }
209
210
211 int HMInputLine(PORT *port, int timeout, char *buf, int bufsize)
212 {
213     fix stop = timer_get_fixed_seconds() + timeout * F_1_1000;
214     int c;
215     char *p = buf;
216     int bufleft = bufsize;
217
218 mprintf((1, "port->InBuf->head=%d, port->InBuf->tail=%d\n", port->InBuf->head,
219   port->InBuf->tail));
220     while (stop >= timer_get_fixed_seconds()) {
221        if ((c = comm_port_test(port)) >= 0) {
222 mprintf((0,"got char %d (%c)", c, c >= 32 ? c : '.'));
223            if (c == '\n' && p > buf) {
224                 *p = 0;
225                 mprintf((0,"got line \"%s\"", buf));
226                if (p - buf >= 2 && !strncmp(buf, "AT", 2)) { /* assume AT... response is echo */
227                    p = buf;
228                    bufleft = bufsize;
229                } else {
230                    *p = 0;
231                    return 1; // ok
232                }
233            }
234            if (c != '\r' && c != '\n') {
235                if (!bufleft)
236                    return -1; // buffer overflow
237                *(p++) = c;
238                bufleft--;
239            }
240        }
241     }
242 mprintf((0,"hminputline: timeout\n"));
243     return 0; // timeout
244 }
245
246 void HMWaitForOK(int timeout, char *buf)
247 {
248     /* what is this supposed to do without a port handle? */
249 }
250
251 void HMSendStringNoWait(PORT *port, char *pbuf, int a)
252 {
253     comm_printf(port, "%s\r", pbuf);
254 }
255
256 int HMSendString(PORT *port, char *msg)
257 {
258 //    char buf[80];
259
260     HMSendStringNoWait(port, msg, -2);
261 //    HMInputLine(port, 5000, buf, sizeof(buf) - 1); /* 5 sec timeout */
262 //    if (!strncmp(buf, "OK", 2))
263 //       return 0; // Ok
264 //    return -1; // Error
265     if(HMInputLine(port, 5000, msg, sizeof(msg) - 1) >= 0)
266      return 0;
267     return -1;
268 }
269 //added on 11/5/98 by Victor Rachels to wait less
270 int HMSendStringWait(PORT *port, char *msg, int wait)
271 {
272   HMSendStringNoWait(port, msg, -2);
273    if(HMInputLine(port, wait, msg, sizeof(msg) - 1) >= 0)
274     return 0;
275   return -1;
276 }
277 //end this section addition - VR
278
279 void HMReset(PORT *port)
280
281 }
282
283 void HMDial(PORT *port, char *pPhoneNum)
284 {
285     comm_printf(port, "ATD%s\r", pPhoneNum);
286 }
287
288 void HMAnswer(PORT *port)
289 {
290     comm_printf(port, "ATA\r");
291 }
292
293 void ClearTXBuffer(PORT *port)
294 {
295     queue_reset(port->OutBuf);
296 }
297
298 int GetLineStatus(PORT *port)
299 {
300     return 0;
301 }