linux/spineldump

165 lines
2.7 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env php
<?php
function checkExit()
{
if (file_exists("/tmp/reloadspineldump")) {
die("Must reload...");
}
}
function mt()
{
return microtime(TRUE);
}
function dumpbyte($b)
{
if (ctype_print($b)) {
C::cyan($b);
} else {
C::white(bin2hex($b));
}
}
echo "Spinel debugger (STDIN)!\n";
include __DIR__ . "/lib/PhpSerial.php";
include __DIR__ . "/lib/Colors.php";
$serial = new PhpSerial;
// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
$serial->deviceSet("/dev/ttyUSB0");
// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(38400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$reqTimeout = 2;
$reqOpened = FALSE;
$reqPos = 0;
$rForm = 0;
$rLen = 999;
$rAddr = 0;
$rSig = 0;
$thrownAway = 0;
$mtStart = mt();
$mtEnd = mt();
// nekonečná smyčka
while (TRUE) {
// exit
checkExit();
// timeout
if ($reqOpened && (($mtStart + $reqTimeout) < mt())) {
C::red(C::BOLD . " Timeout reached... restarting transaction\n");
$reqOpened = FALSE;
}
// načtení bajtu
$str = $serial->readPort(1);
if (strlen($str) == 0) {
// nenačetl, tak pokračujeme
continue;
}
for ($i = 0; $i < strlen($str); $i++) {
$s = substr($str, $i, 1);
$thrownAway++;
if ((!$reqOpened) && ($s == '*')) {
C::nl();
C::green(C::BOLD . "*");
$reqOpened = TRUE;
$reqPos = 1;
$mtStart = mt();
$rForm = 0;
$rLen = 999;
$rAddr = 0;
continue;
}
if (!$reqOpened) {
continue;
}
switch ($reqPos) {
// format
case 1:
$rForm = ord($s);
C::lblue(C::BOLD . $rForm);
$reqPos++;
break;
// prvni byte delky
case 2:
$rLen = ord($s);
$rLen *= 256;
$reqPos++;
break;
// druhy byte delky
case 3:
$rLen += ord($s);
echo " L:";
C::yellow("$rLen");
$reqPos++;
break;
// adresa
case 4:
$rAddr = bin2hex($s);
echo " A:0x";
C::lred(C::BOLD . $rAddr);
$reqPos++;
break;
// signature
case 5:
$rSig = ord($s);
echo " S:";
C::green(C::BOLD . $rSig);
$reqPos++;
break;
default:
$reqPos++;
dumpbyte($s);
break;
}
if (ord($s) == 13) {
$reqOpened = FALSE;
$thrownAway = 0;
C::nl();
echo "Stats: exp:";
C::output($rLen, "purple");
echo " recv:";
C::output($reqPos - 4, 'light_green');
$fromLast = round(1000*($mtStart - $mtEnd), 2);
$mtEnd = mt();
$duration = round(1000*($mtEnd - $mtStart), 2);
echo " DIFF:";
C::output($fromLast . "ms", 'cyan');
echo " DT:";
C::output($duration . "ms", 'light_cyan');
C::nl();
}
}
}