src.dualinventive.com/dinet/libdi-php/tests/can_rpc_rt_device_ping.php

59 lines
1.5 KiB
PHP

<?php
// fail writes the msg to stderr and exit the application with code 1
function fail($msg) {
fwrite(STDERR, "$msg\n");
exit(1);
}
/* Publish rpc device ping as RT message */
$fd = di_can_open("vcan0");
$fd2 = di_can_open("vcan0");
di_can_set_nodeid($fd, "00000000000000000000000000000001");
di_can_set_nodeid($fd2, "00000000000000000000000000000001");
$msg = [];
$msg['rt'] = true;
$msg['msgtype'] = DI_CAN_MSGTYPE_RPC;
$msg['ttype'] = DI_CAN_TRANSFERTYPE_PUBLISH;
$msg['dtype'] = DI_RPC_TYPE_DEVICE_PING;
$msg['dst_id'] = DI_CAN_NODEID_BROADCAST;
// Sending RT messages must result in an error
$ret = di_can_send($fd, $msg);
if ($ret !== false) {
fail("di_can_send returned !== false");
}
// Send RT message with a payload
$msg['ptype'] = DI_CAN_PTYPE_STRING;
$msg['msg'] = "hello RT message!";
for ($i = 0; $i < 10; $i++) {
$ret = di_can_send($fd, $msg);
if ($ret !== true) {
fail("di_can_send returned !== true");
}
}
// It takes some time to propagate all messages to the other filedescriptor
// and reassemble them
sleep(1);
// Receive and test send message for RT flag and correct Realtime Sequence Number
for ($i = 0; $i < 10; $i++) {
$rmsg = di_can_recv($fd2);
if ($rmsg === NULL) {
fail("di_can_recv returned NULL");
}
if (!isset($rmsg['rt'])) {
fail("rt property not set");
}
if (!isset($rmsg['rt:seqnr'])) {
fail("rt:seqnr property not set");
}
$seqnr = $rmsg["rt:seqnr"];
if ($seqnr != $i) {
fail("rt:seqnr invalid got: $seqnr, expect: $i");
}
}