@echo off

:: SigChk.bat
::
:: show signal levels for associated devices at smartbridges access point
::
:: usage: SigChk.bat [deviceIP  [snmpRW_Community]]
::
:: blame: shardy@@methownet.com
::
:: uses: snmpget.exe/snmpset.exe from freeware ucd-snmp
::       sleep.exe from one of the reskits
::       perl

setlocal

:: see AT76C510.mib for definitions

set stationCtAddr=enterprises.410.1.2.5.1.0
set stationInfoAddr=enterprises.410.1.2.5.2.0
set apNameAddr=enterprises.410.1.2.1.10

set cmty=privateAP
set apIP=10.0.21.2

if not %2.==. (
   set apIP=%1&set cmty=%2
  ) else (
   if not %1.==. goto :eof
  )
set ct=
for /f %%a in (' snmpget -Oqv  %apIP% %cmty% %stationCtAddr% ') do (
  set ct=%%a
  )

for /l %%n in (1,1,%ct%) do (
  for /f %%a in ('perl -e "printf qw/%%02x/,%%n" ') do (
    call :clrAssocInfo
    for /f "tokens=*" %%b in (' snmpset -Oqv %apIP% %cmty% %stationInfoAddr% x "%%a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" ') do (
      call :appendAssocInfo %%b  
      )
    call :showAssocInfo
    )
  )

endlocal
goto :eof


:clrAssocInfo
  set assocInfo=
  goto :eof

:appendAssocInfo
  set assocInfo=%assocInfo% %*
  goto :eof

:showAssocInfo
  :: fixup
  set assocInfo=%assocInfo:"=%
  for /f "tokens=1-8*" %%m in ("%assocInfo%") do set assocInfo=%%m %%o:%%p:%%q:%%r:%%s:%%t %%u

  for /f "tokens=1-10*" %%m in ("%assocInfo%") do set assocInfo=%%m %%n %%o %%p %%q:%%r:%%s:%%t:%%u:%%v %%w

  for /f "tokens=1-11*" %%m in ("%assocInfo%") do (
    for /f "tokens=*" %%z in ('perl -e "sub min {my ($x1,$x2)=@_;($x1<$x2)?$x1:$x2;} sub max {my ($x1,$x2)=@_;($x1>$x2)?$x1:$x2;} printf q/%%m %%n %%o %%p %%q %%3d %%3d %%d.%%d.%%d.%%d/,min(100,int(100*0x%%r/44)) ,max(0,int(98-2*0x%%s)),0x%%t,0x%%u,0x%%v,0x%%w"') do (
    	set assocInfo=%%z
      )
    )
  echo %assocInfo%
  goto :eof