Complete ICatInformation implementation.
[wine.git] / server / serial.c
blob486b5abb477b4226cc0e8410423f439d7a84e881
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 <errno.h>
32 #ifdef HAVE_SYS_ERRNO_H
33 #include <sys/errno.h>
34 #endif
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <utime.h>
40 #include <termios.h>
41 #include <sys/ioctl.h>
43 #include "winerror.h"
44 #include "winbase.h"
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
49 #include "async.h"
51 static void serial_dump( struct object *obj, int verbose );
52 static int serial_get_fd( struct object *obj );
53 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags );
54 static int serial_get_poll_events( struct object *obj );
55 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count);
56 static void destroy_serial(struct object *obj);
57 static void serial_poll_event( struct object *obj, int event );
59 struct serial
61 struct object obj;
62 unsigned int access;
63 unsigned int attrib;
65 /* timeout values */
66 unsigned int readinterval;
67 unsigned int readconst;
68 unsigned int readmult;
69 unsigned int writeconst;
70 unsigned int writemult;
72 unsigned int eventmask;
73 unsigned int commerror;
75 struct termios original;
77 struct async_queue read_q;
78 struct async_queue write_q;
79 struct async_queue wait_q;
81 /* FIXME: add dcb, comm status, handler module, sharing */
84 static const struct object_ops serial_ops =
86 sizeof(struct serial), /* size */
87 serial_dump, /* dump */
88 default_poll_add_queue, /* add_queue */
89 default_poll_remove_queue, /* remove_queue */
90 default_poll_signaled, /* signaled */
91 no_satisfied, /* satisfied */
92 serial_get_poll_events, /* get_poll_events */
93 serial_poll_event, /* poll_event */
94 serial_get_fd, /* get_fd */
95 no_flush, /* flush */
96 serial_get_info, /* get_file_info */
97 serial_queue_async, /* queue_async */
98 destroy_serial /* destroy */
101 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
103 struct serial *serial;
104 struct termios tios;
105 int fd, flags = 0;
106 char *name;
108 if (!(name = mem_alloc( len + 1 ))) return NULL;
109 memcpy( name, nameptr, len );
110 name[len] = 0;
112 switch(access & (GENERIC_READ | GENERIC_WRITE))
114 case GENERIC_READ: flags |= O_RDONLY; break;
115 case GENERIC_WRITE: flags |= O_WRONLY; break;
116 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
117 default: break;
120 flags |= O_NONBLOCK;
122 fd = open( name, flags );
123 free( name );
124 if (fd < 0)
126 file_set_error();
127 return NULL;
130 /* check its really a serial port */
131 if (tcgetattr(fd,&tios))
133 file_set_error();
134 close( fd );
135 return NULL;
138 /* set the fd back to blocking if necessary */
139 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
140 if(0>fcntl(fd, F_SETFL, 0))
141 perror("fcntl");
143 if ((serial = alloc_object( &serial_ops, fd )))
145 serial->attrib = attributes;
146 serial->access = access;
147 serial->readinterval = 0;
148 serial->readmult = 0;
149 serial->readconst = 0;
150 serial->writemult = 0;
151 serial->writeconst = 0;
152 serial->eventmask = 0;
153 serial->commerror = 0;
154 init_async_queue(&serial->read_q);
155 init_async_queue(&serial->write_q);
156 init_async_queue(&serial->wait_q);
158 return serial;
161 static void destroy_serial( struct object *obj)
163 struct serial *serial = (struct serial *)obj;
165 destroy_async_queue(&serial->read_q);
166 destroy_async_queue(&serial->write_q);
167 destroy_async_queue(&serial->wait_q);
170 static void serial_dump( struct object *obj, int verbose )
172 struct serial *serial = (struct serial *)obj;
173 assert( obj->ops == &serial_ops );
174 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
177 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
179 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
182 static int serial_get_poll_events( struct object *obj )
184 struct serial *serial = (struct serial *)obj;
185 int events = 0;
186 assert( obj->ops == &serial_ops );
188 if(IS_READY(serial->read_q))
189 events |= POLLIN;
190 if(IS_READY(serial->write_q))
191 events |= POLLOUT;
192 if(IS_READY(serial->wait_q))
193 events |= POLLIN;
195 /* fprintf(stderr,"poll events are %04x\n",events); */
197 return events;
200 static int serial_get_fd( struct object *obj )
202 struct serial *serial = (struct serial *)obj;
203 assert( obj->ops == &serial_ops );
204 return serial->obj.fd;
207 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
209 struct serial *serial = (struct serial *) obj;
210 assert( obj->ops == &serial_ops );
212 if (reply)
214 reply->type = FILE_TYPE_CHAR;
215 reply->attr = 0;
216 reply->access_time = 0;
217 reply->write_time = 0;
218 reply->size_high = 0;
219 reply->size_low = 0;
220 reply->links = 0;
221 reply->index_high = 0;
222 reply->index_low = 0;
223 reply->serial = 0;
226 *flags = 0;
227 if(serial->attrib & FILE_FLAG_OVERLAPPED)
228 *flags |= FD_FLAG_OVERLAPPED;
229 else if(!((serial->readinterval == MAXDWORD) &&
230 (serial->readmult == 0) && (serial->readconst == 0)) )
231 *flags |= FD_FLAG_TIMEOUT;
233 return FD_TYPE_DEFAULT;
236 static void serial_poll_event(struct object *obj, int event)
238 struct serial *serial = (struct serial *)obj;
240 /* fprintf(stderr,"Poll event %02x\n",event); */
242 if(IS_READY(serial->read_q) && (POLLIN & event) )
243 async_notify(serial->read_q.head,STATUS_ALERTED);
245 if(IS_READY(serial->write_q) && (POLLOUT & event) )
246 async_notify(serial->write_q.head,STATUS_ALERTED);
248 if(IS_READY(serial->wait_q) && (POLLIN & event) )
249 async_notify(serial->wait_q.head,STATUS_ALERTED);
251 set_select_events(obj,obj->ops->get_poll_events(obj));
254 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
256 struct serial *serial = (struct serial *)obj;
257 struct async_queue *q;
258 struct async *async;
259 int timeout;
261 assert(obj->ops == &serial_ops);
263 switch(type)
265 case ASYNC_TYPE_READ:
266 q = &serial->read_q;
267 timeout = serial->readconst + serial->readmult*count;
268 break;
269 case ASYNC_TYPE_WAIT:
270 q = &serial->wait_q;
271 timeout = 0;
272 break;
273 case ASYNC_TYPE_WRITE:
274 q = &serial->write_q;
275 timeout = serial->writeconst + serial->writemult*count;
276 break;
277 default:
278 set_error(STATUS_INVALID_PARAMETER);
279 return;
282 async = find_async ( q, current, ptr );
284 if ( status == STATUS_PENDING )
286 struct pollfd pfd;
288 if ( !async )
289 async = create_async ( obj, current, ptr );
290 if ( !async )
291 return;
293 async->status = STATUS_PENDING;
294 if(!async->q)
296 async_add_timeout(async,timeout);
297 async_insert(q, async);
300 /* Check if the new pending request can be served immediately */
301 pfd.fd = obj->fd;
302 pfd.events = serial_get_poll_events ( obj );
303 pfd.revents = 0;
304 poll ( &pfd, 1, 0 );
306 if ( pfd.revents )
307 /* serial_poll_event() calls set_select_events() */
308 serial_poll_event ( obj, pfd.revents );
309 else
310 set_select_events ( obj, pfd.events );
311 return;
313 else if ( async ) destroy_async ( async );
314 else set_error ( STATUS_INVALID_PARAMETER );
316 set_select_events ( obj, serial_get_poll_events ( obj ));
319 /* create a serial */
320 DECL_HANDLER(create_serial)
322 struct serial *serial;
324 reply->handle = 0;
325 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
327 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
328 release_object( serial );
332 DECL_HANDLER(get_serial_info)
334 struct serial *serial;
336 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
338 /* timeouts */
339 reply->readinterval = serial->readinterval;
340 reply->readconst = serial->readconst;
341 reply->readmult = serial->readmult;
342 reply->writeconst = serial->writeconst;
343 reply->writemult = serial->writemult;
345 /* event mask */
346 reply->eventmask = serial->eventmask;
348 /* comm port error status */
349 reply->commerror = serial->commerror;
351 release_object( serial );
355 DECL_HANDLER(set_serial_info)
357 struct serial *serial;
359 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
361 /* timeouts */
362 if(req->flags & SERIALINFO_SET_TIMEOUTS)
364 serial->readinterval = req->readinterval;
365 serial->readconst = req->readconst;
366 serial->readmult = req->readmult;
367 serial->writeconst = req->writeconst;
368 serial->writemult = req->writemult;
371 /* event mask */
372 if(req->flags & SERIALINFO_SET_MASK)
374 serial->eventmask = req->eventmask;
375 if(!serial->eventmask)
377 while(serial->wait_q.head)
379 async_notify(serial->wait_q.head, STATUS_SUCCESS);
380 destroy_async(serial->wait_q.head);
385 /* comm port error status */
386 if(req->flags & SERIALINFO_SET_ERROR)
388 serial->commerror = req->commerror;
391 release_object( serial );