Added mapping to/from unicode for WM_CHAR, WM_DEADCHAR, WM_SYSCHAR,
[wine.git] / server / serial.c
blobc549becd41b8ffd437ca9929db1b8fa0238a67cc
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/stat.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include <unistd.h>
28 #include <utime.h>
29 #include <termios.h>
30 #include <sys/ioctl.h>
32 #include "winerror.h"
33 #include "winbase.h"
35 #include "handle.h"
36 #include "thread.h"
37 #include "request.h"
39 static void serial_dump( struct object *obj, int verbose );
40 static int serial_get_read_fd( struct object *obj );
41 static int serial_get_write_fd( struct object *obj );
42 static int serial_get_info( struct object *obj, struct get_file_info_request *req );
43 static int serial_get_poll_events( struct object *obj );
45 struct serial
47 struct object obj;
48 unsigned int access;
50 /* timeout values */
51 unsigned int readinterval;
52 unsigned int readconst;
53 unsigned int readmult;
54 unsigned int writeconst;
55 unsigned int writemult;
57 unsigned int eventmask;
58 unsigned int commerror;
60 struct termios original;
62 /* FIXME: add dcb, comm status, handler module, sharing */
65 static const struct object_ops serial_ops =
67 sizeof(struct serial), /* size */
68 serial_dump, /* dump */
69 default_poll_add_queue, /* add_queue */
70 default_poll_remove_queue, /* remove_queue */
71 default_poll_signaled, /* signaled */
72 no_satisfied, /* satisfied */
73 serial_get_poll_events, /* get_poll_events */
74 default_poll_event, /* poll_event */
75 serial_get_read_fd, /* get_read_fd */
76 serial_get_write_fd, /* get_write_fd */
77 no_flush, /* flush */
78 serial_get_info, /* get_file_info */
79 no_destroy /* destroy */
82 /* SERIAL PORT functions */
84 static struct serial *create_serial( const char *nameptr, size_t len, unsigned int access )
86 struct serial *serial;
87 struct termios tios;
88 int fd, flags = 0;
89 char *name;
91 if (!(name = mem_alloc( len + 1 ))) return NULL;
92 memcpy( name, nameptr, len );
93 name[len] = 0;
95 switch(access & (GENERIC_READ | GENERIC_WRITE))
97 case GENERIC_READ: flags |= O_RDONLY; break;
98 case GENERIC_WRITE: flags |= O_WRONLY; break;
99 case GENERIC_READ|GENERIC_WRITE: flags |= O_RDWR; break;
100 default: break;
103 fd = open( name, flags );
104 free( name );
105 if (fd < 0)
107 file_set_error();
108 return NULL;
111 /* check its really a serial port */
112 if (tcgetattr(fd,&tios))
114 file_set_error();
115 close( fd );
116 return NULL;
119 if ((serial = alloc_object( &serial_ops, fd )))
121 serial->access = access;
122 serial->readinterval = 0;
123 serial->readmult = 0;
124 serial->readconst = 0;
125 serial->writemult = 0;
126 serial->writeconst = 0;
127 serial->eventmask = 0;
128 serial->commerror = 0;
130 return serial;
133 static void serial_dump( struct object *obj, int verbose )
135 struct serial *serial = (struct serial *)obj;
136 assert( obj->ops == &serial_ops );
137 fprintf( stderr, "Port fd=%d mask=%x\n", serial->obj.fd, serial->eventmask );
140 struct serial *get_serial_obj( struct process *process, int handle, unsigned int access )
142 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
145 static int serial_get_poll_events( struct object *obj )
147 struct serial *serial = (struct serial *)obj;
148 int events = 0;
149 assert( obj->ops == &serial_ops );
150 if (serial->access & GENERIC_READ) events |= POLLIN;
151 if (serial->access & GENERIC_WRITE) events |= POLLOUT;
152 return events;
155 static int serial_get_read_fd( struct object *obj )
157 struct serial *serial = (struct serial *)obj;
158 assert( obj->ops == &serial_ops );
159 return dup( serial->obj.fd );
162 static int serial_get_write_fd( struct object *obj )
164 struct serial *serial = (struct serial *)obj;
165 assert( obj->ops == &serial_ops );
166 return dup( serial->obj.fd );
169 static int serial_get_info( struct object *obj, struct get_file_info_request *req )
171 assert( obj->ops == &serial_ops );
172 req->type = FILE_TYPE_CHAR;
173 req->attr = 0;
174 req->access_time = 0;
175 req->write_time = 0;
176 req->size_high = 0;
177 req->size_low = 0;
178 req->links = 0;
179 req->index_high = 0;
180 req->index_low = 0;
181 req->serial = 0;
182 return 1;
185 /* these functions are for interaction with asynchronous i/o objects */
186 int serial_async_setup(struct object *obj, struct async *ov)
188 struct serial *serial = (struct serial *)obj;
189 int timeout;
191 if(obj->ops != &serial_ops)
192 return 0;
194 switch(async_type(ov))
196 case ASYNC_TYPE_READ:
197 timeout = serial->readconst + serial->readmult*async_count(ov);
198 async_add_timeout(ov, timeout);
199 async_set_eventmask(ov,EV_RXCHAR);
200 break;
201 case ASYNC_TYPE_WRITE:
202 timeout = serial->writeconst + serial->writemult*async_count(ov);
203 async_add_timeout(ov, timeout);
204 async_set_eventmask(ov,EV_TXEMPTY);
205 break;
206 case ASYNC_TYPE_WAIT:
207 async_set_eventmask(ov,serial->eventmask);
208 break;
211 return 1;
214 int serial_async_get_poll_events( struct async *ov )
216 int events=0,mask;
218 switch(async_type(ov))
220 case ASYNC_TYPE_READ:
221 events |= POLLIN;
222 break;
223 case ASYNC_TYPE_WRITE:
224 events |= POLLOUT;
225 break;
226 case ASYNC_TYPE_WAIT:
228 * FIXME: here is the spot to implement other WaitCommEvent flags
230 mask = async_get_eventmask(ov);
231 if(mask&EV_RXCHAR)
232 events |= POLLIN;
233 if(mask&EV_TXEMPTY)
234 events |= POLLOUT;
235 break;
237 return events;
240 /* receive a select event, and output a windows event */
241 int serial_async_poll_event(struct object *obj, int event)
243 int r=0;
246 * FIXME: here is the spot to implement other WaitCommEvent flags
248 if(event & POLLIN)
249 r |= EV_RXCHAR;
250 if(event & POLLOUT)
251 r |= EV_TXEMPTY;
253 return r;
256 /* create a serial */
257 DECL_HANDLER(create_serial)
259 struct serial *serial;
261 req->handle = -1;
262 if ((serial = create_serial( get_req_data(req), get_req_data_size(req), req->access )))
264 req->handle = alloc_handle( current->process, serial, req->access, req->inherit );
265 release_object( serial );
269 DECL_HANDLER(get_serial_info)
271 struct serial *serial;
273 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
275 /* timeouts */
276 req->readinterval = serial->readinterval;
277 req->readconst = serial->readconst;
278 req->readmult = serial->readmult;
279 req->writeconst = serial->writeconst;
280 req->writemult = serial->writemult;
282 /* event mask */
283 req->eventmask = serial->eventmask;
285 /* comm port error status */
286 req->commerror = serial->commerror;
288 release_object( serial );
292 DECL_HANDLER(set_serial_info)
294 struct serial *serial;
296 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
298 /* timeouts */
299 if(req->flags & SERIALINFO_SET_TIMEOUTS)
301 serial->readinterval = req->readinterval;
302 serial->readconst = req->readconst;
303 serial->readmult = req->readmult;
304 serial->writeconst = req->writeconst;
305 serial->writemult = req->writemult;
308 /* event mask */
309 if(req->flags & SERIALINFO_SET_MASK)
311 serial->eventmask = req->eventmask;
314 /* comm port error status */
315 if(req->flags & SERIALINFO_SET_ERROR)
317 serial->commerror = req->commerror;
320 release_object( serial );