/src/lirc_util.c

http://bdremote-ng.googlecode.com/ · C · 116 lines · 64 code · 21 blank · 31 comment · 3 complexity · 4e7f552abd9278908917ff8037cf4552 MD5 · raw file

  1. /*
  2. * bdremoteng - helper daemon for Sony(R) BD Remote Control
  3. * Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
  4. *
  5. * Copyright (C) 2009 Michael Wojciechowski <wojci@wojci.dk>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /** \ingroup LIRC */
  24. /*@{*/
  25. /*! \file lirc_util.c
  26. \brief LIRC, utility functions.
  27. Implements a few utility functions used by the LIRC server.
  28. */
  29. #include "lirc_srv.h"
  30. #include <globaldefs.h>
  31. #define _GNU_SOURCE
  32. #include <stdio.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <syslog.h>
  39. #include <signal.h>
  40. #include <getopt.h>
  41. #include <sys/poll.h>
  42. #include <sys/ioctl.h>
  43. #include <sys/socket.h>
  44. #include <poll.h>
  45. #include <netinet/in.h>
  46. #include <assert.h>
  47. void initTime(keyState* _ks)
  48. {
  49. _ks->cl0.tv_sec = 0;
  50. _ks->cl0.tv_usec = 0;
  51. _ks->cl1.tv_sec = 0;
  52. _ks->cl1.tv_usec = 0;
  53. gettimeofday(&_ks->cl0, NULL);
  54. _ks->cl1 = _ks->cl0;
  55. _ks->elapsed = 0;
  56. }
  57. void updateTime(keyState* _ks)
  58. {
  59. time_t usec = 0;
  60. time_t elapsed = 0;
  61. gettimeofday(&_ks->cl1, NULL);
  62. assert(_ks->cl1.tv_sec >= _ks->cl0.tv_sec);
  63. usec = _ks->cl1.tv_usec - _ks->cl0.tv_usec;
  64. if (usec > 0)
  65. {
  66. elapsed += (usec / 1000);
  67. }
  68. _ks->elapsed += elapsed;
  69. _ks->cl0 = _ks->cl1;
  70. }
  71. int write_socket(int _fd, const char* _buf, int _len)
  72. {
  73. int done = 0;
  74. int todo = _len;
  75. while (todo)
  76. {
  77. done = write(_fd, _buf, todo);
  78. if (done<=0)
  79. {
  80. return done;
  81. }
  82. _buf += done;
  83. todo -= done;
  84. }
  85. return _len;
  86. }
  87. void nolinger(int _sock)
  88. {
  89. static struct linger linger = {0, 0};
  90. int lsize = sizeof(struct linger);
  91. setsockopt(_sock, SOL_SOCKET, SO_LINGER, (void *)&linger, lsize);
  92. }
  93. /*@}*/