diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h index 3641a1be2fd1fcdb769b49c59aabb9d4b14fbc04..67e1d4c7c4fe873c341ed4851d2abef5961622e8 100644 --- a/include/asterisk/channel.h +++ b/include/asterisk/channel.h @@ -1896,7 +1896,7 @@ char *ast_recvtext(struct ast_channel *chan, int timeout); /*! * \brief Waits for a digit * \param c channel to wait for a digit on - * \param ms how many milliseconds to wait + * \param ms how many milliseconds to wait (<0 for indefinite). * \return Returns <0 on error, 0 on no entry, and the digit on success. */ int ast_waitfordigit(struct ast_channel *c, int ms); @@ -1905,7 +1905,7 @@ int ast_waitfordigit(struct ast_channel *c, int ms); * \brief Wait for a digit * Same as ast_waitfordigit() with audio fd for outputting read audio and ctrlfd to monitor for reading. * \param c channel to wait for a digit on - * \param ms how many milliseconds to wait + * \param ms how many milliseconds to wait (<0 for indefinite). * \param audiofd audio file descriptor to write to if audio frames are received * \param ctrlfd control file descriptor to monitor for reading * \return Returns 1 if ctrlfd becomes available diff --git a/main/channel.c b/main/channel.c index d62f34239230a64f54710471aa131ee04e0b4df4..b2548459877e8e20b2b9eb60f14978ee5ed3d592 100644 --- a/main/channel.c +++ b/main/channel.c @@ -3530,7 +3530,6 @@ int ast_waitfor(struct ast_channel *c, int ms) return ms; } -/* XXX never to be called with ms = -1 */ int ast_waitfordigit(struct ast_channel *c, int ms) { return ast_waitfordigit_full(c, ms, -1, -1); @@ -3579,8 +3578,10 @@ int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const v return res; } -int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd) +int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd) { + struct timeval start = ast_tvnow(); + /* Stop if we're a zombie or need a soft hangup */ if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c)) return -1; @@ -3588,13 +3589,27 @@ int ast_waitfordigit_full(struct ast_channel *c, int ms, int audiofd, int cmdfd) /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */ ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY); - /* Wait for a digit, no more than ms milliseconds total. */ - - while (ms) { + /* Wait for a digit, no more than timeout_ms milliseconds total. + * Or, wait indefinitely if timeout_ms is <0. + */ + while (ast_tvdiff_ms(ast_tvnow(), start) < timeout_ms || timeout_ms < 0) { struct ast_channel *rchan; int outfd=-1; + int ms; + + if (timeout_ms < 0) { + ms = timeout_ms; + } else { + ms = timeout_ms - ast_tvdiff_ms(ast_tvnow(), start); + if (ms < 0) { + ms = 0; + } + } errno = 0; + /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited, + * it is unhelpful if it waited less than a millisecond. + */ rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms); if (!rchan && outfd < 0 && ms) {