dvbsec_cfg.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. * dvbsec_cfg (i.e. linuxtv sec format) configuration file support.
  3. *
  4. * Copyright (c) 2005 by Andrew de Quincey <adq_dvb@lidskialf.net>
  5. *
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define _GNU_SOURCE
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <linux/types.h>
  28. #include "dvbsec_cfg.h"
  29. int dvbcfg_issection(char* line, char* sectionname)
  30. {
  31. int len;
  32. len = strlen(line);
  33. if (len < 2)
  34. return 0;
  35. if ((line[0] != '[') || (line[len-1] != ']'))
  36. return 0;
  37. line++;
  38. while(isspace(*line))
  39. line++;
  40. if (strncmp(line, sectionname, strlen(sectionname)))
  41. return 0;
  42. return 1;
  43. }
  44. char* dvbcfg_iskey(char* line, char* keyname)
  45. {
  46. int len = strlen(keyname);
  47. /* does the key match? */
  48. if (strncmp(line, keyname, len))
  49. return NULL;
  50. /* skip keyname & any whitespace */
  51. line += len;
  52. while(isspace(*line))
  53. line++;
  54. /* should be the '=' sign */
  55. if (*line != '=')
  56. return 0;
  57. /* more whitespace skipping */
  58. line++;
  59. while(isspace(*line))
  60. line++;
  61. /* finally, return the value */
  62. return line;
  63. }
  64. int dvbsec_cfg_load(FILE *f,
  65. void *arg,
  66. dvbsec_cfg_callback cb)
  67. {
  68. struct dvbsec_config tmpsec;
  69. char *linebuf = NULL;
  70. size_t line_size = 0;
  71. int len;
  72. int insection = 0;
  73. char *value;
  74. /* process each line */
  75. while((len = getline(&linebuf, &line_size, f)) > 0) {
  76. char *line = linebuf;
  77. /* chop any comments */
  78. char *hashpos = strchr(line, '#');
  79. if (hashpos)
  80. *hashpos = 0;
  81. char *lineend = line + strlen(line);
  82. /* trim the line */
  83. while(*line && isspace(*line))
  84. line++;
  85. while((lineend != line) && isspace(*(lineend-1)))
  86. lineend--;
  87. *lineend = 0;
  88. /* skip blank lines */
  89. if (*line == 0)
  90. continue;
  91. if (dvbcfg_issection(line, "sec")) {
  92. if (insection) {
  93. if (cb(arg, &tmpsec))
  94. return 0;
  95. }
  96. insection = 1;
  97. memset(&tmpsec, 0, sizeof(tmpsec));
  98. } else if ((value = dvbcfg_iskey(line, "name")) != NULL) {
  99. strncpy(tmpsec.id, value, sizeof(tmpsec.id));
  100. } else if ((value = dvbcfg_iskey(line, "switch-frequency")) != NULL) {
  101. tmpsec.switch_frequency = atoi(value);
  102. } else if ((value = dvbcfg_iskey(line, "lof-lo-v")) != NULL) {
  103. tmpsec.lof_lo_v = atoi(value);
  104. } else if ((value = dvbcfg_iskey(line, "lof-lo-h")) != NULL) {
  105. tmpsec.lof_lo_h = atoi(value);
  106. } else if ((value = dvbcfg_iskey(line, "lof-lo-l")) != NULL) {
  107. tmpsec.lof_lo_l = atoi(value);
  108. } else if ((value = dvbcfg_iskey(line, "lof-lo-r")) != NULL) {
  109. tmpsec.lof_lo_r = atoi(value);
  110. } else if ((value = dvbcfg_iskey(line, "lof-hi-v")) != NULL) {
  111. tmpsec.lof_hi_v = atoi(value);
  112. } else if ((value = dvbcfg_iskey(line, "lof-hi-h")) != NULL) {
  113. tmpsec.lof_hi_h = atoi(value);
  114. } else if ((value = dvbcfg_iskey(line, "lof-hi-l")) != NULL) {
  115. tmpsec.lof_hi_l = atoi(value);
  116. } else if ((value = dvbcfg_iskey(line, "lof-hi-r")) != NULL) {
  117. tmpsec.lof_hi_r = atoi(value);
  118. } else if ((value = dvbcfg_iskey(line, "config-type")) != NULL) {
  119. if (!strcasecmp(value, "none")) {
  120. tmpsec.config_type = DVBSEC_CONFIG_NONE;
  121. } else if (!strcasecmp(value, "power")) {
  122. tmpsec.config_type = DVBSEC_CONFIG_POWER;
  123. } else if (!strcasecmp(value, "standard")) {
  124. tmpsec.config_type = DVBSEC_CONFIG_STANDARD;
  125. } else if (!strcasecmp(value, "advanced")) {
  126. tmpsec.config_type = DVBSEC_CONFIG_ADVANCED;
  127. } else {
  128. insection = 0;
  129. }
  130. } else if ((value = dvbcfg_iskey(line, "cmd-lo-v")) != NULL) {
  131. strncpy(tmpsec.adv_cmd_lo_v, value, sizeof(tmpsec.adv_cmd_lo_v));
  132. } else if ((value = dvbcfg_iskey(line, "cmd-lo-h")) != NULL) {
  133. strncpy(tmpsec.adv_cmd_lo_h, value, sizeof(tmpsec.adv_cmd_lo_h));
  134. } else if ((value = dvbcfg_iskey(line, "cmd-lo-r")) != NULL) {
  135. strncpy(tmpsec.adv_cmd_lo_r, value, sizeof(tmpsec.adv_cmd_lo_r));
  136. } else if ((value = dvbcfg_iskey(line, "cmd-lo-l")) != NULL) {
  137. strncpy(tmpsec.adv_cmd_lo_l, value, sizeof(tmpsec.adv_cmd_lo_l));
  138. } else if ((value = dvbcfg_iskey(line, "cmd-hi-v")) != NULL) {
  139. strncpy(tmpsec.adv_cmd_hi_v, value, sizeof(tmpsec.adv_cmd_hi_v));
  140. } else if ((value = dvbcfg_iskey(line, "cmd-hi-h")) != NULL) {
  141. strncpy(tmpsec.adv_cmd_hi_h, value, sizeof(tmpsec.adv_cmd_hi_h));
  142. } else if ((value = dvbcfg_iskey(line, "cmd-hi-r")) != NULL) {
  143. strncpy(tmpsec.adv_cmd_hi_r, value, sizeof(tmpsec.adv_cmd_hi_r));
  144. } else if ((value = dvbcfg_iskey(line, "cmd-hi-l")) != NULL) {
  145. strncpy(tmpsec.adv_cmd_hi_l, value, sizeof(tmpsec.adv_cmd_hi_l));
  146. } else {
  147. insection = 0;
  148. }
  149. }
  150. // output the final section if there is one
  151. if (insection) {
  152. if (cb(arg, &tmpsec))
  153. return 0;
  154. }
  155. if (linebuf)
  156. free(linebuf);
  157. return 0;
  158. }
  159. static int dvbsec_cfg_find_callback(void *arg, struct dvbsec_config *sec);
  160. static int dvbsec_cfg_find_default(const char *sec_id, struct dvbsec_config *sec);
  161. struct findparams {
  162. const char *sec_id;
  163. struct dvbsec_config *sec_dest;
  164. };
  165. int dvbsec_cfg_find(const char *config_file,
  166. const char *sec_id,
  167. struct dvbsec_config *sec)
  168. {
  169. struct findparams findp;
  170. // clear the structure
  171. memset(sec, 0, sizeof(struct dvbsec_config));
  172. // open the file
  173. if (config_file != NULL) {
  174. FILE *f = fopen(config_file, "r");
  175. if (f == NULL)
  176. return -EIO;
  177. // parse each entry
  178. findp.sec_id = sec_id;
  179. findp.sec_dest = sec;
  180. dvbsec_cfg_load(f, &findp, dvbsec_cfg_find_callback);
  181. // done
  182. fclose(f);
  183. // find it?
  184. if (sec->id[0])
  185. return 0;
  186. }
  187. return dvbsec_cfg_find_default(sec_id, sec);
  188. }
  189. static int dvbsec_cfg_find_callback(void *arg, struct dvbsec_config *sec)
  190. {
  191. struct findparams *findp = arg;
  192. if (strcmp(findp->sec_id, sec->id))
  193. return 0;
  194. memcpy(findp->sec_dest, sec, sizeof(struct dvbsec_config));
  195. return 1;
  196. }
  197. int dvbsec_cfg_save(FILE *f,
  198. struct dvbsec_config *secs,
  199. int count)
  200. {
  201. int i;
  202. for(i=0; i<count; i++) {
  203. char *config_type = "";
  204. switch(secs[i].config_type) {
  205. case DVBSEC_CONFIG_NONE:
  206. config_type = "none";
  207. break;
  208. case DVBSEC_CONFIG_POWER:
  209. config_type = "power";
  210. break;
  211. case DVBSEC_CONFIG_STANDARD:
  212. config_type = "standard";
  213. break;
  214. case DVBSEC_CONFIG_ADVANCED:
  215. config_type = "advanced";
  216. break;
  217. }
  218. fprintf(f, "[lnb]\n");
  219. fprintf(f, "switch-frequency=%i\n", secs[i].switch_frequency);
  220. if (secs[i].lof_lo_v)
  221. fprintf(f, "lof-lo-v=%i\n", secs[i].lof_lo_v);
  222. if (secs[i].lof_lo_h)
  223. fprintf(f, "lof-lo-h=%i\n", secs[i].lof_lo_h);
  224. if (secs[i].lof_lo_l)
  225. fprintf(f, "lof-lo-l=%i\n", secs[i].lof_lo_l);
  226. if (secs[i].lof_lo_r)
  227. fprintf(f, "lof-lo-r=%i\n", secs[i].lof_lo_r);
  228. if (secs[i].lof_hi_v)
  229. fprintf(f, "lof-hi-v=%i\n", secs[i].lof_hi_v);
  230. if (secs[i].lof_hi_h)
  231. fprintf(f, "lof-hi-h=%i\n", secs[i].lof_hi_h);
  232. if (secs[i].lof_hi_l)
  233. fprintf(f, "lof-hi-l=%i\n", secs[i].lof_hi_l);
  234. if (secs[i].lof_hi_r)
  235. fprintf(f, "lof-hi-r=%i\n", secs[i].lof_hi_r);
  236. fprintf(f, "config-type=%s\n", config_type);
  237. if (secs[i].config_type == DVBSEC_CONFIG_ADVANCED) {
  238. if (secs[i].adv_cmd_lo_h[0])
  239. fprintf(f, "cmd-lo-h=%s\n", secs[i].adv_cmd_lo_h);
  240. if (secs[i].adv_cmd_lo_v[0])
  241. fprintf(f, "cmd-lo-v=%s\n", secs[i].adv_cmd_lo_v);
  242. if (secs[i].adv_cmd_lo_r[0])
  243. fprintf(f, "cmd-lo-r=%s\n", secs[i].adv_cmd_lo_r);
  244. if (secs[i].adv_cmd_lo_l[0])
  245. fprintf(f, "cmd-lo-l=%s\n", secs[i].adv_cmd_lo_l);
  246. if (secs[i].adv_cmd_hi_h[0])
  247. fprintf(f, "cmd-hi-h=%s\n", secs[i].adv_cmd_hi_h);
  248. if (secs[i].adv_cmd_hi_v[0])
  249. fprintf(f, "cmd-hi-v=%s\n", secs[i].adv_cmd_hi_v);
  250. if (secs[i].adv_cmd_hi_r[0])
  251. fprintf(f, "cmd-hi-r=%s\n", secs[i].adv_cmd_hi_r);
  252. if (secs[i].adv_cmd_hi_l[0])
  253. fprintf(f, "cmd-hi-l=%s\n", secs[i].adv_cmd_hi_l);
  254. }
  255. fprintf(f, "\n");
  256. }
  257. return 0;
  258. }
  259. static struct dvbsec_config defaults[] = {
  260. {
  261. .id = "NULL",
  262. .config_type = DVBSEC_CONFIG_STANDARD,
  263. },
  264. {
  265. .id = "UNIVERSAL",
  266. .switch_frequency = 11700000,
  267. .lof_lo_v = 9750000,
  268. .lof_lo_h = 9750000,
  269. .lof_hi_v = 10600000,
  270. .lof_hi_h = 10600000,
  271. .config_type = DVBSEC_CONFIG_STANDARD,
  272. },
  273. {
  274. .id = "DBS",
  275. .switch_frequency = 0,
  276. .lof_lo_v = 11250000,
  277. .lof_lo_h = 11250000,
  278. .config_type = DVBSEC_CONFIG_STANDARD,
  279. },
  280. {
  281. .id = "STANDARD",
  282. .switch_frequency = 0,
  283. .lof_lo_v = 10000000,
  284. .lof_lo_h = 10000000,
  285. .config_type = DVBSEC_CONFIG_STANDARD,
  286. },
  287. {
  288. .id = "ENHANCED",
  289. .switch_frequency = 0,
  290. .lof_lo_v = 9750000,
  291. .lof_lo_h = 9750000,
  292. .config_type = DVBSEC_CONFIG_STANDARD,
  293. },
  294. {
  295. .id = "C-BAND",
  296. .switch_frequency = 0,
  297. .lof_lo_v = 5150000,
  298. .lof_lo_h = 5150000,
  299. .config_type = DVBSEC_CONFIG_POWER,
  300. },
  301. {
  302. .id = "C-MULTI",
  303. .switch_frequency = 0,
  304. .lof_lo_v = 5150000,
  305. .lof_lo_h = 5750000,
  306. .config_type = DVBSEC_CONFIG_POWER,
  307. },
  308. };
  309. #define defaults_count (sizeof(defaults) / sizeof(struct dvbsec_config))
  310. static int dvbsec_cfg_find_default(const char *sec_id,
  311. struct dvbsec_config *sec)
  312. {
  313. unsigned int i;
  314. for(i=0; i< defaults_count; i++) {
  315. if (!strncmp(sec_id, defaults[i].id, sizeof(defaults[i].id))) {
  316. memcpy(sec, &defaults[i], sizeof(struct dvbsec_config));
  317. return 0;
  318. }
  319. }
  320. return -1;
  321. }