00001 /* 00002 * Asterisk 00003 * 00004 * Mark Spencer <markster@marko.net> 00005 * 00006 * Copyright(C) Mark Spencer 00007 * 00008 * Distributed under the terms of the GNU General Public License (GPL) Version 2 00009 * 00010 * I/O Managment (derived from Cheops-NG) 00011 * 00012 */ 00013 00014 #ifndef _IO_H 00015 #define _IO_H 00016 00017 #include <sys/poll.h> /* For POLL* constants */ 00018 00019 #if defined(__cplusplus) || defined(c_plusplus) 00020 extern "C" { 00021 #endif 00022 00023 /*! Input ready */ 00024 #define AST_IO_IN POLLIN 00025 /*! Output ready */ 00026 #define AST_IO_OUT POLLOUT 00027 /*! Priority input ready */ 00028 #define AST_IO_PRI POLLPRI 00029 00030 /* Implicitly polled for */ 00031 /*! Error condition (errno or getsockopt) */ 00032 #define AST_IO_ERR POLLERR 00033 /*! Hangup */ 00034 #define AST_IO_HUP POLLHUP 00035 /*! Invalid fd */ 00036 #define AST_IO_NVAL POLLNVAL 00037 00038 /* 00039 * An Asterisk IO callback takes its id, a file descriptor, list of events, and 00040 * callback data as arguments and returns 0 if it should not be 00041 * run again, or non-zero if it should be run again. 00042 */ 00043 00044 struct io_context; 00045 00046 //! Creates a context 00047 /*! 00048 * Create a context for I/O operations 00049 * Basically mallocs an IO structure and sets up some default values. 00050 * Returns an allocated io_context structure 00051 */ 00052 extern struct io_context *io_context_create(void); 00053 00054 //! Destroys a context 00055 /* 00056 * \param ioc structure to destroy 00057 * Destroy a context for I/O operations 00058 * Frees all memory associated with the given io_context structure along with the structure itself 00059 */ 00060 extern void io_context_destroy(struct io_context *ioc); 00061 00062 typedef int (*ast_io_cb)(int *id, int fd, short events, void *cbdata); 00063 #define AST_IO_CB(a) ((ast_io_cb)(a)) 00064 00065 //! Adds an IO context 00066 /*! 00067 * \param ioc which context to use 00068 * \param fd which fd to monitor 00069 * \param callback callback function to run 00070 * \param events event mask of events to wait for 00071 * \param data data to pass to the callback 00072 * Watch for any of revents activites on fd, calling callback with data as 00073 * callback data. Returns a pointer to ID of the IO event, or NULL on failure. 00074 */ 00075 extern int *ast_io_add(struct io_context *ioc, int fd, ast_io_cb callback, short events, void *data); 00076 00077 //! Changes an IO handler 00078 /*! 00079 * \param ioc which context to use 00080 * \param id 00081 * \param fd the fd you wish it to contain now 00082 * \param callback new callback function 00083 * \param events event mask to wait for 00084 * \param data data to pass to the callback function 00085 * Change an i/o handler, updating fd if > -1, callback if non-null, and revents 00086 * if >-1, and data if non-null. Returns a pointero to the ID of the IO event, 00087 * or NULL on failure. 00088 */ 00089 extern int *ast_io_change(struct io_context *ioc, int *id, int fd, ast_io_cb callback, short events, void *data); 00090 00091 //! Removes an IO context 00092 /*! 00093 * \param ioc which io_context to remove it from 00094 * \param id which ID to remove 00095 * Remove an I/O id from consideration Returns 0 on success or -1 on failure. 00096 */ 00097 extern int ast_io_remove(struct io_context *ioc, int *id); 00098 00099 //! Waits for IO 00100 /*! 00101 * \param ioc which context to act upon 00102 * \param howlong how many milliseconds to wait 00103 * Wait for I/O to happen, returning after 00104 * howlong milliseconds, and after processing 00105 * any necessary I/O. Returns the number of 00106 * I/O events which took place. 00107 */ 00108 extern int ast_io_wait(struct io_context *ioc, int howlong); 00109 00110 //! Dumps the IO array 00111 /* 00112 * Debugging: Dump everything in the I/O array 00113 */ 00114 extern void ast_io_dump(struct io_context *ioc); 00115 00116 //! Set fd into non-echoing mode (if fd is a tty) 00117 00118 extern int ast_hide_password(int fd); 00119 00120 //! Restores TTY mode 00121 /* 00122 * Call with result from previous ast_hide_password 00123 */ 00124 extern int ast_restore_tty(int fd, int oldstatus); 00125 00126 extern int ast_get_termcols(int fd); 00127 00128 #if defined(__cplusplus) || defined(c_plusplus) 00129 } 00130 #endif 00131 00132 00133 #endif