Added missing defines and fixed a few typos.
[wine.git] / server / serial.c
blob65dd087caffefac2bea836b04039f097b6e02866
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
50 #include "file.h"
51 #include "handle.h"
52 #include "thread.h"
53 #include "request.h"
54 #include "async.h"
56 static void serial_dump( struct object *obj, int verbose );
57 static struct fd *serial_get_fd( struct object *obj );
58 static void serial_destroy(struct object *obj);
60 static int serial_get_poll_events( struct fd *fd );
61 static void serial_poll_event( struct fd *fd, int event );
62 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
63 static int serial_flush( struct fd *fd, struct event **event );
64 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
66 struct serial
68 struct object obj;
69 struct fd *fd;
70 unsigned int access;
71 unsigned int attrib;
73 /* timeout values */
74 unsigned int readinterval;
75 unsigned int readconst;
76 unsigned int readmult;
77 unsigned int writeconst;
78 unsigned int writemult;
80 unsigned int eventmask;
81 unsigned int commerror;
83 struct termios original;
85 struct async_queue read_q;
86 struct async_queue write_q;
87 struct async_queue wait_q;
89 /* FIXME: add dcb, comm status, handler module, sharing */
92 static const struct object_ops serial_ops =
94 sizeof(struct serial), /* size */
95 serial_dump, /* dump */
96 default_fd_add_queue, /* add_queue */
97 default_fd_remove_queue, /* remove_queue */
98 default_fd_signaled, /* signaled */
99 no_satisfied, /* satisfied */
100 serial_get_fd, /* get_fd */
101 serial_destroy /* destroy */
104 static const struct fd_ops serial_fd_ops =
106 serial_get_poll_events, /* get_poll_events */
107 serial_poll_event, /* poll_event */
108 serial_flush, /* flush */
109 serial_get_info, /* get_file_info */
110 serial_queue_async /* queue_async */
113 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
115 struct serial *serial;
116 struct termios tios;
117 int fd, flags = 0;
118 char *name;
120 if (!(name = mem_alloc( len + 1 ))) return NULL;
121 memcpy( name, nameptr, len );
122 name[len] = 0;
124 switch(access & (GENERIC_READ | GENERIC_WRITE))
126 case GENERIC_READ: flags |= O_RDONLY; break;
127 case GENERIC_WRITE: flags |= O_WRONLY; break;
128 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
129 default: break;
132 flags |= O_NONBLOCK;
134 fd = open( name, flags );
135 free( name );
136 if (fd < 0)
138 file_set_error();
139 return NULL;
142 /* check its really a serial port */
143 if (tcgetattr(fd,&tios))
145 file_set_error();
146 close( fd );
147 return NULL;
150 /* set the fd back to blocking if necessary */
151 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
152 if(0>fcntl(fd, F_SETFL, 0))
153 perror("fcntl");
155 if (!(serial = alloc_object( &serial_ops )))
157 close( fd );
158 return NULL;
160 serial->attrib = attributes;
161 serial->access = access;
162 serial->readinterval = 0;
163 serial->readmult = 0;
164 serial->readconst = 0;
165 serial->writemult = 0;
166 serial->writeconst = 0;
167 serial->eventmask = 0;
168 serial->commerror = 0;
169 init_async_queue(&serial->read_q);
170 init_async_queue(&serial->write_q);
171 init_async_queue(&serial->wait_q);
172 if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, fd, &serial->obj )))
174 release_object( serial );
175 return NULL;
177 return serial;
180 static struct fd *serial_get_fd( struct object *obj )
182 struct serial *serial = (struct serial *)obj;
183 return (struct fd *)grab_object( serial->fd );
186 static void serial_destroy( struct object *obj)
188 struct serial *serial = (struct serial *)obj;
190 destroy_async_queue(&serial->read_q);
191 destroy_async_queue(&serial->write_q);
192 destroy_async_queue(&serial->wait_q);
193 if (serial->fd) release_object( serial->fd );
196 static void serial_dump( struct object *obj, int verbose )
198 struct serial *serial = (struct serial *)obj;
199 assert( obj->ops == &serial_ops );
200 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
203 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
205 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
208 static int serial_get_poll_events( struct fd *fd )
210 struct serial *serial = get_fd_user( fd );
211 int events = 0;
212 assert( serial->obj.ops == &serial_ops );
214 if(IS_READY(serial->read_q))
215 events |= POLLIN;
216 if(IS_READY(serial->write_q))
217 events |= POLLOUT;
218 if(IS_READY(serial->wait_q))
219 events |= POLLIN;
221 /* fprintf(stderr,"poll events are %04x\n",events); */
223 return events;
226 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
228 struct serial *serial = get_fd_user( fd );
229 assert( serial->obj.ops == &serial_ops );
231 if (reply)
233 reply->type = FILE_TYPE_CHAR;
234 reply->attr = 0;
235 reply->access_time = 0;
236 reply->write_time = 0;
237 reply->size_high = 0;
238 reply->size_low = 0;
239 reply->links = 0;
240 reply->index_high = 0;
241 reply->index_low = 0;
242 reply->serial = 0;
245 *flags = 0;
246 if(serial->attrib & FILE_FLAG_OVERLAPPED)
247 *flags |= FD_FLAG_OVERLAPPED;
248 else if(!((serial->readinterval == MAXDWORD) &&
249 (serial->readmult == 0) && (serial->readconst == 0)) )
250 *flags |= FD_FLAG_TIMEOUT;
252 return FD_TYPE_DEFAULT;
255 static void serial_poll_event(struct fd *fd, int event)
257 struct serial *serial = get_fd_user( fd );
259 /* fprintf(stderr,"Poll event %02x\n",event); */
261 if(IS_READY(serial->read_q) && (POLLIN & event) )
262 async_notify(serial->read_q.head,STATUS_ALERTED);
264 if(IS_READY(serial->write_q) && (POLLOUT & event) )
265 async_notify(serial->write_q.head,STATUS_ALERTED);
267 if(IS_READY(serial->wait_q) && (POLLIN & event) )
268 async_notify(serial->wait_q.head,STATUS_ALERTED);
270 set_fd_events( fd, serial_get_poll_events(fd) );
273 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
275 struct serial *serial = get_fd_user( fd );
276 struct async_queue *q;
277 struct async *async;
278 int timeout;
280 assert(serial->obj.ops == &serial_ops);
282 switch(type)
284 case ASYNC_TYPE_READ:
285 q = &serial->read_q;
286 timeout = serial->readconst + serial->readmult*count;
287 break;
288 case ASYNC_TYPE_WAIT:
289 q = &serial->wait_q;
290 timeout = 0;
291 break;
292 case ASYNC_TYPE_WRITE:
293 q = &serial->write_q;
294 timeout = serial->writeconst + serial->writemult*count;
295 break;
296 default:
297 set_error(STATUS_INVALID_PARAMETER);
298 return;
301 async = find_async ( q, current, ptr );
303 if ( status == STATUS_PENDING )
305 int events;
307 if ( !async )
308 async = create_async ( &serial->obj, current, ptr );
309 if ( !async )
310 return;
312 async->status = STATUS_PENDING;
313 if(!async->q)
315 async_add_timeout(async,timeout);
316 async_insert(q, async);
319 /* Check if the new pending request can be served immediately */
320 events = check_fd_events( fd, serial_get_poll_events( fd ) );
321 if (events)
323 /* serial_poll_event() calls set_select_events() */
324 serial_poll_event( fd, events );
325 return;
328 else if ( async ) destroy_async ( async );
329 else set_error ( STATUS_INVALID_PARAMETER );
331 set_fd_events ( fd, serial_get_poll_events( fd ));
334 static int serial_flush( struct fd *fd, struct event **event )
336 /* MSDN says: If hFile is a handle to a communications device,
337 * the function only flushes the transmit buffer.
339 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
340 if (!ret) file_set_error();
341 return ret;
344 /* create a serial */
345 DECL_HANDLER(create_serial)
347 struct serial *serial;
349 reply->handle = 0;
350 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
352 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
353 release_object( serial );
357 DECL_HANDLER(get_serial_info)
359 struct serial *serial;
361 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
363 /* timeouts */
364 reply->readinterval = serial->readinterval;
365 reply->readconst = serial->readconst;
366 reply->readmult = serial->readmult;
367 reply->writeconst = serial->writeconst;
368 reply->writemult = serial->writemult;
370 /* event mask */
371 reply->eventmask = serial->eventmask;
373 /* comm port error status */
374 reply->commerror = serial->commerror;
376 release_object( serial );
380 DECL_HANDLER(set_serial_info)
382 struct serial *serial;
384 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
386 /* timeouts */
387 if(req->flags & SERIALINFO_SET_TIMEOUTS)
389 serial->readinterval = req->readinterval;
390 serial->readconst = req->readconst;
391 serial->readmult = req->readmult;
392 serial->writeconst = req->writeconst;
393 serial->writemult = req->writemult;
396 /* event mask */
397 if(req->flags & SERIALINFO_SET_MASK)
399 serial->eventmask = req->eventmask;
400 if(!serial->eventmask)
402 while(serial->wait_q.head)
404 async_notify(serial->wait_q.head, STATUS_SUCCESS);
405 destroy_async(serial->wait_q.head);
410 /* comm port error status */
411 if(req->flags & SERIALINFO_SET_ERROR)
413 serial->commerror = req->commerror;
416 release_object( serial );