app_getcpeid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Execute arbitrary system commands
  5. *
  6. * Copyright (C) 1999-2005, Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/adsi.h>
  20. #include <asterisk/options.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. static char *tdesc = "Get ADSI CPE ID";
  26. static char *app = "GetCPEID";
  27. static char *synopsis = "Get ADSI CPE ID";
  28. static char *descrip =
  29. " GetCPEID: Obtains and displays ADSI CPE ID and other information in order\n"
  30. "to properly setup zapata.conf for on-hook operations.\n"
  31. "Returns -1 on hangup only.\n";
  32. STANDARD_LOCAL_USER;
  33. LOCAL_USER_DECL;
  34. static int cpeid_setstatus(struct ast_channel *chan, char *stuff[], int voice)
  35. {
  36. int justify[5] = { ADSI_JUST_CENT, ADSI_JUST_LEFT, ADSI_JUST_LEFT, ADSI_JUST_LEFT };
  37. char *tmp[5];
  38. int x;
  39. for (x=0;x<4;x++)
  40. tmp[x] = stuff[x];
  41. tmp[4] = NULL;
  42. return adsi_print(chan, tmp, justify, voice);
  43. }
  44. static int cpeid_exec(struct ast_channel *chan, void *idata)
  45. {
  46. int res=0;
  47. struct localuser *u;
  48. unsigned char cpeid[4];
  49. int gotgeometry = 0;
  50. int gotcpeid = 0;
  51. int width, height, buttons;
  52. char data[4][80];
  53. char *stuff[4];
  54. LOCAL_USER_ADD(u);
  55. stuff[0] = data[0];
  56. stuff[1] = data[1];
  57. stuff[2] = data[2];
  58. stuff[3] = data[3];
  59. memset(data, 0, sizeof(data));
  60. strncpy(stuff[0], "** CPE Info **", sizeof(data[0]) - 1);
  61. strncpy(stuff[1], "Identifying CPE...", sizeof(data[1]) - 1);
  62. strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1);
  63. res = adsi_load_session(chan, NULL, 0, 1);
  64. if (res > 0) {
  65. cpeid_setstatus(chan, stuff, 0);
  66. res = adsi_get_cpeid(chan, cpeid, 0);
  67. if (res > 0) {
  68. gotcpeid = 1;
  69. if (option_verbose > 2)
  70. ast_verbose(VERBOSE_PREFIX_3 "Got CPEID of '%02x:%02x:%02x:%02x' on '%s'\n", cpeid[0], cpeid[1], cpeid[2], cpeid[3], chan->name);
  71. }
  72. if (res > -1) {
  73. strncpy(stuff[1], "Measuring CPE...", sizeof(data[1]) - 1);
  74. strncpy(stuff[2], "Please wait...", sizeof(data[2]) - 1);
  75. cpeid_setstatus(chan, stuff, 0);
  76. res = adsi_get_cpeinfo(chan, &width, &height, &buttons, 0);
  77. if (res > -1) {
  78. if (option_verbose > 2)
  79. ast_verbose(VERBOSE_PREFIX_3 "CPE has %d lines, %d columns, and %d buttons on '%s'\n", height, width, buttons, chan->name);
  80. gotgeometry = 1;
  81. }
  82. }
  83. if (res > -1) {
  84. if (gotcpeid)
  85. snprintf(stuff[1], sizeof(data[1]), "CPEID: %02x:%02x:%02x:%02x", cpeid[0], cpeid[1], cpeid[2], cpeid[3]);
  86. else
  87. strncpy(stuff[1], "CPEID Unknown", sizeof(data[1]) - 1);
  88. if (gotgeometry)
  89. snprintf(stuff[2], sizeof(data[2]), "Geom: %dx%d, %d buttons", width, height, buttons);
  90. else
  91. strncpy(stuff[2], "Geometry unknown", sizeof(data[2]) - 1);
  92. strncpy(stuff[3], "Press # to exit", sizeof(data[3]) - 1);
  93. cpeid_setstatus(chan, stuff, 1);
  94. for(;;) {
  95. res = ast_waitfordigit(chan, 1000);
  96. if (res < 0)
  97. break;
  98. if (res == '#') {
  99. res = 0;
  100. break;
  101. }
  102. }
  103. adsi_unload_session(chan);
  104. }
  105. }
  106. LOCAL_USER_REMOVE(u);
  107. return res;
  108. }
  109. int unload_module(void)
  110. {
  111. STANDARD_HANGUP_LOCALUSERS;
  112. return ast_unregister_application(app);
  113. }
  114. int load_module(void)
  115. {
  116. return ast_register_application(app, cpeid_exec, synopsis, descrip);
  117. }
  118. char *description(void)
  119. {
  120. return tdesc;
  121. }
  122. int usecount(void)
  123. {
  124. int res;
  125. STANDARD_USECOUNT(res);
  126. return res;
  127. }
  128. char *key()
  129. {
  130. return ASTERISK_GPL_KEY;
  131. }