#!/usr/bin/php -q $v) { if (strrpos($k, $prefix) !== false) { if ($value == $v) return $k; } } return ""; } if (!isset($argv[1])) { echo "argument expected" . PHP_EOL; echo "usage: di-candump " . PHP_EOL; exit(1); } $fd = di_can_open($argv[1]); if ($fd === null) { echo "error opening: '".$argv[1]."'" . PHP_EOL; echo "usage: di-candump " . PHP_EOL; exit(1); } declare(ticks=1); function sig_handler($signo) { echo "Got signal $signo...\n"; di_can_close($GLOBALS['fd']); unset($GLOBALS['fd']); switch ($signo) { case SIGINT: exit; break; case SIGTERM: exit; break; case SIGHUP: break; default: } } pcntl_signal(SIGTERM, "sig_handler"); pcntl_signal(SIGHUP, "sig_handler"); pcntl_signal(SIGINT, "sig_handler"); while (1) { $x = di_can_recv($fd); if ($x === null) continue; // Unpack MessagePack if (isset($x['msg']) && $x['msg'] !== null) { if ($x['ptype'] == DI_CAN_PTYPE_MSGPACK) $x['msg'] = msgpack_unpack($x['msg']); if ($x['msgtype'] == DI_CAN_MSGTYPE_LOG) { $x['msg']['loglevel'] = to_str("DI_LOG_LEVEL_", $x['msg']['loglevel']); $x['msg']['component'] = to_str("DI_LOG_COMPONENT_", $x['msg']['component']); } } // Convert msg if ($x['msgtype'] == DI_CAN_MSGTYPE_RAW && $x['dtype'] == DI_CAN_RAW_DTYPE_CLOUDLIGHT_STATE) { $x['msg'] = to_str("DI_DEVICE_CLOUDLIGHT_STATE_", $x['msg']); } // Convert dtype if ($x['msgtype'] == DI_CAN_MSGTYPE_RAW) { $x['dtype'] = to_str("DI_CAN_RAW_DTYPE_", $x['dtype']); $dtype = strtolower(str_replace("DI_CAN_RAW_DTYPE_", "", $x['dtype'])); } else if ($x['msgtype'] == DI_CAN_MSGTYPE_RPC) { $x['dtype'] = to_str("DI_RPC_TYPE_", $x['dtype']); $dtype = strtolower(str_replace("DI_RPC_TYPE_", "", $x['dtype'])); $dtype = str_replace("_", ":", $dtype); } else if ($x['msgtype'] == DI_CAN_MSGTYPE_NET) { $x['dtype'] = to_str("DI_CAN_NET_DTYPE_", $x['dtype']); $dtype = strtolower(str_replace("DI_CAN_NET_DTYPE_", "", $x['dtype'])); } else if ($x['msgtype'] == DI_CAN_MSGTYPE_LOG) { $x['dtype'] = to_str("DI_LOG_LEVEL_", $x['dtype']); $dtype = strtolower(str_replace("DI_LOG_LEVEL_", "", $x['dtype'])); } else { $x['dtype'] = "unknown"; } // Convert ttype, msgtype and ptype to string if ($x['ttype'] == DI_CAN_TRANSFERTYPE_REQUEST) $ttype = "req"; else if ($x['ttype'] == DI_CAN_TRANSFERTYPE_REPLY) $ttype = "rep"; else if ($x['ttype'] == DI_CAN_TRANSFERTYPE_PUBLISH) $ttype = "pub"; if ($x['msgtype'] == DI_CAN_MSGTYPE_NET) $msgtype = "net"; else if ($x['msgtype'] == DI_CAN_MSGTYPE_RPC) $msgtype = "rpc"; else if ($x['msgtype'] == DI_CAN_MSGTYPE_RAW) $msgtype = "raw"; else if ($x['msgtype'] == DI_CAN_MSGTYPE_LOG) $msgtype = "log"; $ptype = strtolower(str_replace("DI_CAN_PTYPE_", "", to_str("DI_CAN_PTYPE_", $x['ptype']))); $rt = ""; if (isset($x['rt'])) $rt = "(rt seqnr " . $x['rt:seqnr'] . ") "; // Unset size/msg and ptype when there is no payload if ($x['size'] == 0) { unset($x['size']); unset($x['msg']); unset($x['ptype']); } else { if ($x['ptype'] == DI_CAN_PTYPE_MSGPACK) $msg = json_encode($x['msg']); else if ($x['ptype'] == DI_CAN_PTYPE_STRUCT) $msg = bin2hex($x['msg']); else if ($x['ptype'] == DI_CAN_PTYPE_DI_ERRNO) $msg = to_str("DNE_", $x['msg']); else $msg = strval($x['msg']); } // Convert src and dst node id to hex $src_id = dechex($x['src_id']); $src_id = '0x' . str_pad($src_id, 8, "0", STR_PAD_LEFT); $dst_id = dechex($x['dst_id']); $dst_id = '0x' . str_pad($dst_id, 8, "0", STR_PAD_LEFT); $t = microtime(true); $micro = sprintf("%06d",($t - floor($t)) * 1000000); $d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) ); $timestr = $d->format("Y-m-d H:i:s.u"); if (isset($x['size'])) { echo "[" . $timestr . "] ($src_id -> $dst_id) $rt$ttype $msgtype $dtype $ptype $msg". PHP_EOL; } else { echo "[" . $timestr . "] ($src_id -> $dst_id) $rt$ttype $msgtype $dtype". PHP_EOL; } unset($x); } ?>