/src/lirc_srv.h

http://bdremote-ng.googlecode.com/ · C Header · 162 lines · 46 code · 37 blank · 79 comment · 0 complexity · 9e574af361edc9f86081d0c2e9b05143 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. /** @defgroup LIRC LIRC
  24. * This group contains a function used to change the UID/GID of the
  25. * application to one with less priviledges.
  26. * @{
  27. */
  28. /*! \file lirc_srv.h
  29. \brief LIRC server.
  30. This file contains a LIRC server. It's purpose is to translate
  31. bluetooth events received using a queue from the bluetooth
  32. interface into key press events which are understood by LIRC.
  33. */
  34. #ifndef BD_LIRC_SRV_H
  35. #define BD_LIRC_SRV_H
  36. #include <globaldefs.h>
  37. #include <bdrcfg.h>
  38. #include <stdint.h>
  39. #include <pthread.h>
  40. #include <sys/time.h>
  41. #include <q.h>
  42. /** The number of LIRC clients that can connect at the same time. */
  43. #define MAX_CLIENTS 16
  44. /** Information used by the LIRC server. */
  45. typedef struct
  46. {
  47. #if BDREMOTE_DEBUG
  48. /** Magic value, used for asserting. */
  49. int magic0;
  50. #endif /* BDREMOTE_DEBUG */
  51. /** Configuration. */
  52. const configuration* config;
  53. /** Socked used to accept new LIRC clients. */
  54. int sockinet;
  55. /** Client sockets. */
  56. int clis[MAX_CLIENTS];
  57. /** Number of client sockets. */
  58. int clin;
  59. /* State information. */
  60. /** Battery charge in percent. */
  61. int charge_percent;
  62. /** Indictes if a charge was previously set.*/
  63. int charge_percent_set;
  64. /** Mutex used. */
  65. pthread_mutex_t dataMutex;
  66. /** Queue used to communicate BT events to lirc clients.
  67. * Thread safe.
  68. */
  69. queue qu;
  70. /** Thread used to receive bluetooth events (keypresses) and
  71. sending them to LIRC in a format it understands.. */
  72. pthread_t thread;
  73. } lirc_data;
  74. /** Init data used by the LIRC server part of this application. */
  75. void initLircData(lirc_data* _ld, const configuration* _config);
  76. /** Start a thread used to receive bluetooth events (keypresses) and
  77. sending them to LIRC in a format it understands. */
  78. void startLircThread(lirc_data* _ld);
  79. /** Wait for the LIRC thread to terminate. */
  80. void waitForLircThread(lirc_data* _ld);
  81. /** Run a LIRC server. Should be run from the main thread of an
  82. application. Blocks. */
  83. int lirc_server(configuration* _config, lirc_data* _lircdata);
  84. /** Broadcast a message to all connected LIRC sockets. */
  85. void broadcast_message(lirc_data* _lircdata, const char* _message);
  86. /** Destroy LIRC data used by the LIRC server part of this application. */
  87. void destroyLircData(lirc_data* _ld);
  88. /** Write a message to a socket. */
  89. int write_socket(int _fd, const char* _buf, int _len);
  90. /** Close sockets gracefully. */
  91. void nolinger(int sock);
  92. /** Remote a LIRC client from the list of clients receiving our messages. */
  93. void remove_client(lirc_data* _lircdata, int fd);
  94. /** Struct used to keep track of pressed key and repeat state. */
  95. typedef struct
  96. {
  97. /** Indicates if a key is down. */
  98. int keyDown;
  99. /** Last sent key. */
  100. int lastKey;
  101. /** Clock used to keep time. */
  102. struct timeval cl0;
  103. /** Clock used to keep time. */
  104. struct timeval cl1;
  105. /** Number of elapsed miliseconds. */
  106. unsigned long elapsed;
  107. /** Repeat counter. */
  108. int repeat_count;
  109. /** Indicates that the driver is done waiting to begin stending
  110. repeated keypresses. */
  111. int done_waiting;
  112. } keyState;
  113. /* The following two functions should be used to get elapsed time
  114. under one second. */
  115. /** Start keeping track of time. */
  116. void initTime(keyState* _ks);
  117. /** Update time counter. To avoid overflows, reset the counter
  118. often. */
  119. void updateTime(keyState* _ks);
  120. #endif /* BD_LIRC_SRV_H */
  121. /*@}*/