Release 20030408.
[wine/gsoc-2012-control.git] / server / serial.c
blob3831b7e22407bab17292ac9f6896a549071b695b
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 <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <unistd.h>
35 #ifdef HAVE_UTIME_H
36 #include <utime.h>
37 #endif
38 #ifdef HAVE_TERMIOS_H
39 #include <termios.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
45 #include "winerror.h"
46 #include "winbase.h"
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
52 #include "async.h"
54 static void serial_dump( struct object *obj, int verbose );
55 static struct fd *serial_get_fd( struct object *obj );
56 static void serial_destroy(struct object *obj);
58 static int serial_get_poll_events( struct fd *fd );
59 static void serial_poll_event( struct fd *fd, int event );
60 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags );
61 static int serial_flush( struct fd *fd );
62 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count);
64 struct serial
66 struct object obj;
67 struct fd *fd;
68 unsigned int access;
69 unsigned int attrib;
71 /* timeout values */
72 unsigned int readinterval;
73 unsigned int readconst;
74 unsigned int readmult;
75 unsigned int writeconst;
76 unsigned int writemult;
78 unsigned int eventmask;
79 unsigned int commerror;
81 struct termios original;
83 struct async_queue read_q;
84 struct async_queue write_q;
85 struct async_queue wait_q;
87 /* FIXME: add dcb, comm status, handler module, sharing */
90 static const struct object_ops serial_ops =
92 sizeof(struct serial), /* size */
93 serial_dump, /* dump */
94 default_fd_add_queue, /* add_queue */
95 default_fd_remove_queue, /* remove_queue */
96 default_fd_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 serial_get_fd, /* get_fd */
99 serial_destroy /* destroy */
102 static const struct fd_ops serial_fd_ops =
104 serial_get_poll_events, /* get_poll_events */
105 serial_poll_event, /* poll_event */
106 serial_flush, /* flush */
107 serial_get_info, /* get_file_info */
108 serial_queue_async /* queue_async */
111 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
113 struct serial *serial;
114 struct termios tios;
115 int fd, flags = 0;
116 char *name;
118 if (!(name = mem_alloc( len + 1 ))) return NULL;
119 memcpy( name, nameptr, len );
120 name[len] = 0;
122 switch(access & (GENERIC_READ | GENERIC_WRITE))
124 case GENERIC_READ: flags |= O_RDONLY; break;
125 case GENERIC_WRITE: flags |= O_WRONLY; break;
126 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
127 default: break;
130 flags |= O_NONBLOCK;
132 fd = open( name, flags );
133 free( name );
134 if (fd < 0)
136 file_set_error();
137 return NULL;
140 /* check its really a serial port */
141 if (tcgetattr(fd,&tios))
143 file_set_error();
144 close( fd );
145 return NULL;
148 /* set the fd back to blocking if necessary */
149 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
150 if(0>fcntl(fd, F_SETFL, 0))
151 perror("fcntl");
153 if (!(serial = alloc_object( &serial_ops )))
155 close( fd );
156 return NULL;
158 serial->attrib = attributes;
159 serial->access = access;
160 serial->readinterval = 0;
161 serial->readmult = 0;
162 serial->readconst = 0;
163 serial->writemult = 0;
164 serial->writeconst = 0;
165 serial->eventmask = 0;
166 serial->commerror = 0;
167 init_async_queue(&serial->read_q);
168 init_async_queue(&serial->write_q);
169 init_async_queue(&serial->wait_q);
170 if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, fd, &serial->obj )))
172 release_object( serial );
173 return NULL;
175 return serial;
178 static struct fd *serial_get_fd( struct object *obj )
180 struct serial *serial = (struct serial *)obj;
181 return (struct fd *)grab_object( serial->fd );
184 static void serial_destroy( struct object *obj)
186 struct serial *serial = (struct serial *)obj;
188 destroy_async_queue(&serial->read_q);
189 destroy_async_queue(&serial->write_q);
190 destroy_async_queue(&serial->wait_q);
191 if (serial->fd) release_object( serial->fd );
194 static void serial_dump( struct object *obj, int verbose )
196 struct serial *serial = (struct serial *)obj;
197 assert( obj->ops == &serial_ops );
198 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
201 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
203 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
206 static int serial_get_poll_events( struct fd *fd )
208 struct serial *serial = get_fd_user( fd );
209 int events = 0;
210 assert( serial->obj.ops == &serial_ops );
212 if(IS_READY(serial->read_q))
213 events |= POLLIN;
214 if(IS_READY(serial->write_q))
215 events |= POLLOUT;
216 if(IS_READY(serial->wait_q))
217 events |= POLLIN;
219 /* fprintf(stderr,"poll events are %04x\n",events); */
221 return events;
224 static int serial_get_info( struct fd *fd, struct get_file_info_reply *reply, int *flags )
226 struct serial *serial = get_fd_user( fd );
227 assert( serial->obj.ops == &serial_ops );
229 if (reply)
231 reply->type = FILE_TYPE_CHAR;
232 reply->attr = 0;
233 reply->access_time = 0;
234 reply->write_time = 0;
235 reply->size_high = 0;
236 reply->size_low = 0;
237 reply->links = 0;
238 reply->index_high = 0;
239 reply->index_low = 0;
240 reply->serial = 0;
243 *flags = 0;
244 if(serial->attrib & FILE_FLAG_OVERLAPPED)
245 *flags |= FD_FLAG_OVERLAPPED;
246 else if(!((serial->readinterval == MAXDWORD) &&
247 (serial->readmult == 0) && (serial->readconst == 0)) )
248 *flags |= FD_FLAG_TIMEOUT;
250 return FD_TYPE_DEFAULT;
253 static void serial_poll_event(struct fd *fd, int event)
255 struct serial *serial = get_fd_user( fd );
257 /* fprintf(stderr,"Poll event %02x\n",event); */
259 if(IS_READY(serial->read_q) && (POLLIN & event) )
260 async_notify(serial->read_q.head,STATUS_ALERTED);
262 if(IS_READY(serial->write_q) && (POLLOUT & event) )
263 async_notify(serial->write_q.head,STATUS_ALERTED);
265 if(IS_READY(serial->wait_q) && (POLLIN & event) )
266 async_notify(serial->wait_q.head,STATUS_ALERTED);
268 set_fd_events( fd, serial_get_poll_events(fd) );
271 static void serial_queue_async(struct fd *fd, void *ptr, unsigned int status, int type, int count)
273 struct serial *serial = get_fd_user( fd );
274 struct async_queue *q;
275 struct async *async;
276 int timeout;
278 assert(serial->obj.ops == &serial_ops);
280 switch(type)
282 case ASYNC_TYPE_READ:
283 q = &serial->read_q;
284 timeout = serial->readconst + serial->readmult*count;
285 break;
286 case ASYNC_TYPE_WAIT:
287 q = &serial->wait_q;
288 timeout = 0;
289 break;
290 case ASYNC_TYPE_WRITE:
291 q = &serial->write_q;
292 timeout = serial->writeconst + serial->writemult*count;
293 break;
294 default:
295 set_error(STATUS_INVALID_PARAMETER);
296 return;
299 async = find_async ( q, current, ptr );
301 if ( status == STATUS_PENDING )
303 int events;
305 if ( !async )
306 async = create_async ( &serial->obj, current, ptr );
307 if ( !async )
308 return;
310 async->status = STATUS_PENDING;
311 if(!async->q)
313 async_add_timeout(async,timeout);
314 async_insert(q, async);
317 /* Check if the new pending request can be served immediately */
318 events = check_fd_events( fd, serial_get_poll_events( fd ) );
319 if (events)
321 /* serial_poll_event() calls set_select_events() */
322 serial_poll_event( fd, events );
323 return;
326 else if ( async ) destroy_async ( async );
327 else set_error ( STATUS_INVALID_PARAMETER );
329 set_fd_events ( fd, serial_get_poll_events( fd ));
332 static int serial_flush( struct fd *fd )
334 /* MSDN says: If hFile is a handle to a communications device,
335 * the function only flushes the transmit buffer.
337 int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
338 if (!ret) file_set_error();
339 return ret;
342 /* create a serial */
343 DECL_HANDLER(create_serial)
345 struct serial *serial;
347 reply->handle = 0;
348 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
350 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
351 release_object( serial );
355 DECL_HANDLER(get_serial_info)
357 struct serial *serial;
359 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
361 /* timeouts */
362 reply->readinterval = serial->readinterval;
363 reply->readconst = serial->readconst;
364 reply->readmult = serial->readmult;
365 reply->writeconst = serial->writeconst;
366 reply->writemult = serial->writemult;
368 /* event mask */
369 reply->eventmask = serial->eventmask;
371 /* comm port error status */
372 reply->commerror = serial->commerror;
374 release_object( serial );
378 DECL_HANDLER(set_serial_info)
380 struct serial *serial;
382 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
384 /* timeouts */
385 if(req->flags & SERIALINFO_SET_TIMEOUTS)
387 serial->readinterval = req->readinterval;
388 serial->readconst = req->readconst;
389 serial->readmult = req->readmult;
390 serial->writeconst = req->writeconst;
391 serial->writemult = req->writemult;
394 /* event mask */
395 if(req->flags & SERIALINFO_SET_MASK)
397 serial->eventmask = req->eventmask;
398 if(!serial->eventmask)
400 while(serial->wait_q.head)
402 async_notify(serial->wait_q.head, STATUS_SUCCESS);
403 destroy_async(serial->wait_q.head);
408 /* comm port error status */
409 if(req->flags & SERIALINFO_SET_ERROR)
411 serial->commerror = req->commerror;
414 release_object( serial );