107 lines
2.7 KiB
PHP
107 lines
2.7 KiB
PHP
<?php
|
|
require_once("./config.php");
|
|
|
|
class DNCM {
|
|
private $fd = null;
|
|
private $__CANtimeoutMs = 3000;
|
|
|
|
function __construct($iface = "can0") {
|
|
if ($iface == "")
|
|
return;
|
|
|
|
$this->fd = di_can_open($iface);
|
|
if ($this->fd) {
|
|
// Our own nodeid based on device:uid doesn't really matter for the switch
|
|
// but we need to set it so we won't have an 0x00000000 nodeid (unset)
|
|
di_can_set_nodeid($this->fd, "12343456789012345678901234567890");
|
|
}
|
|
}
|
|
|
|
function __destruct() {
|
|
if ($this->fd) {
|
|
di_can_close($this->fd);
|
|
$this->fd = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create new can timeout
|
|
*/
|
|
private function canTimeoutNew() {
|
|
return (microtime(true) * 1000) + $this->__CANtimeoutMs;
|
|
}
|
|
|
|
/**
|
|
* Check if CAN receive timeout is exceeded
|
|
* @return true when timeout is exceeded, false otherwise
|
|
*/
|
|
private function canTimeoutIsExceeded($t) {
|
|
if ((microtime(true) * 1000) > $t)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/** Set CAN receive timeout */
|
|
public function setCANRecvTimeout($timeout_ms) {
|
|
$this->__CANtimeoutMs = $timeout_ms;
|
|
}
|
|
|
|
/**
|
|
* Force trigger a GPS sensor update
|
|
*/
|
|
public function can_raw_dncm_gps_sensor_trigger() {
|
|
$req = [];
|
|
$req['msgtype'] = DI_CAN_MSGTYPE_RAW;
|
|
$req['ttype'] = DI_CAN_TRANSFERTYPE_REQUEST;
|
|
$req['dtype'] = 0x405; // TODO macro
|
|
$req['dst_id'] = DI_CAN_NODEID_BROADCAST;
|
|
|
|
$rep = di_can_reqrep_with_retry($this->fd, $req, 2000, 3); // TODO wait max 500ms because of semihosting
|
|
if ($rep)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Set GPS sensor update interval (wakeup time)
|
|
*/
|
|
public function can_raw_dncm_gps_sensor_set_update_interval($interval_sec) {
|
|
$req = [];
|
|
$req['msgtype'] = DI_CAN_MSGTYPE_RAW;
|
|
$req['ttype'] = DI_CAN_TRANSFERTYPE_REQUEST;
|
|
$req['dtype'] = 0x405; // TODO macro
|
|
$req['dst_id'] = DI_CAN_NODEID_BROADCAST;
|
|
$req['ptype'] = DI_CAN_PTYPE_U16;
|
|
$req['msg'] = $interval_sec;
|
|
|
|
$rep = di_can_reqrep_with_retry($this->fd, $req, 2000, 3); // TODO wait max 500ms because of semihosting
|
|
if ($rep)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Set DNCM communication status sending
|
|
*/
|
|
public function can_raw_dncm_communication_status($on_change, $interval_sec) {
|
|
$req = [];
|
|
$req['msgtype'] = DI_CAN_MSGTYPE_RAW;
|
|
$req['ttype'] = DI_CAN_TRANSFERTYPE_REQUEST;
|
|
$req['dtype'] = 0x406; // TODO macro
|
|
$req['dst_id'] = DI_CAN_NODEID_BROADCAST;
|
|
$req['ptype'] = DI_CAN_PTYPE_STRUCT;
|
|
$payload = "";
|
|
if ($on_change)
|
|
$payload = "\x01";
|
|
else
|
|
$payload = "\x00";
|
|
$payload .= pack("n*", $interval_sec);
|
|
$req['msg'] = $payload;
|
|
|
|
$rep = di_can_reqrep_with_retry($this->fd, $req, 2000, 3); // TODO wait max 500ms because of semihosting
|
|
if ($rep)
|
|
return true;
|
|
return false;
|
|
}
|
|
};
|