#!/usr/bin/perl

%codes;
$startTime = -1;

$codes{"10"} = "NET_C_FE_MSG_POOL_EMPTY";
$codes{"11"} = "NET_C_FE_SEND_QUEUE_FULL";
$codes{"12"} = "NET_C_FE_NO_ROUTE";
$codes{"13"} = "NET_C_FE_SUBSEND_OFF";
$codes{"14"} = "NET_C_FE_SUBSEND_BUSY";
$codes{"15"} = "NET_C_FE_BAD_SENDDONE";
$codes{"16"} = "NET_C_FE_QENTRY_POOL_EMPTY";
$codes{"17"} = "NET_C_FE_SUBSEND_SIZE";
$codes{"18"} = "NET_C_FE_LOOP_DETECTED";
$codes{"19"} = "NET_C_FE_SEND_BUSY = 0x19";


$codes{"20"} = "NET_C_FE_SENT_MSG";
$codes{"21"} = "NET_C_FE_RCV_MSG";
$codes{"22"} = "NET_C_FE_FWD_MSG";
$codes{"23"} = "NET_C_FE_DST_MSG";
$codes{"24"} = "NET_C_FE_SENDDONE_FAIL";
$codes{"25"} = "NET_C_FE_SENDDONE_WAITACK";
$codes{"26"} = "NET_C_FE_SENDDONE_FAIL_ACK_SEND";
$codes{"27"} = "NET_C_FE_SENDDONE_FAIL_ACK_FWD";
$codes{"28"} = "NET_C_FE_DUPLICATE_CACHE";
$codes{"29"} = "NET_C_FE_DUPLICATE_QUEUE";
$codes{"2A"} = "NET_C_FE_CACHE_AT_SEND";
$codes{"2B"} = "NET_C_FE_CONGESTION_SENDWAIT";
$codes{"2C"} = "NET_C_FE_CONGESTION_BEGIN";
$codes{"2D"} = "NET_C_FE_CONGESTION_END";
$codes{"2E"} = "NET_C_FE_CONGESTED";
$codes{"30"} = "NET_C_TREE_NO_ROUTE";
$codes{"31"} = "NET_C_TREE_NEW_PARENT";
$codes{"32"} = "NET_C_TREE_ROUTE_INFO";
$codes{"33"} = "NET_C_TREE_SENT_BEACON";
$codes{"34"} = "NET_C_TREE_RCV_BEACON";

$codes{"40"} = "NET_C_DBG_1";
$codes{"41"} = "NET_C_DBG_2";
$codes{"42"} = "NET_C_DBG_3";

$codes{"50"} = "NET_C_FE_SENDQUEUE_EMPTY";
$codes{"51"} = "NET_C_FE_PUT_MSGPOOL_ERR";
$codes{"52"} = "NET_C_FE_PUT_QEPOOL_ERR";
$codes{"53"} = "NET_C_FE_GET_MSGPOOL_ERR";
$codes{"54"} = "NET_C_FE_GET_QEPOOL_ERR";


$codes{"60"} = "NET_C_LE_INSERT";       #:insert neighbor :addr
$codes{"61"} = "NET_C_LE_EVICT";       #:evict  neighbor :addr
$codes{"62"} = "NET_C_LE_NO_ROOM";     #:no room in table:addr
$codes{"63"} = "NET_C_LE_UPDATE";      #:entry update    :addr, quality, flags

$codes{"67"} = "TRIGGER_PARENT_CHANGE";

$codes{"6A"} = "UART_COMMAND_RECEIVED";

$codes{"70"} = "NET_C_FE_RCV_PULL";
$codes{"71"} = "NET_C_FE_SET_CONGESTED";
$codes{"72"} = "NET_C_FE_SET_WAS_CONGESTED";
$codes{"73"} = "NET_C_TREE_RCV_PULL";

$codes{"78"} = "NET_C_FE_QUEUE_SIZE";

$codes{"88"} = "NET_C_UART_FAIL";
$codes{"89"} = "NET_C_FAIL_SENDD";
$codes{"82"} = "NET_C_SENT_UART";

$codes{"99"} = "APPTIMER";

while (<>) {
  @bytes =  split(" ");
  unshift(@bytes, "0");
  $time = $bytes[0];
  $type = $bytes[8];
  $code = $bytes[9];
  if ($startTime == -1 && $time > 256) {
    $startTime = $time;
  }
  if ($type eq "16") {
#    $time -= $startTime;
    print "$time: ", $codes{"\U$code"}, "";

    if (($code eq "20") || ($code eq "21") || ($code eq "22") || ($code eq "24") || ($code eq "25")
	|| ($code eq "26") || ($code eq "27") || ($code eq "18")) {
	print " " . hex($bytes[12].$bytes[13]) . " " . hex($bytes[10].$bytes[11]) . " " . hex($bytes[14].$bytes[15]);
    }

    elsif (($code eq "33") || ($code eq "34")) {
#1176536400.307027 00 ff ff 00 00 09 00 16 34 00 0d 00 00 16 00 79 cf
#1176536401.271617 00 ff ff 00 00 09 00 16 33 00 0e 00 00 69 00 79 dd
#                0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17
	print " " . hex($bytes[10].$bytes[11]) . " " . hex($bytes[12]) . " " . hex("$bytes[13]$bytes[14]");
    }

    elsif ($code eq "78") {
	print " " . hex($bytes[11]);
    }

    elsif ($code eq "60" || $code eq "62" || $code eq "31") {
	print " " . hex($bytes[10].$bytes[11]);
    } 
    elsif ($code eq "63" || $code eq "61" || $code eq "67") {
	print " " . hex($bytes[10].$bytes[11]) . " " . hex($bytes[12].$bytes[13]) . " 0x". $bytes[15];
    }


    print " " . hex("$bytes[16]$bytes[17]") . " " . hex("$bytes[18]$bytes[19]$bytes[20]$bytes[21]");

    print "\n";

} else {
    print "$_";
}

}
