lock_s.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * lock_s - Ultra simple DVB-S lock test application
  3. * A minimal lock test application derived from szap.c
  4. * for testing purposes
  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 <sys/param.h>
  30. #include <fcntl.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. #include <ctype.h>
  34. #include <stdint.h>
  35. #include <sys/time.h>
  36. #include <linux/dvb/frontend.h>
  37. #define FEDEVICE "/dev/dvb/adapter%d/frontend%d"
  38. static char *usage_str = "\n lock_s ver:0.0.1: Simple utility to test DVB-S signal lock.\n"
  39. " \n"
  40. " usage: lock_s [[PARAMS] [OPTIONS]]\n"
  41. " dvbstune [params] tune to user provided DVB-S parameters\n"
  42. " -a number : use given adapter (default 0)\n"
  43. " -f number : use given frontend (default 0)\n"
  44. " -p params : parameters to be used for tuning\n"
  45. " frequency (Mhz) Polarization (v/h) Symbol rate (MSPS)\n"
  46. " \n"
  47. " -s pos : DiSEqC switch position\n"
  48. " -H : human readable output\n"
  49. " -l lnb-type (DVB-S Only) (use -l help to print types) or \n"
  50. " -l low[,high[,switch]] in Mhz\n";
  51. static char *univ_desc[] = {
  52. "Europe",
  53. "10800 to 11800 MHz and 11600 to 12700 Mhz",
  54. "Dual LO, loband 9750, hiband 10600 MHz",
  55. (char *)NULL
  56. };
  57. static char *dbs_desc[] = {
  58. "Expressvu, North America",
  59. "12200 to 12700 MHz",
  60. "Single LO, 11250 MHz",
  61. (char *)NULL
  62. };
  63. static char *standard_desc[] = {
  64. "10945 to 11450 Mhz",
  65. "Single LO, 10000 Mhz",
  66. (char *)NULL
  67. };
  68. static char *enhan_desc[] = {
  69. "Astra",
  70. "10700 to 11700 MHz",
  71. "Single LO, 9750 MHz",
  72. (char *)NULL
  73. };
  74. static char *cband_desc[] = {
  75. "Big Dish",
  76. "3700 to 4200 MHz",
  77. "Single LO, 5150 Mhz",
  78. (char *)NULL
  79. };
  80. enum sec_bands {
  81. SEC_LO_BAND = 0,
  82. SEC_HI_BAND = 1,
  83. };
  84. struct sec_params {
  85. unsigned int freq;
  86. enum fe_sec_voltage voltage;
  87. unsigned int srate;
  88. unsigned int freq_if;
  89. int pos; /* DiSEqC sw. pos */
  90. fe_sec_tone_mode_t tone;
  91. fe_sec_mini_cmd_t burst;
  92. };
  93. struct lnb_types_st {
  94. char *name;
  95. char **desc;
  96. unsigned long low_val;
  97. unsigned long high_val; /* zero indicates no hiband */
  98. unsigned long switch_val; /* zero indicates no hiband */
  99. } lnbs[] = {
  100. {"UNIVERSAL", univ_desc, 9750, 10600, 11700 },
  101. {"DBS", dbs_desc, 11250, 0, 0 },
  102. {"STANDARD", standard_desc, 10000, 0, 0 },
  103. {"ENHANCED", enhan_desc, 9750, 0, 0 },
  104. {"C-BAND", cband_desc, 5150, 0, 0 },
  105. };
  106. struct diseqc_cmd {
  107. struct dvb_diseqc_master_cmd cmd;
  108. uint32_t wait;
  109. };
  110. int lnb_parse(char *str, struct lnb_types_st *lnbp)
  111. {
  112. int i;
  113. char *cp, *np;
  114. memset(lnbp, 0, sizeof (*lnbp));
  115. cp = str;
  116. while (*cp && isspace(*cp))
  117. cp++;
  118. if (isalpha(*cp)) {
  119. for (i = 0; i < (int)(sizeof(lnbs) / sizeof(lnbs[0])); i++) {
  120. if (!strcasecmp(lnbs[i].name, cp)) {
  121. *lnbp = lnbs[i];
  122. return 1;
  123. }
  124. }
  125. return -1;
  126. }
  127. if (*cp == '\0' || !isdigit(*cp))
  128. return -1;
  129. lnbp->low_val = strtoul(cp, &np, 0);
  130. if (lnbp->low_val == 0)
  131. return -1;
  132. cp = np;
  133. while(*cp && (isspace(*cp) || *cp == ','))
  134. cp++;
  135. if (*cp == '\0')
  136. return 1;
  137. if (!isdigit(*cp))
  138. return -1;
  139. lnbp->high_val = strtoul(cp, &np, 0);
  140. cp = np;
  141. while(*cp && (isspace(*cp) || *cp == ','))
  142. cp++;
  143. if (*cp == '\0')
  144. return 1;
  145. if (!isdigit(*cp))
  146. return -1;
  147. lnbp->switch_val = strtoul(cp, NULL, 0);
  148. return 1;
  149. }
  150. int params_decode(char *str, char **argv, struct sec_params *params)
  151. {
  152. unsigned int freq;
  153. char *pol;
  154. char *cp, *np;
  155. cp = str;
  156. while (*cp && isspace(*cp))
  157. cp++;
  158. /* get frequency */
  159. if (*cp == '\0' || !isdigit(*cp))
  160. return -1;
  161. freq = strtoul(cp, &np, 0);
  162. params->freq = freq;
  163. if (freq == 0)
  164. return -1;
  165. /* polarization v=13v:0, h=18v:0 */
  166. pol = argv[optind]; /* v/h */
  167. (!strncmp(pol, "v", 1)) ? (params->voltage = 0) : (params->voltage = 1);
  168. params->srate = strtoul(argv[optind + 1], NULL, 0);
  169. return 1;
  170. }
  171. /**
  172. * lnb_setup(struct lnb_types_st *lnb, unsigned int freq, unsigned int *freq_if)
  173. * @lnb : lnb type as described int lnb_types_st
  174. * @freq : transponder frequency which user requests
  175. * @freq_if : resultant Intermediate Frequency after down conversion
  176. */
  177. static int lnb_setup(struct lnb_types_st *lnb, struct sec_params *params)
  178. {
  179. int ret = 0;
  180. if (!lnb) {
  181. fprintf(stderr, "Error: lnb_types=NULL\n");
  182. ret = -EINVAL;
  183. goto err;
  184. }
  185. /* TODO! check upper and lower limits from LNB types */
  186. if (!params->freq) {
  187. fprintf(stderr, "Error: invalid frequency, f:%d", params->freq);
  188. ret = -EINVAL;
  189. goto err;
  190. }
  191. if (lnb->switch_val && lnb->high_val && params->freq >= lnb->switch_val) {
  192. /* HI Band */
  193. params->freq_if = params->freq - lnb->high_val;
  194. params->tone = SEC_TONE_ON;
  195. } else {
  196. /* LO Band */
  197. params->tone = SEC_TONE_OFF;
  198. if (params->freq < lnb->low_val)
  199. params->freq_if = lnb->low_val - params->freq;
  200. else
  201. params->freq_if = params->freq - lnb->low_val;
  202. }
  203. err:
  204. return ret;
  205. }
  206. static int diseqc_send_msg(int fd, struct sec_params *params, struct diseqc_cmd *cmd)
  207. {
  208. int ret = 0;
  209. ret = ioctl(fd, FE_SET_TONE, SEC_TONE_OFF);
  210. if (ret < 0) {
  211. fprintf(stderr, "FE_SET_TONE failed\n");
  212. goto err;
  213. }
  214. ret = ioctl(fd, FE_SET_VOLTAGE, params->voltage);
  215. if (ret < 0) {
  216. fprintf(stderr, "FE_SET_VOLTAGE failed, voltage=%d\n", params->voltage);
  217. usleep(15 * 1000);
  218. goto err;
  219. }
  220. ret = ioctl(fd, FE_DISEQC_SEND_MASTER_CMD, &cmd->cmd);
  221. if (ret < 0) {
  222. perror("FE_DISEQC_SEND_MASTER_CMD failed");
  223. usleep(cmd->wait * 1000);
  224. usleep(15 * 1000);
  225. goto err;
  226. }
  227. ret = ioctl(fd, FE_DISEQC_SEND_BURST, params->burst);
  228. if (ret < 0) {
  229. fprintf(stderr, "FE_DISEQC_SEND_BURST failed, burst=%d\n", params->burst);
  230. usleep(15 * 1000);
  231. goto err;
  232. }
  233. ret = ioctl(fd, FE_SET_TONE, params->tone);
  234. if (ret < 0) {
  235. fprintf(stderr, "FE_SET_TONE failed, tone=%d\n", params->tone);
  236. goto err;
  237. }
  238. err:
  239. return ret;
  240. }
  241. static int diseqc_setup(int fd, struct sec_params *params)
  242. {
  243. int pos = params->pos;
  244. int band = params->tone;
  245. fe_sec_voltage_t voltage = params->voltage;
  246. int ret;
  247. struct diseqc_cmd cmd = {{{0xe0, 0x10, 0x38, 0xf0, 0x00, 0x00}, 4}, 0 };
  248. /*
  249. * param: high nibble: reset bits, low nibble set bits,
  250. * bits are: option, position, polarization, band
  251. */
  252. cmd.cmd.msg[3] = 0xf0 |
  253. (((pos * 4) & 0x0f) |
  254. (band ? 1 : 0) |
  255. (voltage ? 0 : 2));
  256. ret = diseqc_send_msg(fd, params, &cmd);
  257. if (ret < 0) {
  258. fprintf(stderr, "SEC message send failed, err=%d", ret);
  259. return -EIO;
  260. }
  261. return 0;
  262. }
  263. static int tune_to(int fd, struct sec_params *sec)
  264. {
  265. struct dvb_frontend_parameters params;
  266. struct dvb_frontend_event ev;
  267. int ret;
  268. /* discard stale QPSK events */
  269. while (1) {
  270. ret = ioctl(fd, FE_GET_EVENT, &ev);
  271. if (ret == -1)
  272. break;
  273. }
  274. params.frequency = sec->freq_if;
  275. params.inversion = INVERSION_AUTO;
  276. params.u.qpsk.symbol_rate = sec->srate;
  277. params.u.qpsk.fec_inner = FEC_AUTO;
  278. ret = ioctl(fd, FE_SET_FRONTEND, &params);
  279. if (ret == -1) {
  280. fprintf(stderr, "FE_SET_FRONTEND error=%d\n", ret);
  281. return -1;
  282. }
  283. return 0;
  284. }
  285. static int frontend_open(int *fd, int adapter, int frontend)
  286. {
  287. static struct dvb_frontend_info info;
  288. char fedev[128];
  289. int ret = 0;
  290. snprintf(fedev, sizeof(fedev), FEDEVICE, adapter, frontend);
  291. *fd = open(fedev, O_RDWR | O_NONBLOCK);
  292. if (*fd < 0) {
  293. fprintf(stderr, "Frontend %d open failed\n", frontend);
  294. ret = -1;
  295. goto err;
  296. }
  297. ret = ioctl(*fd, FE_GET_INFO, &info);
  298. if (ret < 0) {
  299. fprintf(stderr, "ioctl FE_GET_INFO failed\n");
  300. goto err;
  301. }
  302. if (info.type != FE_QPSK) {
  303. fprintf(stderr, "frontend device is not a QPSK (DVB-S) device!\n");
  304. ret = -ENODEV;
  305. goto err;
  306. }
  307. return 0;
  308. err:
  309. fprintf(stderr, "Closing Adapter:%d Frontend:%d\n", adapter, frontend);
  310. close(*fd);
  311. return ret;
  312. }
  313. void bad_usage(char *pname, int prlnb)
  314. {
  315. int i;
  316. struct lnb_types_st *lnb;
  317. char **cp;
  318. if (!prlnb) {
  319. fprintf (stderr, usage_str, pname);
  320. } else {
  321. i = 0;
  322. fprintf(stderr, "-l <lnb-type> or -l low[,high[,switch]] in Mhz\nwhere <lnb-type> is:\n");
  323. while ((lnb = &lnbs[i]) != NULL) {
  324. fprintf (stderr, "%s\n", lnb->name);
  325. for (cp = lnb->desc; *cp; cp++)
  326. fprintf (stderr, " %s\n", *cp);
  327. i++;
  328. }
  329. }
  330. }
  331. static int check_frontend (int fd, int human_readable)
  332. {
  333. fe_status_t status;
  334. uint16_t snr, signal;
  335. uint32_t ber, uncorrected_blocks;
  336. int ret;
  337. do {
  338. ret = ioctl(fd, FE_READ_STATUS, &status);
  339. if (ret == -1) {
  340. fprintf(stderr, "FE_READ_STATUS failed, err=%d", ret);
  341. return -EIO;
  342. }
  343. /*
  344. * some frontends might not support all these ioctls, thus we
  345. * avoid printing errors
  346. */
  347. ret = ioctl(fd, FE_READ_SIGNAL_STRENGTH, &signal);
  348. if (ret == -1)
  349. signal = -2;
  350. ret = ioctl(fd, FE_READ_SNR, &snr);
  351. if (ret == -1)
  352. snr = -2;
  353. ret = ioctl(fd, FE_READ_BER, &ber);
  354. if (ret == -1)
  355. ber = -2;
  356. ret = ioctl(fd, FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks);
  357. if (ret == -1)
  358. uncorrected_blocks = -2;
  359. if (human_readable) {
  360. printf ("status %02x | signal %3u%% | snr %3u%% | ber %d | unc %d | ",
  361. status, (signal * 100) / 0xffff, (snr * 100) / 0xffff, ber, uncorrected_blocks);
  362. } else {
  363. printf ("status %02x | signal %04x | snr %04x | ber %08x | unc %08x | ",
  364. status, signal, snr, ber, uncorrected_blocks);
  365. }
  366. if (status & FE_HAS_LOCK)
  367. printf("FE_HAS_LOCK");
  368. printf("\n");
  369. usleep(1000000);
  370. } while (1);
  371. return 0;
  372. }
  373. /**
  374. * basic options needed
  375. * @adapter
  376. * @frontend
  377. * @freq (Mhz)
  378. * @pol (13/18)
  379. * @srate (kSps)
  380. * @pos (0 - 3)
  381. * @LNB (low, high, switch) Mhz
  382. *
  383. */
  384. int main(int argc, char *argv[])
  385. {
  386. int opt;
  387. int ret, fd, adapter=0, frontend=0, pos;
  388. // int simple;
  389. struct lnb_types_st lnb;
  390. struct sec_params sec;
  391. lnb = lnbs[0]; /* default is Universal LNB */
  392. while ((opt = getopt(argc, argv, "Hh:a:f:p:s:l:")) != -1) {
  393. switch (opt) {
  394. case '?':
  395. case 'h':
  396. default: /* help */
  397. bad_usage(argv[0], 0);
  398. break;
  399. case 'a': /* adapter */
  400. adapter = strtoul(optarg, NULL, 0);
  401. break;
  402. case 'f': /* demodulator (device) */
  403. frontend = strtoul(optarg, NULL, 0);
  404. break;
  405. case 'p': /* parameters */
  406. if (params_decode(optarg, argv, &sec) < 0) {
  407. bad_usage(argv[0], 1);
  408. return -1;
  409. }
  410. break;
  411. case 's': /* diseqc position */
  412. pos = strtoul(optarg, NULL, 0);
  413. pos % 2 ? (sec.burst = SEC_MINI_B) : (sec.burst = SEC_MINI_A);
  414. break;
  415. case 'l':
  416. if (lnb_parse(optarg, &lnb) < 0) {
  417. bad_usage(argv[0], 1);
  418. return -1;
  419. }
  420. break;
  421. case 'H':
  422. // simple = 1; /* human readable */
  423. break;
  424. }
  425. }
  426. lnb.low_val *= 1000; /* kHz */
  427. lnb.high_val *= 1000; /* kHz */
  428. lnb.switch_val *= 1000; /* kHz */
  429. sec.freq *= 1000; /* kHz */
  430. sec.srate *= 1000;
  431. ret = frontend_open(&fd, adapter, frontend);
  432. if (ret < 0) {
  433. fprintf(stderr, "Adapter:%d Frontend:%d open failed, err=%d\n", adapter, frontend, ret);
  434. return -1;
  435. }
  436. ret = lnb_setup(&lnb, &sec);
  437. if (ret < 0) {
  438. fprintf(stderr, "LNB setup failed, err=%d\n", ret);
  439. fprintf(stderr, "%s", usage_str);
  440. goto err;
  441. }
  442. ret = diseqc_setup(fd, &sec);
  443. if (ret < 0) {
  444. fprintf(stderr, "SEC setup failed, err=%d\n", ret);
  445. goto err;;
  446. }
  447. ret = tune_to(fd, &sec);
  448. if (ret < 0) {
  449. fprintf(stderr, "Adapter:%d Frontend:%d tune_to %d %d failed, err=%d\n", adapter, frontend, sec.freq, sec.srate, ret);
  450. goto err;;
  451. }
  452. ret = check_frontend(fd, 1);
  453. if (ret < 0) {
  454. fprintf(stderr, "check frontend failed\n");
  455. goto err;;
  456. }
  457. err:
  458. return ret;
  459. }