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