app_chanisavail.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Check if Channel is Available
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. * James Golovich <james@gnuinter.net>
  10. *
  11. * This program is free software, distributed under the terms of
  12. * the GNU General Public License
  13. *
  14. */
  15. #include <asterisk/lock.h>
  16. #include <asterisk/file.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/channel.h>
  19. #include <asterisk/pbx.h>
  20. #include <asterisk/module.h>
  21. #include <asterisk/app.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <sys/ioctl.h>
  28. static char *tdesc = "Check if channel is available";
  29. static char *app = "ChanIsAvail";
  30. static char *synopsis = "Check if channel is available";
  31. static char *descrip =
  32. " ChanIsAvail(Technology/resource[&Technology2/resource2...]): \n"
  33. "Checks is any of the requested channels are available. If none\n"
  34. "of the requested channels are available the new priority will be\n"
  35. "n+101 (unless such a priority does not exist or on error, in which\n"
  36. "case ChanIsAvail will return -1).\n"
  37. "If any of the requested channels are available, the next priority will be n+1,\n"
  38. "the channel variable ${AVAILCHAN} will be set to the name of the available channel\n"
  39. "and the ChanIsAvail app will return 0.\n"
  40. "${AVAILORIGCHAN} is the canonical channel name that was used to create the channel.\n"
  41. "${AVAILSTATUS} is the status code for the channel.\n";
  42. STANDARD_LOCAL_USER;
  43. LOCAL_USER_DECL;
  44. static int chanavail_exec(struct ast_channel *chan, void *data)
  45. {
  46. int res=-1;
  47. struct localuser *u;
  48. char info[512], tmp[512], *peers, *tech, *number, *rest, *cur;
  49. struct ast_channel *tempchan;
  50. if (!data) {
  51. ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
  52. return -1;
  53. }
  54. LOCAL_USER_ADD(u);
  55. strncpy(info, (char *)data, sizeof(info)-1);
  56. peers = info;
  57. if (peers) {
  58. cur = peers;
  59. do {
  60. /* remember where to start next time */
  61. rest = strchr(cur, '&');
  62. if (rest) {
  63. *rest = 0;
  64. rest++;
  65. }
  66. tech = cur;
  67. number = strchr(tech, '/');
  68. if (!number) {
  69. ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
  70. return -1;
  71. }
  72. *number = '\0';
  73. number++;
  74. if ((tempchan = ast_request(tech, chan->nativeformats, number))) {
  75. pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
  76. /* Store the originally used channel too */
  77. snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
  78. pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
  79. ast_hangup(tempchan);
  80. tempchan = NULL;
  81. res = 1;
  82. break;
  83. }
  84. cur = rest;
  85. } while (cur);
  86. }
  87. if (res < 1) {
  88. pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
  89. pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
  90. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  91. chan->priority+=100;
  92. else
  93. return -1;
  94. }
  95. LOCAL_USER_REMOVE(u);
  96. return 0;
  97. }
  98. int unload_module(void)
  99. {
  100. STANDARD_HANGUP_LOCALUSERS;
  101. return ast_unregister_application(app);
  102. }
  103. int load_module(void)
  104. {
  105. return ast_register_application(app, chanavail_exec, synopsis, descrip);
  106. }
  107. char *description(void)
  108. {
  109. return tdesc;
  110. }
  111. int usecount(void)
  112. {
  113. int res;
  114. STANDARD_USECOUNT(res);
  115. return res;
  116. }
  117. char *key()
  118. {
  119. return ASTERISK_GPL_KEY;
  120. }