Repository navigation
Expand file tree
/
Copy pathrigctld.php
More file actions
125 lines (105 loc) · 2.69 KB
/
Copy pathrigctld.php
File metadata and controls
125 lines (105 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* @brief rigctld API
* @date 2018-12-03
* @author Manawyrm
* @copyright MIT-licensed
*
*/
class rigctldAPI
{
private $host;
private $port;
private $socket = false;
private $fp = false;
function __construct($host = "127.0.0.1", $port = 4532)
{
$this->host = $host;
$this->port = $port;
$this->connect();
}
function __destruct()
{
if ($this->fp !== false) {
fclose($this->fp);
}
}
public function connect()
{
$this->fp = fsockopen($this->host, $this->port, $errno, $errstr, 5);
if (!$this->fp)
return false;
return true;
}
private function runCommand($command)
{
if ($this->fp === false) {
return false;
}
if (feof($this->fp)) {
$this->fp = false;
return false;
}
// Clear any buffered data before sending command
stream_set_blocking($this->fp, false);
while (($discard = fgets($this->fp)) !== false) {
// discard everything
}
stream_set_blocking($this->fp, true);
stream_set_timeout($this->fp, 2);
fwrite($this->fp, $command . "\n");
// Read one line response (trimmed)
$line = trim(fgets($this->fp));
return $line === false ? false : $line;
}
public function getFrequencyAndMode()
{
$freq = null;
$mode = null;
// Try reading frequency, retry if invalid
for ($i = 0; $i < 3; $i++) {
$freqCandidate = $this->runCommand("f");
if (is_numeric($freqCandidate) && intval($freqCandidate) > 1000000) {
$freq = $freqCandidate;
break;
}
// wait briefly before retry
usleep(100000);
}
// Try reading mode, retry if invalid
$validModes = ['CW','USB','LSB','FM','AM','DIGI','RTTY','PKT','CWR','SAM'];
for ($i = 0; $i < 3; $i++) {
$modeCandidate = strtoupper($this->runCommand("m"));
if (in_array($modeCandidate, $validModes)) {
$mode = $modeCandidate;
break;
}
usleep(100000);
}
if ($freq === null || $mode === null) {
error_log("Could not get valid frequency or mode after retries. freq='$freq' mode='$mode'");
return [
"frequency" => null,
"mode" => null,
];
}
return [
"frequency" => $freq,
"mode" => $mode,
];
}
public function getFrequency()
{
return $this->runCommand("f");
}
public function getMode()
{
$mode = $this->runCommand("m");
if ($mode === false)
return false;
$mode = explode("\n", $mode);
return [
"mode" => $mode[0],
];
}
}