Partial implementation of RtlDosPathNameToNtPathName_U.
[wine.git] / server / serial.c
blobf267234904602fc60ba5e07bedeb9ffd707ebbf3
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 );
58 static int serial_flush( struct object *obj );
60 struct serial
62 struct object obj;
63 unsigned int access;
64 unsigned int attrib;
66 /* timeout values */
67 unsigned int readinterval;
68 unsigned int readconst;
69 unsigned int readmult;
70 unsigned int writeconst;
71 unsigned int writemult;
73 unsigned int eventmask;
74 unsigned int commerror;
76 struct termios original;
78 struct async_queue read_q;
79 struct async_queue write_q;
80 struct async_queue wait_q;
82 /* FIXME: add dcb, comm status, handler module, sharing */
85 static const struct object_ops serial_ops =
87 sizeof(struct serial), /* size */
88 serial_dump, /* dump */
89 default_poll_add_queue, /* add_queue */
90 default_poll_remove_queue, /* remove_queue */
91 default_poll_signaled, /* signaled */
92 no_satisfied, /* satisfied */
93 serial_get_poll_events, /* get_poll_events */
94 serial_poll_event, /* poll_event */
95 serial_get_fd, /* get_fd */
96 serial_flush, /* flush */
97 serial_get_info, /* get_file_info */
98 serial_queue_async, /* queue_async */
99 destroy_serial /* destroy */
102 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access, int attributes )
104 struct serial *serial;
105 struct termios tios;
106 int fd, flags = 0;
107 char *name;
109 if (!(name = mem_alloc( len + 1 ))) return NULL;
110 memcpy( name, nameptr, len );
111 name[len] = 0;
113 switch(access & (GENERIC_READ | GENERIC_WRITE))
115 case GENERIC_READ: flags |= O_RDONLY; break;
116 case GENERIC_WRITE: flags |= O_WRONLY; break;
117 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
118 default: break;
121 flags |= O_NONBLOCK;
123 fd = open( name, flags );
124 free( name );
125 if (fd < 0)
127 file_set_error();
128 return NULL;
131 /* check its really a serial port */
132 if (tcgetattr(fd,&tios))
134 file_set_error();
135 close( fd );
136 return NULL;
139 /* set the fd back to blocking if necessary */
140 if( ! (attributes & FILE_FLAG_OVERLAPPED) )
141 if(0>fcntl(fd, F_SETFL, 0))
142 perror("fcntl");
144 if ((serial = alloc_object( &serial_ops, fd )))
146 serial->attrib = attributes;
147 serial->access = access;
148 serial->readinterval = 0;
149 serial->readmult = 0;
150 serial->readconst = 0;
151 serial->writemult = 0;
152 serial->writeconst = 0;
153 serial->eventmask = 0;
154 serial->commerror = 0;
155 init_async_queue(&serial->read_q);
156 init_async_queue(&serial->write_q);
157 init_async_queue(&serial->wait_q);
159 return serial;
162 static void destroy_serial( struct object *obj)
164 struct serial *serial = (struct serial *)obj;
166 destroy_async_queue(&serial->read_q);
167 destroy_async_queue(&serial->write_q);
168 destroy_async_queue(&serial->wait_q);
171 static void serial_dump( struct object *obj, int verbose )
173 struct serial *serial = (struct serial *)obj;
174 assert( obj->ops == &serial_ops );
175 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
178 struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
180 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
183 static int serial_get_poll_events( struct object *obj )
185 struct serial *serial = (struct serial *)obj;
186 int events = 0;
187 assert( obj->ops == &serial_ops );
189 if(IS_READY(serial->read_q))
190 events |= POLLIN;
191 if(IS_READY(serial->write_q))
192 events |= POLLOUT;
193 if(IS_READY(serial->wait_q))
194 events |= POLLIN;
196 /* fprintf(stderr,"poll events are %04x\n",events); */
198 return events;
201 static int serial_get_fd( struct object *obj )
203 struct serial *serial = (struct serial *)obj;
204 assert( obj->ops == &serial_ops );
205 return serial->obj.fd;
208 static int serial_get_info( struct object *obj, struct get_file_info_reply *reply, int *flags )
210 struct serial *serial = (struct serial *) obj;
211 assert( obj->ops == &serial_ops );
213 if (reply)
215 reply->type = FILE_TYPE_CHAR;
216 reply->attr = 0;
217 reply->access_time = 0;
218 reply->write_time = 0;
219 reply->size_high = 0;
220 reply->size_low = 0;
221 reply->links = 0;
222 reply->index_high = 0;
223 reply->index_low = 0;
224 reply->serial = 0;
227 *flags = 0;
228 if(serial->attrib & FILE_FLAG_OVERLAPPED)
229 *flags |= FD_FLAG_OVERLAPPED;
230 else if(!((serial->readinterval == MAXDWORD) &&
231 (serial->readmult == 0) && (serial->readconst == 0)) )
232 *flags |= FD_FLAG_TIMEOUT;
234 return FD_TYPE_DEFAULT;
237 static void serial_poll_event(struct object *obj, int event)
239 struct serial *serial = (struct serial *)obj;
241 /* fprintf(stderr,"Poll event %02x\n",event); */
243 if(IS_READY(serial->read_q) && (POLLIN & event) )
244 async_notify(serial->read_q.head,STATUS_ALERTED);
246 if(IS_READY(serial->write_q) && (POLLOUT & event) )
247 async_notify(serial->write_q.head,STATUS_ALERTED);
249 if(IS_READY(serial->wait_q) && (POLLIN & event) )
250 async_notify(serial->wait_q.head,STATUS_ALERTED);
252 set_select_events(obj,obj->ops->get_poll_events(obj));
255 static void serial_queue_async(struct object *obj, void *ptr, unsigned int status, int type, int count)
257 struct serial *serial = (struct serial *)obj;
258 struct async_queue *q;
259 struct async *async;
260 int timeout;
262 assert(obj->ops == &serial_ops);
264 switch(type)
266 case ASYNC_TYPE_READ:
267 q = &serial->read_q;
268 timeout = serial->readconst + serial->readmult*count;
269 break;
270 case ASYNC_TYPE_WAIT:
271 q = &serial->wait_q;
272 timeout = 0;
273 break;
274 case ASYNC_TYPE_WRITE:
275 q = &serial->write_q;
276 timeout = serial->writeconst + serial->writemult*count;
277 break;
278 default:
279 set_error(STATUS_INVALID_PARAMETER);
280 return;
283 async = find_async ( q, current, ptr );
285 if ( status == STATUS_PENDING )
287 struct pollfd pfd;
289 if ( !async )
290 async = create_async ( obj, current, ptr );
291 if ( !async )
292 return;
294 async->status = STATUS_PENDING;
295 if(!async->q)
297 async_add_timeout(async,timeout);
298 async_insert(q, async);
301 /* Check if the new pending request can be served immediately */
302 pfd.fd = obj->fd;
303 pfd.events = serial_get_poll_events ( obj );
304 pfd.revents = 0;
305 poll ( &pfd, 1, 0 );
307 if ( pfd.revents )
308 /* serial_poll_event() calls set_select_events() */
309 serial_poll_event ( obj, pfd.revents );
310 else
311 set_select_events ( obj, pfd.events );
312 return;
314 else if ( async ) destroy_async ( async );
315 else set_error ( STATUS_INVALID_PARAMETER );
317 set_select_events ( obj, serial_get_poll_events ( obj ));
320 static int serial_flush( struct object *obj )
322 int ret;
323 struct serial *serial = (struct serial *)grab_object(obj);
324 assert( obj->ops == &serial_ops );
326 /* MSDN says: If hFile is a handle to a communications device,
327 * the function only flushes the transmit buffer.
329 ret = (tcflush( serial->obj.fd, TCOFLUSH ) != -1);
330 if (!ret) file_set_error();
331 release_object( serial );
332 return ret;
335 /* create a serial */
336 DECL_HANDLER(create_serial)
338 struct serial *serial;
340 reply->handle = 0;
341 if ((serial = create_serial( get_req_data(), get_req_data_size(), req->access, req->attributes )))
343 reply->handle = alloc_handle( current->process, serial, req->access, req->inherit );
344 release_object( serial );
348 DECL_HANDLER(get_serial_info)
350 struct serial *serial;
352 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
354 /* timeouts */
355 reply->readinterval = serial->readinterval;
356 reply->readconst = serial->readconst;
357 reply->readmult = serial->readmult;
358 reply->writeconst = serial->writeconst;
359 reply->writemult = serial->writemult;
361 /* event mask */
362 reply->eventmask = serial->eventmask;
364 /* comm port error status */
365 reply->commerror = serial->commerror;
367 release_object( serial );
371 DECL_HANDLER(set_serial_info)
373 struct serial *serial;
375 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
377 /* timeouts */
378 if(req->flags & SERIALINFO_SET_TIMEOUTS)
380 serial->readinterval = req->readinterval;
381 serial->readconst = req->readconst;
382 serial->readmult = req->readmult;
383 serial->writeconst = req->writeconst;
384 serial->writemult = req->writemult;
387 /* event mask */
388 if(req->flags & SERIALINFO_SET_MASK)
390 serial->eventmask = req->eventmask;
391 if(!serial->eventmask)
393 while(serial->wait_q.head)
395 async_notify(serial->wait_q.head, STATUS_SUCCESS);
396 destroy_async(serial->wait_q.head);
401 /* comm port error status */
402 if(req->flags & SERIALINFO_SET_ERROR)
404 serial->commerror = req->commerror;
407 release_object( serial );