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