Code:
void getValues() {
char line[100];
remove("/tmp/value.txt");
system(leftCmd);
system(rightCmd);
system(ledCmd);
lv1=lv2;
rv1=rv2;
lev1=lev2;
fr = fopen ("/tmp/value.txt", "r");
fgets(line, 80, fr) ;
sscanf (line, "%llu", &lv2);
fgets(line, 80, fr) ;
sscanf (line, "%llu", &rv2);
fgets(line, 80, fr) ;
sscanf (line, "%llu", &lev2);
fclose(fr);
}
void getConfig ()
{
FILE *fc;
char key[100];
char value[200];
char row[1024];
char *p;
fc=fopen("current.cfg","r");
while (!feof(fc) ){
fgets(row,200,fc);
if (strlen(row)<4) continue;
if( sscanf(row,"%[^=]=%[^=\n]",key,value)==2 ) {
if (strlen(key)<4) continue;
if (strcmp (key,"sleepTime")==0) sleepTime=strtol(value,&p, 10);
if (strcmp(key,"leftComunity")==0) strcpy(leftComunity,value);
if (strcmp(key,"leftIP")==0) strcpy(leftIP,value);
if (strcmp(key,"leftMIB")==0) strcpy(leftMIB,value);
if (strcmp(key,"rightComunity")==0) strcpy(rightComunity,value);
if (strcmp(key,"rightIP")==0) strcpy(rightIP,value);
if (strcmp(key,"rightMIB")==0) strcpy(rightMIB,value);
if (strcmp(key,"ledComunity")==0) strcpy(ledComunity,value);
if (strcmp(key,"ledIP")==0) strcpy(ledIP,value);
if (strcmp(key,"ledMIB")==0) strcpy(ledMIB,value);
if (strcmp(key,"leftMax")==0) leftMax=strtol (value, &p, 10);
if (strcmp(key,"rightMax")==0) rightMax=strtol (value, &p, 10);
if (strcmp(key,"ledMax")==0) ledMax=strtol (value, &p, 10);
}
}
fclose(fc);
sprintf(leftCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", leftComunity,leftIP,leftMIB);
sprintf(rightCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", rightComunity,rightIP,rightMIB);
sprintf(ledCmd,"snmpget -v 1 -c \"%s\" %s %s | awk '{print $4 >> \"/tmp/value.txt\" }'", ledComunity,ledIP,ledMIB);
}
int main(int argc, char ** argv)
{
int difference;
//struct ntptimeval start, elapsed;
struct usb_dev_handle * perf_fs_usb = usb_perf_fs_usb_open();
getConfig();
printf("Starting monitoring:\n");
ntp_gettime(&start);
getValues();
while(1) {
usleep(sleepTime);
getValues();
ntp_gettime(&elapsed);
difference=difference_micro(&start, &elapsed) ;
ntp_gettime(&start);
left=umin((lv2-lv1)*800/difference/leftMax ,100);
right=umin((rv2-rv1)*800/difference/rightMax ,100);
led=umin((lev2-lev1)*800/difference/ledMax ,100);
printf ("sleep %i ",difference);
printf ("display: %i %i %i\n",right,left, led);
writeUSB(perf_fs_usb,right,left,led);
}