main: reformat code

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
This commit is contained in:
Heinrich Schuchardt 2020-04-07 18:21:57 +02:00
parent bacfc8e95a
commit 5807e488e8
1 changed files with 79 additions and 77 deletions

View File

@ -134,7 +134,7 @@ static void print_disclaimer(void)
return; return;
} }
static void print_usage(char* name) static void print_usage(char *name)
{ {
print_disclaimer(); print_disclaimer();
fprintf(stderr,"\n" fprintf(stderr,"\n"
@ -186,22 +186,22 @@ static void print_usage(char* name)
#endif #endif
} }
static void parse_command_line(int argc, char* argv[], int count, static void parse_command_line(int argc, char *argv[], int count,
struct usb_device*dev[], char *usbdevsn[]) struct usb_device *dev[], char *usbdevsn[])
{ {
int numeric=0; int numeric = 0;
int c; int c;
int i,j; int i,j;
int result; int result;
int from=1, upto=4; int from = 1, upto = 4;
int status; int status;
int devnum=0; int devnum = 0;
usb_dev_handle *udev=NULL; usb_dev_handle *udev = NULL;
usb_dev_handle *sudev=NULL; //scan device usb_dev_handle *sudev = NULL; //scan device
unsigned int id=0; //product id of current device unsigned int id=0; //product id of current device
char* onoff[] = { "off", "on", "0", "1" }; char *onoff[] = {"off", "on", "0", "1"};
#ifndef WEBLESS #ifndef WEBLESS
char* bindaddr=0; char *bindaddr=0;
#endif #endif
unsigned int outlet; unsigned int outlet;
struct plannif plan; struct plannif plan;
@ -213,7 +213,7 @@ static void parse_command_line(int argc, char* argv[], int count,
bindaddr=BINDADDR; bindaddr=BINDADDR;
#endif #endif
while( (c=getopt(argc, argv,"i:o:f:t:a:A:b:g:m:lLqvh?nsd:D:u:p:U:")) != -1 ) { while((c=getopt(argc, argv,"i:o:f:t:a:A:b:g:m:lLqvh?nsd:D:u:p:U:")) != -1) {
if (count == 0) { if (count == 0) {
switch(c) { switch(c) {
case '?': case '?':
@ -229,16 +229,16 @@ static void parse_command_line(int argc, char* argv[], int count,
exit(1); exit(1);
} }
} }
if( c=='o' || c=='f' || c=='g' || c=='t' || c=='a' || c=='A' || c=='m') { if(strchr("ofgtaAm", c)) {
if((strncmp(optarg,"all",strlen("all"))==0) if(!strncmp(optarg,"all", strlen("all"))
|| (atoi(optarg)==7) ) { || (atoi(optarg) == 7) ) {
//use all outlets //use all outlets
from=1; from=1;
upto=4; upto=4;
} else { } else {
from = upto = atoi(optarg); from = upto = atoi(optarg);
} }
if (from<1 || upto>4) { if (from < 1 || upto > 4) {
fprintf(stderr,"Invalid outlet number given: %s\n" fprintf(stderr,"Invalid outlet number given: %s\n"
"Expected: 1, 2, 3, 4, or all.\nTerminating.\n",optarg); "Expected: 1, 2, 3, 4, or all.\nTerminating.\n",optarg);
print_disclaimer(); print_disclaimer();
@ -247,23 +247,24 @@ static void parse_command_line(int argc, char* argv[], int count,
} else { } else {
from = upto = 0; from = upto = 0;
} }
if( c=='o' || c=='f' || c=='g' || c=='b' || c=='t' || c=='a' || c=='A' if(strchr("ofgbtaAm", c)) { //we need a device handle for these commands
|| c=='m') { //we need a device handle for these commands
/* get device-handle/-id if it wasn't done already */ /* get device-handle/-id if it wasn't done already */
if(udev==NULL) { if(udev == NULL) {
udev = get_handle(dev[devnum]); udev = get_handle(dev[devnum]);
if(udev==NULL) { if(udev == NULL) {
fprintf(stderr, "No access to Gembird #%d USB device %s\n", fprintf(stderr, "No access to Gembird #%d USB device %s\n",
devnum, dev[devnum]->filename ); devnum, dev[devnum]->filename );
exit(1); exit(1);
} else if(verbose) printf("Accessing Gembird #%d USB device %s\n", } else if(verbose) {
devnum, dev[devnum]->filename ); printf("Accessing Gembird #%d USB device %s\n", devnum,
dev[devnum]->filename);
}
id = get_id(dev[devnum]); id = get_id(dev[devnum]);
} }
} }
#ifdef WEBLESS #ifdef WEBLESS
if (c=='l' || c=='L' || c=='i' || c=='p' || c=='u' ) { if (strchr("lLipu", c)) {
fprintf(stderr,"Application was compiled without web-interface. " fprintf(stderr,"Application was compiled without web-interface. "
"Feature not available.\n"); "Feature not available.\n");
exit(-100); exit(-100);
@ -285,7 +286,7 @@ static void parse_command_line(int argc, char* argv[], int count,
for (i=from; i <= upto; ++i) { for (i=from; i <= upto; ++i) {
switch(c) { switch(c) {
case 's': case 's':
for (status=0; status<count; ++status) { for (status = 0; status < count; ++status) {
if (numeric == 0) if (numeric == 0)
printf("Gembird #%d\nUSB information: bus %s, device %s\n", status, printf("Gembird #%d\nUSB information: bus %s, device %s\n", status,
dev[status]->bus->dirname, dev[status]->filename); dev[status]->bus->dirname, dev[status]->filename);
@ -306,7 +307,7 @@ static void parse_command_line(int argc, char* argv[], int count,
printf("1\n"); printf("1\n");
sudev = get_handle(dev[status]); sudev = get_handle(dev[status]);
id = get_id(dev[status]); id = get_id(dev[status]);
if(sudev==NULL) { if(sudev == NULL) {
fprintf(stderr, "No access to Gembird #%d USB device %s\n", fprintf(stderr, "No access to Gembird #%d USB device %s\n",
status, dev[status]->filename ); status, dev[status]->filename );
exit(1); exit(1);
@ -314,7 +315,7 @@ static void parse_command_line(int argc, char* argv[], int count,
if (numeric == 0) if (numeric == 0)
printf("serial number: %s\n",get_serial(sudev)); printf("serial number: %s\n",get_serial(sudev));
else else
printf("%s\n",get_serial(sudev)); printf("%s\n", get_serial(sudev));
usb_close(sudev); usb_close(sudev);
sudev = NULL; sudev = NULL;
printf("\n"); printf("\n");
@ -339,7 +340,7 @@ static void parse_command_line(int argc, char* argv[], int count,
} }
break; break;
case 'D': // by serial number case 'D': // by serial number
for (j=0; j < count; ++j) { for (j = 0; j < count; ++j) {
if (debug) if (debug)
fprintf(stderr, "now comparing %s and %s\n", usbdevsn[j], optarg); fprintf(stderr, "now comparing %s and %s\n", usbdevsn[j], optarg);
if (strcasecmp(usbdevsn[j], optarg) == 0) { if (strcasecmp(usbdevsn[j], optarg) == 0) {
@ -363,8 +364,8 @@ static void parse_command_line(int argc, char* argv[], int count,
break; break;
case 'U': // by USB Bus:Device case 'U': // by USB Bus:Device
for (j=0; j < count; ++j) { for (j=0; j < count; ++j) {
char tmp[8194]; char tmp[8194];
sprintf(tmp, "%s:%s", dev[j]->bus->dirname, dev[j]->filename); sprintf(tmp, "%s:%s", dev[j]->bus->dirname, dev[j]->filename);
if (debug) if (debug)
fprintf(stderr, "now comparing %s and %s\n", tmp, optarg); fprintf(stderr, "now comparing %s and %s\n", tmp, optarg);
@ -386,20 +387,20 @@ static void parse_command_line(int argc, char* argv[], int count,
} }
exit(-8); exit(-8);
} }
break; break;
case 'o': case 'o':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
sispm_switch_on(udev,id,outlet); sispm_switch_on(udev,id,outlet);
if(verbose) printf("Switched outlet %d %s\n",i,onoff[1+numeric]); if(verbose) printf("Switched outlet %d %s\n",i,onoff[1+numeric]);
break; break;
case 'f': case 'f':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
sispm_switch_off(udev,id,outlet); sispm_switch_off(udev,id,outlet);
if(verbose) printf("Switched outlet %d %s\n",i,onoff[0+numeric]); if(verbose) printf("Switched outlet %d %s\n",i,onoff[0+numeric]);
break; break;
case 't': case 't':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
result=sispm_switch_toggle(udev,id,outlet); result = sispm_switch_toggle(udev,id,outlet);
if(verbose) printf("Toggled outlet %d %s\n",i,onoff[result]); if(verbose) printf("Toggled outlet %d %s\n",i,onoff[result]);
break; break;
case 'A': { case 'A': {
@ -411,22 +412,22 @@ static void parse_command_line(int argc, char* argv[], int count,
int optindsave = optind; int optindsave = optind;
int actionNo=0; int actionNo=0;
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
time( &date ); time( &date );
timeStamp_tm = localtime(&date); timeStamp_tm = localtime(&date);
lastEventTime = ((ulong)(date/60))*60; // round to previous minute lastEventTime = ((ulong)(date / 60)) * 60; // round to previous minute
plannif_reset (&plan); plannif_reset (&plan);
plan.socket = outlet; plan.socket = outlet;
plan.timeStamp = date; plan.timeStamp = date;
plan.actions[0].switchOn = 0; plan.actions[0].switchOn = 0;
const struct option opts[] = { const struct option opts[] = {
{ "Ado", 1, NULL, 'd' }, {"Ado", 1, NULL, 'd'},
{ "Aafter", 1, NULL, 'a' }, {"Aafter", 1, NULL, 'a'},
{ "Aat", 1, NULL, '@' }, {"Aat", 1, NULL, '@'},
{ "Aloop", 1, NULL, 'l' }, {"Aloop", 1, NULL, 'l'},
{ NULL, 0, 0, 0 } {NULL, 0, 0, 0}
}; };
// scan long options and store in plan+loop variables // scan long options and store in plan+loop variables
@ -442,8 +443,7 @@ static void parse_command_line(int argc, char* argv[], int count,
} }
switch (opt) { switch (opt) {
case 'd': case 'd':
plan.actions[actionNo+1].switchOn = (strcmp(optarg, "on") plan.actions[actionNo+1].switchOn = !strcmp(optarg, "on");
== 0 ? 1 : 0);
break; break;
case 'a': case 'a':
plan.actions[actionNo].timeForNext = atol(optarg); plan.actions[actionNo].timeForNext = atol(optarg);
@ -456,23 +456,25 @@ static void parse_command_line(int argc, char* argv[], int count,
strptime(optarg, "%Y-%m-%d %H:%M", &tm); strptime(optarg, "%Y-%m-%d %H:%M", &tm);
time4next = mktime(&tm); time4next = mktime(&tm);
if (time4next > lastEventTime) if (time4next > lastEventTime)
plan.actions[actionNo].timeForNext = (time4next - lastEventTime) / 60; plan.actions[actionNo].timeForNext =
(time4next - lastEventTime) / 60;
else else
plan.actions[actionNo].timeForNext = 0; plan.actions[actionNo].timeForNext = 0;
break; break;
} }
default: default:
fprintf(stderr,"Unknown Option: %s\nTerminating\n",argv[optind-1]); fprintf(stderr, "Unknown Option: %s\nTerminating\n",
argv[optind-1]);
exit(-7); exit(-7);
break; break;
} }
if (plan.actions[actionNo].timeForNext == 0) { if (plan.actions[actionNo].timeForNext == 0) {
fprintf(stderr,"Incorrect Date: %s\nTerminating\n",optarg); fprintf(stderr, "Incorrect Date: %s\nTerminating\n", optarg);
exit(-7); exit(-7);
} }
if (plan.actions[actionNo].timeForNext != -1 if (plan.actions[actionNo].timeForNext != -1
&& plan.actions[actionNo+1].switchOn != -1) { && plan.actions[actionNo + 1].switchOn != -1) {
lastEventTime += 60 * plan.actions[actionNo].timeForNext; lastEventTime += 60 * plan.actions[actionNo].timeForNext;
++actionNo; ++actionNo;
} }
@ -497,54 +499,54 @@ static void parse_command_line(int argc, char* argv[], int count,
usb_command_setplannif(udev, &plan); usb_command_setplannif(udev, &plan);
if(verbose) { if(verbose) {
plannif_reset (&plan); plannif_reset (&plan);
usb_command_getplannif(udev,outlet,&plan); usb_command_getplannif(udev, outlet, &plan);
plannif_display(&plan, 0, NULL); plannif_display(&plan, 0, NULL);
} }
if (i<upto) if (i < upto)
optind = optindsave; // reset for next device if needed optind = optindsave; // reset for next device if needed
break; break;
} }
case 'a': case 'a':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
struct plannif plan; struct plannif plan;
plannif_reset (&plan); plannif_reset (&plan);
usb_command_getplannif(udev,outlet,&plan); usb_command_getplannif(udev,outlet,&plan);
plannif_display(&plan, verbose, argv[0]); plannif_display(&plan, verbose, argv[0]);
break; break;
case 'g': case 'g':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
result=sispm_switch_getstatus(udev,id,outlet); result = sispm_switch_getstatus(udev,id,outlet);
if(verbose) printf("Status of outlet %d:\t",i); if(verbose) printf("Status of outlet %d:\t",i);
printf("%s\n",onoff[ result +numeric]); printf("%s\n",onoff[ result +numeric]);
break; break;
case 'm': case 'm':
outlet=check_outlet_number(id, i); outlet = check_outlet_number(id, i);
result=sispm_get_power_supply_status(udev,id,outlet); result = sispm_get_power_supply_status(udev,id,outlet);
if(verbose) printf("Power supply status is:\t"); if(verbose) printf("Power supply status is:\t");
//take bit 1, which gives the relais status //take bit 1, which gives the relais status
printf("%s\n",onoff[ result +numeric]); printf("%s\n",onoff[ result +numeric]);
break; break;
#ifndef WEBLESS #ifndef WEBLESS
case 'p': case 'p':
listenport=atoi(optarg); listenport = atoi(optarg);
if(verbose) printf("Server will listen on port %d.\n",listenport); if(verbose) printf("Server will listen on port %d.\n",listenport);
break; break;
case 'u': case 'u':
homedir=strdup(optarg); homedir = strdup(optarg);
if(verbose) printf("Web pages come from \"%s\".\n",homedir); if(verbose) printf("Web pages come from \"%s\".\n",homedir);
break; break;
case 'i': case 'i':
bindaddr=optarg; bindaddr = optarg;
if (verbose) printf("Web server will bind on interface with IP %s\n", if (verbose) printf("Web server will bind on interface with IP %s\n",
bindaddr); bindaddr);
break; break;
case 'l': case 'l':
case 'L': { case 'L': {
int* s; int *s;
openlog("sispmctl", LOG_PID, LOG_INFO); openlog("sispmctl", LOG_PID, LOG_INFO);
read_password(); read_password();
if (verbose) if (verbose)
printf("Server goes to listen mode now.\n"); printf("Server goes to listen mode now.\n");
@ -559,18 +561,18 @@ static void parse_command_line(int argc, char* argv[], int count,
} }
#endif #endif
case 'q': case 'q':
verbose=1-verbose; verbose = 1 - verbose;
break; break;
case 'n': case 'n':
numeric=2-numeric; numeric = 2 - numeric;
break; break;
case 'b': case 'b':
if (strncmp(optarg,"on",strlen("on"))==0) { if (!strncmp(optarg, "on", strlen("on"))) {
sispm_buzzer_on(udev); sispm_buzzer_on(udev);
if(verbose) printf("Turned buzzer %s\n",onoff[1+numeric]); if(verbose) printf("Turned buzzer %s\n", onoff[1 + numeric]);
} else if (strncmp(optarg,"off",strlen("off"))==0) { } else if (!strncmp(optarg,"off", strlen("off"))) {
sispm_buzzer_off(udev); sispm_buzzer_off(udev);
if(verbose) printf("Turned buzzer %s\n",onoff[0+numeric]); if(verbose) printf("Turned buzzer %s\n", onoff[numeric]);
} }
break; break;
case 'v': case 'v':
@ -578,7 +580,7 @@ static void parse_command_line(int argc, char* argv[], int count,
break; break;
case '?': case '?':
case 'h': case 'h':
print_usage( argv[0] ); print_usage(argv[0]);
exit(1); exit(1);
default: default:
fprintf(stderr,"Unknown Option: %c(%x)\nTerminating\n",c,c); fprintf(stderr,"Unknown Option: %c(%x)\nTerminating\n",c,c);
@ -587,7 +589,7 @@ static void parse_command_line(int argc, char* argv[], int count,
} // loop through devices } // loop through devices
} // loop through options } // loop through options
if (udev!=NULL) { if (udev) {
usb_close(udev); usb_close(udev);
udev = NULL; udev = NULL;
} }
@ -595,7 +597,7 @@ static void parse_command_line(int argc, char* argv[], int count,
} }
int main(int argc, char** argv) int main(int argc, char *argv[])
{ {
struct usb_bus *bus; struct usb_bus *bus;
struct usb_device *dev, *usbdev[MAXGEMBIRD], *usbdevtemp; struct usb_device *dev, *usbdev[MAXGEMBIRD], *usbdevtemp;
@ -603,7 +605,7 @@ int main(int argc, char** argv)
int count=0, found = 0, i=1; int count=0, found = 0, i=1;
#ifndef MSG_NOSIGNAL #ifndef MSG_NOSIGNAL
(void) signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif #endif
memset(usbdev,0,sizeof(usbdev)); memset(usbdev,0,sizeof(usbdev));
@ -613,9 +615,9 @@ int main(int argc, char** argv)
usb_find_devices(); usb_find_devices();
// initialize by setting device pointers to zero // initialize by setting device pointers to zero
for (count=0; count < MAXGEMBIRD; ++count) for (count = 0; count < MAXGEMBIRD; ++count)
usbdev[count]=NULL; usbdev[count] = NULL;
count=0; count = 0;
//first search for GEMBIRD (m)SiS-PM devices //first search for GEMBIRD (m)SiS-PM devices
for (bus = usb_busses; bus; bus = bus->next) { for (bus = usb_busses; bus; bus = bus->next) {
@ -641,11 +643,11 @@ int main(int argc, char** argv)
if (count > 1) { if (count > 1) {
do { do {
found = 0; found = 0;
for (i=1; i< count; ++i) { for (i = 1; i < count; ++i) {
if (usbdev[i]->devnum < usbdev[i-1]->devnum) { if (usbdev[i]->devnum < usbdev[i - 1]->devnum) {
usbdevtemp = usbdev[i]; usbdevtemp = usbdev[i];
usbdev[i] = usbdev[i-1]; usbdev[i] = usbdev[i - 1];
usbdev[i-1] = usbdevtemp; usbdev[i - 1] = usbdevtemp;
found = 1; found = 1;
} }
} }
@ -653,7 +655,7 @@ int main(int argc, char** argv)
} }
/* get serial number of each device */ /* get serial number of each device */
for (i=0; i < count; ++i) { for (i = 0; i < count; ++i) {
usb_dev_handle *sudev = NULL; usb_dev_handle *sudev = NULL;
sudev = get_handle(usbdev[i]); sudev = get_handle(usbdev[i]);
@ -675,7 +677,7 @@ int main(int argc, char** argv)
if (argc <= 1) if (argc <= 1)
print_usage(argv[0]); print_usage(argv[0]);
else else
parse_command_line(argc,argv,count,usbdev,usbdevsn); parse_command_line(argc, argv, count, usbdev, usbdevsn);
return 0; return 0;
} }