Made all 16<->32 HWND conversions use explicit functions instead of
[wine/multimedia.git] / server / serial.c
blob614be004f2b92e35da2d02c3d5f2cf564417720b
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000 Mike McCormack
7 * TODO:
8 * Add async read, write and WaitCommEvent handling.
12 #include "config.h"
14 #include <assert.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #ifdef HAVE_SYS_ERRNO_H
21 #include <sys/errno.h>
22 #endif
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <utime.h>
28 #include <termios.h>
29 #include <sys/ioctl.h>
31 #include "winerror.h"
32 #include "winbase.h"
34 #include "handle.h"
35 #include "thread.h"
36 #include "request.h"
38 static void serial_dump( struct object *obj, int verbose );
39 static int serial_get_fd( struct object *obj );
40 static int serial_get_info( struct object *obj, struct get_file_info_request *req );
41 static int serial_get_poll_events( struct object *obj );
43 struct serial
45 struct object obj;
46 unsigned int access;
48 /* timeout values */
49 unsigned int readinterval;
50 unsigned int readconst;
51 unsigned int readmult;
52 unsigned int writeconst;
53 unsigned int writemult;
55 unsigned int eventmask;
56 unsigned int commerror;
58 struct termios original;
60 /* FIXME: add dcb, comm status, handler module, sharing */
63 static const struct object_ops serial_ops =
65 sizeof(struct serial), /* size */
66 serial_dump, /* dump */
67 default_poll_add_queue, /* add_queue */
68 default_poll_remove_queue, /* remove_queue */
69 default_poll_signaled, /* signaled */
70 no_satisfied, /* satisfied */
71 serial_get_poll_events, /* get_poll_events */
72 default_poll_event, /* poll_event */
73 serial_get_fd, /* get_fd */
74 no_flush, /* flush */
75 serial_get_info, /* get_file_info */
76 no_destroy /* destroy */
79 /* SERIAL PORT functions */
81 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access )
83 struct serial *serial;
84 struct termios tios;
85 int fd, flags = 0;
86 char *name;
88 if (!(name = mem_alloc( len + 1 ))) return NULL;
89 memcpy( name, nameptr, len );
90 name[len] = 0;
92 switch(access & (GENERIC_READ | GENERIC_WRITE))
94 case GENERIC_READ: flags |= O_RDONLY; break;
95 case GENERIC_WRITE: flags |= O_WRONLY; break;
96 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
97 default: break;
100 flags |= O_NONBLOCK;
102 fd = open( name, flags );
103 free( name );
104 if (fd < 0)
106 file_set_error();
107 return NULL;
110 /* check its really a serial port */
111 if (tcgetattr(fd,&tios))
113 file_set_error();
114 close( fd );
115 return NULL;
118 if ((serial = alloc_object( &serial_ops, fd )))
120 serial->access = access;
121 serial->readinterval = 0;
122 serial->readmult = 0;
123 serial->readconst = 0;
124 serial->writemult = 0;
125 serial->writeconst = 0;
126 serial->eventmask = 0;
127 serial->commerror = 0;
129 return serial;
132 static void serial_dump( struct object *obj, int verbose )
134 struct serial *serial = (struct serial *)obj;
135 assert( obj->ops == &serial_ops );
136 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
139 struct serial *get_serial_obj( struct process *process, handle_t handle, unsigned int access )
141 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
144 static int serial_get_poll_events( struct object *obj )
146 struct serial *serial = (struct serial *)obj;
147 int events = 0;
148 assert( obj->ops == &serial_ops );
149 if (serial->access & GENERIC_READ) events |= POLLIN;
150 if (serial->access & GENERIC_WRITE) events |= POLLOUT;
151 return events;
154 static int serial_get_fd( struct object *obj )
156 struct serial *serial = (struct serial *)obj;
157 assert( obj->ops == &serial_ops );
158 return serial->obj.fd;
161 static int serial_get_info( struct object *obj, struct get_file_info_request *req )
163 assert( obj->ops == &serial_ops );
164 req->type = FILE_TYPE_CHAR;
165 req->attr = 0;
166 req->access_time = 0;
167 req->write_time = 0;
168 req->size_high = 0;
169 req->size_low = 0;
170 req->links = 0;
171 req->index_high = 0;
172 req->index_low = 0;
173 req->serial = 0;
174 return 1;
177 /* these function calculates the timeout for an async operation
178 on a serial port */
179 int get_serial_async_timeout(struct object *obj, int type, int count)
181 struct serial *serial = (struct serial *)obj;
183 if(obj->ops != &serial_ops)
184 return 0;
186 switch(type)
188 case ASYNC_TYPE_READ:
189 return serial->readconst + serial->readmult*count;
190 case ASYNC_TYPE_WRITE:
191 return serial->writeconst + serial->writemult*count;
193 return 0;
197 /* create a serial */
198 DECL_HANDLER(create_serial)
200 struct serial *serial;
202 req->handle = 0;
203 if ((serial = create_serial( get_req_data(req), get_req_data_size(req), req->access )))
205 req->handle = alloc_handle( current->process, serial, req->access, req->inherit );
206 release_object( serial );
210 DECL_HANDLER(get_serial_info)
212 struct serial *serial;
214 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
216 /* timeouts */
217 req->readinterval = serial->readinterval;
218 req->readconst = serial->readconst;
219 req->readmult = serial->readmult;
220 req->writeconst = serial->writeconst;
221 req->writemult = serial->writemult;
223 /* event mask */
224 req->eventmask = serial->eventmask;
226 /* comm port error status */
227 req->commerror = serial->commerror;
229 release_object( serial );
233 DECL_HANDLER(set_serial_info)
235 struct serial *serial;
237 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
239 /* timeouts */
240 if(req->flags & SERIALINFO_SET_TIMEOUTS)
242 serial->readinterval = req->readinterval;
243 serial->readconst = req->readconst;
244 serial->readmult = req->readmult;
245 serial->writeconst = req->writeconst;
246 serial->writemult = req->writemult;
249 /* event mask */
250 if(req->flags & SERIALINFO_SET_MASK)
252 serial->eventmask = req->eventmask;
255 /* comm port error status */
256 if(req->flags & SERIALINFO_SET_ERROR)
258 serial->commerror = req->commerror;
261 release_object( serial );