summaryrefslogtreecommitdiff
path: root/util/isensor.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2016-05-08 23:32:28 +0200
committerJörg Frings-Fürst <debian@jff-webhosting.net>2016-05-08 23:32:28 +0200
commit55adddbc16f65732b57ab8585c47001fced91d77 (patch)
tree27e8239d48c8a6b353db356aff92ee4d225c0a0e /util/isensor.c
parentd3c90cdcdf56146af89056ed36bfcc7b4d4956c5 (diff)
Imported Upstream version 2.9.9upstream/2.9.9
Diffstat (limited to 'util/isensor.c')
-rw-r--r--util/isensor.c193
1 files changed, 108 insertions, 85 deletions
diff --git a/util/isensor.c b/util/isensor.c
index 6aaf3fb..a8694f9 100644
--- a/util/isensor.c
+++ b/util/isensor.c
@@ -247,7 +247,7 @@ char *decode_entity_id(int id) {
* Global Data
************************/
static char *progname = "isensor";
-static char *progver = "2.98";
+static char *progver = "2.99";
#ifdef WIN32
static char savefile[] = "%ipmiutildir%\\thresholds.cmd";
#else
@@ -277,6 +277,7 @@ static int fgetmem = 0;
static int fprivset = 0;
static char fremote = 0;
static int nloops = 1; /* num times to show repeated sensor readings */
+static int loopsec = 1; /* wait N sec between loops, default 1 */
static char bdelim = BDELIM; /* delimiter for canonical output */
static char tmpstr[20]; /* temp string */
static char *binfile = NULL;
@@ -1298,7 +1299,8 @@ int GetSDR(int r_id, int *r_next, uchar *recdata, int srecdata, int *rlen)
if (sresp >= 2) thislen = sresp - 2;
else thislen = 0;
reclen = off + thislen; /* truncate, stop reading */
- fprintf(stderr,"SDR record %x is malformed, length %d is less than minimum %d\n",sresp,thislen+2);
+ fprintf(stderr,"SDR record %x is malformed, length %d is less than minimum %d\n",r_id,sresp,thislen+2);
+ rc = ERR_SDR_MALFORMED;
}
/* successful */
memcpy(&resp[off],&respchunk[2],thislen);
@@ -1342,7 +1344,7 @@ int get_sdr_file(char *sdrfile, uchar **sdrlist)
FILE *fp = NULL;
int i, n, num, nsdr, isdr, len;
uchar *sdrbuf;
- uchar buff[255];
+ char buff[255];
uchar hbuf[85];
char fvalid;
@@ -1468,7 +1470,7 @@ int find_nsdrs(uchar *pcache)
ulong asz = 0;
int i, len;
uchar *sdr;
- ushort recid;
+ ushort recid = 0;
if (pcache == NULL) return(num);
for (i = 0; (int)asz < sz_sdrs; i++)
@@ -1559,7 +1561,7 @@ int find_sdr_by_tag(uchar *psdr, uchar *pcache, char *tag, uchar dbg)
printf("sdr[%d] idx=%02x%02x num=%x tag: %s\n",i,sdr[1],sdr[0],
sdr[7],tmp);
}
- if (strncmp(tag,&sdr[k],n) == 0) {
+ if (strncmp(tag,(char *)&sdr[k],n) == 0) {
memcpy(psdr,sdr,len);
return(0);
}
@@ -2499,7 +2501,7 @@ ShowSDR(char *tag, uchar *sdr)
idstr[ilen] = 0; /* stringify */
if ((sdr01->sens_capab & 0x40) == 0) brearm = 'm'; /*manual rearm*/
else brearm = 'a'; /*automatic rearm*/
- if (fdebug) printf("entity %d.%d, idlen=%d sizeof=%d idstr0=%c s0=%x\n",
+ if (fdebug) printf("entity %d.%d, idlen=%d sizeof=%lu idstr0=%c s0=%x\n",
sdr01->entity_id, sdr01->entity_inst,
ilen,sizeof(SDR01REC),idstr[0],sdr[ioff]);
rc = GetSensorReading(sdr01->sens_num,sdr01,sens);
@@ -2508,7 +2510,7 @@ ShowSDR(char *tag, uchar *sdr)
val = 0;
if (rc == 0xCB) { /*sensor not present*/
i = 10; /* Absent */
- typestr = "na";
+ typestr = "na";
} else typestr = decode_rv(rc);
} else {
j = (sens[2] & 0x3f); /*sensor reading state*/
@@ -2518,6 +2520,10 @@ ShowSDR(char *tag, uchar *sdr)
sens[2],i,sens[0],sens[1],sdr01->sens_base,
sdr01->sens_units);
if ((sens[1] & 0x20) != 0) { i = 7; val = 0; } /* Init state */
+ else if (sens[2] == 0xc7) { i = 10; val = 0; /* Absent (Intel) */
+ if (fdebug) printf("sensor[%x] is absent (c7), no reading\n",
+ sdr01->sens_num);
+ }
else val = RawToFloat(sens[0],sdr);
typestr = get_unit_type(sdr01->sens_units, sdr01->sens_base,
sdr01->sens_mod, fsimple);
@@ -2616,7 +2622,7 @@ ShowSDR(char *tag, uchar *sdr)
idstr[ilen] = 0; /* stringify */
if ((sdr02->sens_capab & 0x40) == 0) brearm = 'm'; /*manual rearm*/
else brearm = 'a'; /*automatic rearm*/
- if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%d, s0=%x\n",
+ if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%lu, s0=%x\n",
ilen,idstr[0],sizeof(SDR02REC),sdr[ioff]);
memset(sens,0,sizeof(sens));
rc = GetSensorReading(sdr02->sens_num,sdr02,sens);
@@ -2792,7 +2798,7 @@ ShowSDR(char *tag, uchar *sdr)
if (ilen >= sizeof(idstr)) ilen = sizeof(idstr) - 1;
memcpy(idstr,&sdr[ioff],ilen);
idstr[ilen] = 0; /* stringify */
- if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%d, s0=%x\n",
+ if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%lu, s0=%x\n",
ilen,idstr[0],sizeof(SDR11REC),sdr[ioff]);
printf("%s", tag);
if (fsimple)
@@ -2822,7 +2828,7 @@ ShowSDR(char *tag, uchar *sdr)
if (ilen >= sizeof(idstr)) ilen = sizeof(idstr) - 1;
memcpy(idstr,&sdr[ioff],ilen);
idstr[ilen] = 0; /* stringify */
- if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%d, s0=%x\n",
+ if (fdebug) printf("ilen=%d, istr0=%c, sizeof=%lu, s0=%x\n",
ilen,idstr[0],sizeof(SDR12REC),sdr[ioff]);
printf("%s", tag);
if (fsimple)
@@ -2986,6 +2992,31 @@ static int get_filesize(char *fileName, ulong *psize)
}
#endif
+int write_sdr_binfile(char *binfile)
+{
+ uchar *pbuf = NULL;
+ FILE *fp;
+ int len, ret;
+ ret = get_sdr_cache(&pbuf); /* sets nsdrs, sz_sdrs */
+ if (ret == 0) {
+ fp = fopen(binfile,"wb");
+ if (fp == NULL) {
+ ret = get_LastError();
+ printf("Cannot open file %s for writing, error %d\n",binfile,ret);
+ } else {
+ printf("Writing SDR size %d to %s ...\n",sz_sdrs,binfile);
+ len = (int)fwrite(pbuf, 1, sz_sdrs, fp);
+ fclose(fp);
+ if (len <= 0) {
+ ret = get_LastError();
+ printf("Error %d writing file %s\n",ret,binfile);
+ } else ret = 0;
+ }
+ free_sdr_cache(pbuf);
+ }
+ return(ret);
+}
+
int read_sdr_binfile(char *binfile, uchar **pbufret, int *buflen)
{
uchar *pbuf = NULL;
@@ -3006,9 +3037,9 @@ int read_sdr_binfile(char *binfile, uchar **pbufret, int *buflen)
#endif
fp = fopen(binfile,"rb");
if (fp == NULL) {
- ret = get_LastError();
- printf("Cannot open file %s, error %d\n",binfile,ret);
- return(ret);
+ ret = get_LastError();
+ printf("Cannot open file %s, error %d\n",binfile,ret);
+ return(ret);
}
fseek(fp, 0L, SEEK_SET);
#ifndef WIN32
@@ -3030,29 +3061,29 @@ int read_sdr_binfile(char *binfile, uchar **pbufret, int *buflen)
pbuf = malloc(len);
if (fdebug) printf("sdr_binfile: malloc(%d) pbuf=%p\n",len,pbuf);
if (pbuf == NULL) {
- ret = -1;
- fclose(fp);
- return(ret);
+ ret = -2;
+ fclose(fp);
+ return(ret);
}
psdrcache = pbuf;
/*ok, so proceed with restore*/
ret = 0;
len = (int)fread(pbuf, 1, sz_sdrs, fp);
if (len <= 0) {
- ret = get_LastError();
- printf("Error %d reading file %s\n",ret,binfile);
- sz_sdrs = 0; /*for safety*/
+ ret = get_LastError();
+ printf("Error %d reading file %s\n",ret,binfile);
+ sz_sdrs = 0; /*for safety*/
} else if (len < sz_sdrs) {
- /* Show error if this happens in Windows */
- ret = get_LastError();
- printf("truncated fread(%s): attempted %d, got %d, error %d\n",
- binfile,sz_sdrs,len,ret);
+ /* Show error if this happens in Windows */
+ ret = get_LastError();
+ printf("truncated fread(%s): attempted %d, got %d, error %d\n",
+ binfile,sz_sdrs,len,ret);
ret = 0; /*try to keep going*/
}
fclose(fp);
if (fdebug) {
- printf("SDR buffer from file (len=%d,sz=%d)\n",len,sz_sdrs);
- dump_buf("SDR buffer",pbuf,len,1);
+ printf("SDR buffer from file (len=%d,sz=%d)\n",len,sz_sdrs);
+ dump_buf("SDR buffer",pbuf,len,1);
}
*pbufret = pbuf;
*buflen = len;
@@ -3086,7 +3117,7 @@ int i_sensor(int argc, char **argv)
printf("%s: version %s\n",progname,progver);
- while ( (c = getopt( argc, argv,"a:bcd:ef:g:h:i:j:l:m:n:opqrstu:vwxT:V:J:L:EYF:P:N:R:U:Z:?")) != EOF )
+ while ( (c = getopt( argc, argv,"a:bcd:ef:g:h:i:j:k:l:m:n:opqrstu:vwxT:V:J:L:EYF:P:N:R:U:Z:?")) != EOF )
switch(c) {
case 'a': /* reArm sensor number N */
if (strncmp(optarg,"0x",2) == 0) frearm = htoi(&optarg[2]);
@@ -3116,11 +3147,7 @@ int i_sensor(int argc, char **argv)
break;
case 'j': fjumpstart = 1; /* Load SDR cache from a file*/
binfile = optarg; break;
- case 't': fshowthr = 1; break;
- case 'v': fshowthr = 1; sens_verbose = 1; break;
- case 'p': fsavethresh = 1; break;
- case 'q': fshowthr = 2; fwrap = 1; break;
- case 'r': frawsdr = 1; break;
+ case 'k': loopsec = atoi(optarg); break; /*N sec between loops*/
case 'm': /* specific MC, 3-byte address, e.g. "409600" */
g_bus = htoi(&optarg[0]); /*bus/channel*/
g_sa = htoi(&optarg[2]); /*device slave address*/
@@ -3161,6 +3188,11 @@ int i_sensor(int argc, char **argv)
fsetthresh = 2; /*indicates float conversion*/
}
break;
+ case 'p': fsavethresh = 1; break;
+ case 'q': fshowthr = 2; fwrap = 1; break;
+ case 'r': frawsdr = 1; break;
+ case 't': fshowthr = 1; break;
+ case 'v': fshowthr = 1; sens_verbose = 1; break;
case 'u': /* specify unique thresholds in hex or float */
/* raw hex format: 0xLNLCLUHNHCHU, all 6 required */
if (strncmp(optarg,"0x",2) == 0) { /*raw hex thresholds*/
@@ -3246,8 +3278,9 @@ int i_sensor(int argc, char **argv)
// printf(" -f file Restore SDRs from a binary dump file\n");
printf(" -g fan show only this sensor type group\n");
printf(" -h tval specifies the Highest threshold to set\n");
- printf(" -i id only show these sensor ids\n");
+ printf(" -i id only show these sensor id numbers\n");
printf(" -j file Jump-start SDR cache from a binary file\n");
+ printf(" -k K If -L, wait K sec between loops (default=1)\n");
printf(" -l tval specifies the Lowest threshold to set\n");
printf(" -m002000 specific MC (bus 00,sa 20,lun 00)\n");
printf(" -n snum specifies the sensor Number to set hi/lo\n");
@@ -3260,7 +3293,7 @@ int i_sensor(int argc, char **argv)
printf(" -u thr set Unique threshold values (e.g. 3:2:1:48:49:50)\n");
printf(" -v Verbose: thresholds, max/min, hysteresis\n");
printf(" -w Wrap thresholds on sensor line\n");
- printf(" -L n Loop n times\n");
+ printf(" -L n Loop n times every k seconds (default k=1)\n");
print_lan_opt_usage(0);
ret = ERR_USAGE;
goto do_exit;
@@ -3374,26 +3407,7 @@ int i_sensor(int argc, char **argv)
}
if (fdump) {
- uchar *pbuf = NULL;
- FILE *fp;
- int len;
- ret = get_sdr_cache(&pbuf);
- if (ret == 0) {
- fp = fopen(binfile,"wb");
- if (fp == NULL) {
- ret = get_LastError();
- printf("Cannot open file %s, error %d\n",binfile,ret);
- } else {
- printf("Writing SDR size %d to %s ...\n",sz_sdrs,binfile);
- len = (int)fwrite(pbuf, 1, sz_sdrs, fp);
- fclose(fp);
- if (len <= 0) {
- ret = get_LastError();
- printf("Error %d writing file %s\n",ret,binfile);
- } else ret = 0;
- }
- free_sdr_cache(pbuf);
- }
+ ret = write_sdr_binfile(binfile);
goto do_exit;
} /*endif fdump*/
@@ -3405,25 +3419,25 @@ int i_sensor(int argc, char **argv)
ret = read_sdr_binfile(binfile,&pbuf,&slen);
if (ret == 0) { /*successful, so write SDRs */
- nsdrs = find_nsdrs(pbuf);
- printf("Ready to restore %d SDRs\n",nsdrs);
- set_reserve(1);
- ret = sdr_clear_repo(fdevsdrs);
- if (ret != 0) {
- printf("SDR Clear Repository error %d\n",ret);
- goto do_exit;
- }
- id = 0;
- while(find_sdr_next(sdr,pbuf,id) == 0) {
- id = sdr[0] + (sdr[1] << 8);
- if (fdebug) printf("adding SDR[%x]\n",id);
- set_reserve(1);
- ret = sdr_add_record(sdr,fdevsdrs);
- if (ret != 0) {
- printf("SDR[%x] add error %d\n",id,ret);
- break;
- }
- } /*end while sdr*/
+ nsdrs = find_nsdrs(pbuf);
+ printf("Ready to restore %d SDRs\n",nsdrs);
+ set_reserve(1);
+ ret = sdr_clear_repo(fdevsdrs);
+ if (ret != 0) {
+ printf("SDR Clear Repository error %d\n",ret);
+ goto do_exit;
+ }
+ id = 0;
+ while(find_sdr_next(sdr,pbuf,id) == 0) {
+ id = sdr[0] + (sdr[1] << 8);
+ if (fdebug) printf("adding SDR[%x]\n",id);
+ set_reserve(1);
+ ret = sdr_add_record(sdr,fdevsdrs);
+ if (ret != 0) {
+ printf("SDR[%x] add error %d\n",id,ret);
+ break;
+ }
+ } /*end while sdr*/
}
if (ret == 0) printf("Restored %d SDRs successfully.\n",nsdrs);
free_sdr_cache(pbuf); /* does nothing if (pbuf == NULL) */
@@ -3434,12 +3448,19 @@ int i_sensor(int argc, char **argv)
uchar *pbuf = NULL;
int slen;
ret = read_sdr_binfile(binfile,&pbuf,&slen);
- if (ret != 0) fjumpstart = 0; /* use normal method if error*/
- else { /* set this as the SDR cache */
- psdrcache = pbuf;
- sz_sdrs = slen;
- nsdrs = find_nsdrs(pbuf);
- if (fdebug) printf("jumpstart cache: nsdrs=%d size=%d\n",nsdrs,slen);
+ if (ret != 0) {
+ /* Try to dump sdrs to this file if not there */
+ ret = write_sdr_binfile(binfile);
+ if (ret == 0)
+ ret = read_sdr_binfile(binfile,&pbuf,&slen);
+ if (ret != 0) {
+ fjumpstart = 0; /*cannot do jumpstart*/
+ }
+ } else { /* set this as the SDR cache */
+ psdrcache = pbuf;
+ sz_sdrs = slen;
+ nsdrs = find_nsdrs(pbuf);
+ if (fdebug) printf("jumpstart cache: nsdrs=%d size=%d\n",nsdrs,slen);
}
} /*endif fjumpstart*/
@@ -3493,9 +3514,9 @@ int i_sensor(int argc, char **argv)
printf("GetSDR[%04x]: ret = %x, next=%x\n",recid,ret,recnext);
if (ret != 0) {
if (ret > 0) { /* ret is a completion code error */
- fprintf(stderr,"%04x GetSDR error 0x%02x %s, rlen=%d\n",
+ fprintf(stderr,"%04x GetSDR error 0x%02x %s, rlen=%d\n",
recid,ret,decode_cc((ushort)0,(uchar)ret),sz);
- if (ret == 0xC5) { /* lost Reservation ID, retry */
+ if (ret == 0xC5) { /* lost Reservation ID, retry */
/* This means that some other IPMI software has
* requested a Reservation before we finished, so
* we need to refresh the Reservation ID * retry. */
@@ -3504,11 +3525,12 @@ int i_sensor(int argc, char **argv)
if (fdebug)
printf("GetSDR[%04x]: ret = %x, next=%x\n",recid,ret,
recnext);
- }
+ if (ret == 0xC5) sz = 0; /*failed to get Reservation ID*/
+ }
} else fprintf(stderr,"%04x GetSDR error %d, rlen = %d\n",
- recid,ret,sz);
+ recid,ret,sz);
if (sz < MIN_SDR_SZ) { /* don't have recnext, so abort */
- break;
+ break;
} /* else fall through & continue */
}
} /*end-else*/
@@ -3662,6 +3684,7 @@ int i_sensor(int argc, char **argv)
} /*endif ok, got full SDR */
NextSdr:
+ if (ret == ERR_SDR_MALFORMED) break;
if (fjumpstart) recid = recnext;
else {
if (recnext == recid) recid = 0xffff; /*break;*/
@@ -3674,8 +3697,8 @@ NextSdr:
irec++;
} /*end while recid*/
if (fdoloop && (nloops > 1)) {
- printf("\n"); /* output an empty separator line */
- os_usleep(1,0); /*delay 1 sec between loops*/
+ printf("\n"); /* output an empty separator line */
+ os_usleep(loopsec,0); /*delay 1 sec between loops*/
}
} /*end for nloops*/