dvbnet.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * libdvbnet - a DVB network support library
  3. *
  4. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <sys/param.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <sys/ioctl.h>
  27. #include <linux/dvb/net.h>
  28. #include <errno.h>
  29. #include "dvbnet.h"
  30. int dvbnet_open(int adapter, int netdeviceid)
  31. {
  32. char filename[PATH_MAX+1];
  33. int fd;
  34. sprintf(filename, "/dev/dvb/adapter%i/net%i", adapter, netdeviceid);
  35. if ((fd = open(filename, O_RDWR)) < 0) {
  36. // if that failed, try a flat /dev structure
  37. sprintf(filename, "/dev/dvb%i.net%i", adapter, netdeviceid);
  38. fd = open(filename, O_RDWR);
  39. }
  40. return fd;
  41. }
  42. int dvbnet_add_interface(int fd, uint16_t pid, enum dvbnet_encap encapsulation)
  43. {
  44. struct dvb_net_if params;
  45. int status;
  46. memset(&params, 0, sizeof(params));
  47. params.pid = pid;
  48. switch(encapsulation) {
  49. case DVBNET_ENCAP_MPE:
  50. params.feedtype = DVB_NET_FEEDTYPE_MPE;
  51. break;
  52. case DVBNET_ENCAP_ULE:
  53. params.feedtype = DVB_NET_FEEDTYPE_ULE;
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. status = ioctl(fd, NET_ADD_IF, &params);
  59. if (status < 0)
  60. return status;
  61. return params.if_num;
  62. }
  63. int dvbnet_get_interface(int fd, int ifnum, uint16_t *pid, enum dvbnet_encap *encapsulation)
  64. {
  65. struct dvb_net_if info;
  66. int res;
  67. memset(&info, 0, sizeof(struct dvb_net_if));
  68. info.if_num = ifnum;
  69. if ((res = ioctl(fd, NET_GET_IF, &info)) < 0)
  70. return res;
  71. *pid = info.pid;
  72. switch(info.feedtype) {
  73. case DVB_NET_FEEDTYPE_MPE:
  74. *encapsulation = DVBNET_ENCAP_MPE;
  75. break;
  76. case DVB_NET_FEEDTYPE_ULE:
  77. *encapsulation = DVBNET_ENCAP_ULE;
  78. break;
  79. default:
  80. return -EINVAL;
  81. }
  82. return 0;
  83. }
  84. int dvbnet_remove_interface(int fd, int ifnum)
  85. {
  86. return ioctl(fd, NET_REMOVE_IF, ifnum);
  87. }