femon.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* femon -- monitor frontend status
  2. *
  3. * Copyright (C) 2003 convergence GmbH
  4. * Johannes Stezenbach <js@convergence.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/poll.h>
  29. #include <fcntl.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32. #include <stdint.h>
  33. #include <sys/time.h>
  34. #include <libdvbapi/dvbfe.h>
  35. #define FE_STATUS_PARAMS (DVBFE_INFO_LOCKSTATUS|DVBFE_INFO_SIGNAL_STRENGTH|DVBFE_INFO_BER|DVBFE_INFO_SNR|DVBFE_INFO_UNCORRECTED_BLOCKS)
  36. static char *usage_str =
  37. "\nusage: femon [options]\n"
  38. " -H : human readable output\n"
  39. " -A : Acoustical mode. A sound indicates the signal quality.\n"
  40. " -r : If 'Acoustical mode' is active it tells the application\n"
  41. " is called remotely via ssh. The sound is heard on the 'real'\n"
  42. " machine but. The user has to be root.\n"
  43. " -a number : use given adapter (default 0)\n"
  44. " -f number : use given frontend (default 0)\n"
  45. " -c number : samples to take (default 0 = infinite)\n\n";
  46. int sleep_time=1000000;
  47. int acoustical_mode=0;
  48. int remote=0;
  49. static void usage(void)
  50. {
  51. fprintf(stderr, "%s", usage_str);
  52. exit(1);
  53. }
  54. static
  55. int check_frontend (struct dvbfe_handle *fe, int human_readable, unsigned int count)
  56. {
  57. struct dvbfe_info fe_info;
  58. unsigned int samples = 0;
  59. FILE *ttyFile=NULL;
  60. // We dont write the "beep"-codes to stdout but to /dev/tty1.
  61. // This is neccessary for Thin-Client-Systems or Streaming-Boxes
  62. // where the computer does not have a monitor and femon is called via ssh.
  63. if(acoustical_mode)
  64. {
  65. if(remote)
  66. {
  67. ttyFile=fopen("/dev/tty1","w");
  68. if(!ttyFile)
  69. {
  70. fprintf(stderr, "Could not open /dev/tty1. No access rights?\n");
  71. exit(-1);
  72. }
  73. }
  74. else
  75. {
  76. ttyFile=stdout;
  77. }
  78. }
  79. do {
  80. if (dvbfe_get_info(fe, FE_STATUS_PARAMS, &fe_info, DVBFE_INFO_QUERYTYPE_IMMEDIATE, 0) != FE_STATUS_PARAMS) {
  81. fprintf(stderr, "Problem retrieving frontend information: %m\n");
  82. }
  83. if (human_readable) {
  84. printf ("status %c%c%c%c%c | signal %3u%% | snr %3u%% | ber %d | unc %d | ",
  85. fe_info.signal ? 'S' : ' ',
  86. fe_info.carrier ? 'C' : ' ',
  87. fe_info.viterbi ? 'V' : ' ',
  88. fe_info.sync ? 'Y' : ' ',
  89. fe_info.lock ? 'L' : ' ',
  90. (fe_info.signal_strength * 100) / 0xffff,
  91. (fe_info.snr * 100) / 0xffff,
  92. fe_info.ber,
  93. fe_info.ucblocks);
  94. } else {
  95. printf ("status %c%c%c%c%c | signal %04x | snr %04x | ber %08x | unc %08x | ",
  96. fe_info.signal ? 'S' : ' ',
  97. fe_info.carrier ? 'C' : ' ',
  98. fe_info.viterbi ? 'V' : ' ',
  99. fe_info.sync ? 'Y' : ' ',
  100. fe_info.lock ? 'L' : ' ',
  101. fe_info.signal_strength,
  102. fe_info.snr,
  103. fe_info.ber,
  104. fe_info.ucblocks);
  105. }
  106. if (fe_info.lock)
  107. printf("FE_HAS_LOCK");
  108. // create beep if acoustical_mode enabled
  109. if(acoustical_mode)
  110. {
  111. int signal=(fe_info.signal_strength * 100) / 0xffff;
  112. fprintf( ttyFile, "\033[10;%d]\a", 500+(signal*2));
  113. // printf("Variable : %d\n", signal);
  114. fflush(ttyFile);
  115. }
  116. printf("\n");
  117. fflush(stdout);
  118. usleep(sleep_time);
  119. samples++;
  120. } while ((!count) || (count-samples));
  121. if(ttyFile)
  122. fclose(ttyFile);
  123. return 0;
  124. }
  125. static
  126. int do_mon(unsigned int adapter, unsigned int frontend, int human_readable, unsigned int count)
  127. {
  128. int result;
  129. struct dvbfe_handle *fe;
  130. struct dvbfe_info fe_info;
  131. char *fe_type = "UNKNOWN";
  132. fe = dvbfe_open(adapter, frontend, 1);
  133. if (fe == NULL) {
  134. perror("opening frontend failed");
  135. return 0;
  136. }
  137. dvbfe_get_info(fe, 0, &fe_info, DVBFE_INFO_QUERYTYPE_IMMEDIATE, 0);
  138. switch(fe_info.type) {
  139. case DVBFE_TYPE_DVBS:
  140. fe_type = "DVBS";
  141. break;
  142. case DVBFE_TYPE_DVBC:
  143. fe_type = "DVBC";
  144. break;
  145. case DVBFE_TYPE_DVBT:
  146. fe_type = "DVBT";
  147. break;
  148. case DVBFE_TYPE_ATSC:
  149. fe_type = "ATSC";
  150. break;
  151. }
  152. printf("FE: %s (%s)\n", fe_info.name, fe_type);
  153. result = check_frontend (fe, human_readable, count);
  154. dvbfe_close(fe);
  155. return result;
  156. }
  157. int main(int argc, char *argv[])
  158. {
  159. unsigned int adapter = 0, frontend = 0, count = 0;
  160. int human_readable = 0;
  161. int opt;
  162. while ((opt = getopt(argc, argv, "rAHa:f:c:")) != -1) {
  163. switch (opt)
  164. {
  165. default:
  166. usage();
  167. break;
  168. case 'a':
  169. adapter = strtoul(optarg, NULL, 0);
  170. break;
  171. case 'c':
  172. count = strtoul(optarg, NULL, 0);
  173. break;
  174. case 'f':
  175. frontend = strtoul(optarg, NULL, 0);
  176. break;
  177. case 'H':
  178. human_readable = 1;
  179. break;
  180. case 'A':
  181. // Acoustical mode: we have to reduce the delay between
  182. // checks in order to hear nice sound
  183. sleep_time=5000;
  184. acoustical_mode=1;
  185. break;
  186. case 'r':
  187. remote=1;
  188. break;
  189. }
  190. }
  191. do_mon(adapter, frontend, human_readable, count);
  192. return 0;
  193. }