Mon, 05 May 2014 13:49:53 +0200
Read configuraion searches two system paths and users home path. Also the configuration filename must be passed to the rdconfig function
lib/mbselib.h | file | annotate | diff | comparison | revisions | |
lib/rc-switch.c | file | annotate | diff | comparison | revisions | |
lib/rdconfig.c | file | annotate | diff | comparison | revisions | |
thermometers/main.c | file | annotate | diff | comparison | revisions |
--- a/lib/mbselib.h Mon May 05 10:33:26 2014 +0200 +++ b/lib/mbselib.h Mon May 05 13:49:53 2014 +0200 @@ -50,6 +50,12 @@ int lastval; /* Last valid value */ } w1_therm; +typedef struct _rc_switch { + struct _rc_switch *next; + char *address; /* Address code */ + char *alias; /* Friendly name */ +} rc_switch; + typedef struct _sys_config { char *name; /* Configuration name */ char *mosq_host; /* mosquitto server hostname */ @@ -58,13 +64,16 @@ #ifdef HAVE_WIRINGPI_H int lcd_cols; /* LCD display columns */ int lcd_rows; /* LCD display rows */ + int rx433; /* 433 MHz receiver pin */ + int tx433; /* 433 MHz transmitter pin */ + rc_switch *rcswitch; /* 433 MHz RC Power switches */ #endif } sys_config; void killconfig(void); -int rdconfig(void); +int rdconfig(char *); /* xutil.c */
--- a/lib/rc-switch.c Mon May 05 10:33:26 2014 +0200 +++ b/lib/rc-switch.c Mon May 05 13:49:53 2014 +0200 @@ -33,7 +33,7 @@ unsigned int rcReceivedDelay = 0; unsigned int rcReceivedProtocol = 0; int rcReceiveTolerance = 60; -int rcReceiverInterrupt = -1; +int rcReceiverInterruptPin = -1; unsigned int timings[RCSWITCH_MAX_CHANGES]; int rcTransmitterPin = -1; @@ -529,10 +529,10 @@ void transmit(int nHighPulses, int nLowPulses) { bool disabled_Receive = false; - int nReceiverInterrupt_backup = rcReceiverInterrupt; + int nReceiverInterrupt_backup = rcReceiverInterruptPin; if (rcTransmitterPin != -1) { - if (rcReceiverInterrupt != -1) { + if (rcReceiverInterruptPin != -1) { disableReceive(); disabled_Receive = true; } @@ -658,16 +658,16 @@ /* * Enable receiving data */ -void enableReceiveIRQ(int interrupt) { - rcReceiverInterrupt = interrupt; +void enableReceiveIRQ(int Pin) { + rcReceiverInterruptPin = Pin; enableReceive(); } void enableReceive(void) { - if (rcReceiverInterrupt != -1) { + if (rcReceiverInterruptPin != -1) { rcReceivedValue = 0; rcReceivedBitlength = 0; - wiringPiISR(rcReceiverInterrupt, INT_EDGE_BOTH, &handleInterrupt); + wiringPiISR(rcReceiverInterruptPin, INT_EDGE_BOTH, &handleInterrupt); } } @@ -678,7 +678,7 @@ */ void disableReceive() { // wiringPi disable interrupts ??? - rcReceiverInterrupt = -1; + rcReceiverInterruptPin = -1; }
--- a/lib/rdconfig.c Mon May 05 10:33:26 2014 +0200 +++ b/lib/rdconfig.c Mon May 05 13:49:53 2014 +0200 @@ -35,6 +35,7 @@ static int getstr(char **); static int getint(char **); static int getw1(char **); +static int getrcs(char **); //static int getbyt(char **); //static int gethex(char **); @@ -51,6 +52,9 @@ #ifdef HAVE_WIRINGPI_H {(char *)"lcd_cols", getint, (char **)&Config.lcd_cols}, {(char *)"lcd_rows", getint, (char **)&Config.lcd_rows}, + {(char *)"rx433", getint, (char **)&Config.rx433}, + {(char *)"tx433", getint, (char **)&Config.tx433}, + {(char *)"rcswitch", getrcs, (char **)&Config.rcswitch}, #endif {NULL, NULL, NULL} }; @@ -60,6 +64,7 @@ void killconfig(void) { w1_therm *tmp1, *old1; + rc_switch *tmp2, *old2; if (Config.name) free(Config.name); @@ -85,12 +90,27 @@ #ifdef HAVE_WIRINGPI_H Config.lcd_cols = 16; Config.lcd_rows = 2; + Config.rx433 = -1; + Config.tx433 = -1; + + for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) { + old2 = tmp2->next; + if (tmp2->address) + free(tmp2->address); + tmp2->address = NULL; + if (tmp2->alias) + free(tmp2->alias); + tmp2->alias = NULL; + free(tmp2); + } + Config.rcswitch = NULL; + #endif } -int rdconfig(void) +int rdconfig(char *config) { char buf[256], *p; FILE *fp; @@ -99,14 +119,30 @@ killconfig(); /* - * Get config from the system path + * Search config file */ - mypath = xstrcpy((char *)STR(ETCDIR)); - mypath = xstrcat(mypath, (char *)"/mbsepi-apps.conf"); - + mypath = xstrcpy(getenv((char *)"HOME")); + mypath = xstrcat(mypath, (char *)"/mbsepi-apps/"); + mypath = xstrcat(mypath, config); if ((fp = fopen(mypath, "r")) == NULL) { - syslog(LOG_NOTICE, "rdconfig: could not open %s", mypath); - return 1; + /* + * Not in the users home directory + */ + free(mypath); + mypath = xstrcpy((char *)"/etc/mbsepi-apps/"); + mypath = xstrcat(mypath, config); + if ((fp = fopen(mypath, "r")) == NULL) { + /* + * Try /usr/local/etc + */ + free(mypath); + mypath = xstrcpy((char *)"/usr/local/etc/mbsepi-apps/"); + mypath = xstrcat(mypath, config); + if ((fp = fopen(mypath, "r")) == NULL) { + syslog(LOG_NOTICE, "rdconfig: could not open %s", mypath); + return 1; + } + } } linecnt = 0; @@ -234,6 +270,46 @@ +static int getrcs(char **dest) +{ + char *p, *q = NULL, *r = NULL; + rc_switch **tmpm; + + for (p = v; *p && !isspace(*p); p++); + if (*p) + *p++ = '\0'; + while (*p && isspace(*p)) + p++; + if (*p == '\0') { + syslog(LOG_NOTICE, "rdconfig: %s(%d): less then two tokens", mypath, linecnt); + return 1; + } + + for (q = p; *q && !isspace(*q); q++); + if (*q && isspace(*q)) { + if (*q) + *q++ = '\0'; + while (*q && isspace(*q)) + q++; + + for (r = q; *r && !isspace(*r); r++); + if (*r) + *r++ = '\0'; + if (debug) + syslog(LOG_NOTICE, "rdconfig: getrcs: %s(%d): %s %s", mypath, linecnt, v, p); + } + + for (tmpm = (rc_switch**)dest; *tmpm; tmpm=&((*tmpm)->next)); + (*tmpm) = (rc_switch *) xmalloc(sizeof(rc_switch)); + (*tmpm)->next = NULL; + (*tmpm)->address = xstrcpy(v); + (*tmpm)->alias = xstrcpy(p); + + return 0; +} + + + /* static int getbyt(char **dest) {
--- a/thermometers/main.c Mon May 05 10:33:26 2014 +0200 +++ b/thermometers/main.c Mon May 05 13:49:53 2014 +0200 @@ -168,7 +168,7 @@ if (debug) fprintf(stdout, "mbsePi-apps thermometers v%s starting\n", VERSION); - if (rdconfig()) { + if (rdconfig((char *)"thermometers.conf")) { fprintf(stderr, "Error reading configuration\n"); syslog(LOG_NOTICE, "halted"); return 1;