/src/bdrcfg.c

http://bdremote-ng.googlecode.com/ · C · 255 lines · 188 code · 36 blank · 31 comment · 32 complexity · 3f809f84c4d8796c14408d29bbfc8434 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 Gen
  24. * @{
  25. */
  26. /*! \file bdrcfg.c
  27. \brief Implements functions for handling configuration.
  28. Implements a few functions used to set certain config keys.
  29. */
  30. #include "bdrcfg.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <assert.h>
  35. #include <globaldefs.h>
  36. void setDefaults(configuration* _config)
  37. {
  38. assert(_config != NULL);
  39. _config->listen_port = 8888;
  40. _config->disconnect_timeout = 60;
  41. _config->repeat_rate = 10;
  42. _config->repeat_delay = 200;
  43. _config->debug = 0;
  44. _config->interface_addr_set = 0;
  45. FREEVAL(_config->interface_addr);
  46. FREEVAL(_config->remote_addr);
  47. _config->remote_addr = NULL;
  48. _config->detach = 1;
  49. FREEVAL(_config->release);
  50. _config->lirc_namespace = 0;
  51. _config->release = NULL;
  52. _config->event_out = 0;
  53. FREEVAL(_config->user);
  54. _config->user = NULL;
  55. FREEVAL(_config->group);
  56. _config->group = NULL;
  57. _config->log_filename_set = 0;
  58. FREEVAL(_config->log_filename);
  59. _config->log_filename = NULL;
  60. _config->battery_script_set = 0;
  61. FREEVAL(_config->battery_script);
  62. _config->battery_script = NULL;
  63. }
  64. void setRemoteAddress(configuration* _config, const char* _address)
  65. {
  66. assert(_config != NULL);
  67. assert(_address != NULL);
  68. SETVAL(_config->remote_addr, _address);
  69. }
  70. void setInterfaceAddress(configuration* _config, const char* _address)
  71. {
  72. assert(_config != NULL);
  73. assert(_address != NULL);
  74. SETVAL(_config->interface_addr, _address);
  75. _config->interface_addr_set = 1;
  76. }
  77. void setRelease(configuration* _config, const char* _release)
  78. {
  79. assert(_config != NULL);
  80. assert(_release != NULL);
  81. SETVAL(_config->release, _release);
  82. }
  83. void setUser(configuration* _config, const char* _user)
  84. {
  85. assert(_config != NULL);
  86. assert(_user != NULL);
  87. SETVAL(_config->user, _user);
  88. }
  89. void setGroup(configuration* _config, const char* _group)
  90. {
  91. assert(_config != NULL);
  92. assert(_group != NULL);
  93. SETVAL(_config->group, _group);
  94. }
  95. void setBatteryScript(configuration* _config, const char* _script)
  96. {
  97. assert(_config != NULL);
  98. assert(_script != NULL);
  99. SETVAL(_config->battery_script, _script);
  100. _config->battery_script_set = 1;
  101. }
  102. void destroyConfig(configuration* _config)
  103. {
  104. assert(_config != NULL);
  105. FREEVAL(_config->remote_addr);
  106. FREEVAL(_config->release);
  107. FREEVAL(_config->user);
  108. FREEVAL(_config->group);
  109. FREEVAL(_config->log_filename);
  110. FREEVAL(_config->battery_script);
  111. }
  112. void printConfig(const configuration* _config)
  113. {
  114. assert(_config != NULL);
  115. fprintf(printStream, "Configuration:\n");
  116. fprintf(printStream, " - listen port : %d.\n", _config->listen_port);
  117. fprintf(printStream, " - timeout : %d second(s).\n", _config->disconnect_timeout);
  118. #if ENABLE_REPEAT
  119. fprintf(printStream, " - repeat rate : %d per secound.\n", _config->repeat_rate);
  120. fprintf(printStream, " - repeat delay: %d ms.\n", _config->repeat_delay);
  121. #else
  122. fprintf(printStream, " - repeat rate : disabled.\n");
  123. fprintf(printStream, " - repeat delay: disabled.\n");
  124. #endif
  125. if (_config->release == NULL)
  126. {
  127. fprintf(printStream, " - release : not set.\n");
  128. }
  129. else
  130. {
  131. fprintf(printStream, " - release : %s.\n", _config->release);
  132. }
  133. fprintf(printStream, " - LIRC names : %d.\n", _config->lirc_namespace);
  134. fprintf(printStream, " - event output: %d.\n", _config->event_out);
  135. fprintf(printStream, " - debug : %d.\n", _config->debug);
  136. if (_config->debug)
  137. {
  138. fprintf(printStream, " LIRC thread(%u) sock(%u) callback(%u),\n",
  139. BDREMOTE_MASKCHECK(MODULEMASK_LIRC_THR),
  140. BDREMOTE_MASKCHECK(MODULEMASK_LIRC_SOCK),
  141. BDREMOTE_MASKCHECK(MODULEMASK_LIRC_CB));
  142. fprintf(printStream, " bluetooth interface(%u) implementation(%u),\n",
  143. BDREMOTE_MASKCHECK(MODULEMASK_BT_IF),
  144. BDREMOTE_MASKCHECK(MODULEMASK_BT_IMPL));
  145. fprintf(printStream, " queue(%u),\n",
  146. BDREMOTE_MASKCHECK(MODULEMASK_QUEUE));
  147. fprintf(printStream, " spare(%u),\n",
  148. BDREMOTE_MASKCHECK(MODULEMASK_SPARE));
  149. fprintf(printStream, " main(%u).\n",
  150. BDREMOTE_MASKCHECK(MODULEMASK_MAIN));
  151. }
  152. if (_config->interface_addr_set)
  153. {
  154. fprintf(printStream, " - iface addr : %s.\n", _config->interface_addr);
  155. }
  156. else
  157. {
  158. fprintf(printStream, " - iface addr : ANY.\n");
  159. }
  160. fprintf(printStream, " - remote addr : %s.\n", _config->remote_addr);
  161. fprintf(printStream, " - detach : %d.\n", _config->detach);
  162. if (_config->user == NULL)
  163. {
  164. fprintf(printStream, " - user : not set.\n");
  165. }
  166. else
  167. {
  168. fprintf(printStream, " - user : %s.\n", _config->user);
  169. }
  170. if (_config->group == NULL)
  171. {
  172. fprintf(printStream, " - group : not set.\n");
  173. }
  174. else
  175. {
  176. fprintf(printStream, " - group : %s.\n", _config->group);
  177. }
  178. if (_config->log_filename_set)
  179. {
  180. fprintf(printStream, " - log : '%s'.\n", _config->log_filename);
  181. }
  182. else
  183. {
  184. fprintf(printStream, " - log : stdout.\n");
  185. }
  186. if (_config->battery_script_set)
  187. {
  188. fprintf(printStream, " - battery\n");
  189. fprintf(printStream, " - script : '%s'.\n", _config->battery_script);
  190. }
  191. else
  192. {
  193. fprintf(printStream, " - battery\n");
  194. fprintf(printStream, " - script : not set.\n");
  195. }
  196. }
  197. int userAndGroupSet(const configuration* _config)
  198. {
  199. int status = 1;
  200. assert(_config != NULL);
  201. if (_config->user == NULL)
  202. {
  203. status = 0;
  204. }
  205. if (_config->group == NULL)
  206. {
  207. status = 0;
  208. }
  209. return status;
  210. }
  211. void setLogFilename(configuration* _config, const char* _filename)
  212. {
  213. assert(_config != NULL);
  214. assert(_filename != NULL);
  215. SETVAL(_config->log_filename, _filename);
  216. _config->log_filename_set = 1;
  217. }
  218. /*@}*/