RF-Positioning-System V0.1
by Max Heinekamp
Loading...
Searching...
No Matches
scpi.h
Go to the documentation of this file.
1
11
12#ifndef SCPI_H
13#define SCPI_H
14
15#include <Ethernet.h>
16#include "config.h" // defines SCPI_HASH_TYPE — must precede the parser include below
17#define VREKRER_SCPI_PARSER_NO_IMPL
18#include "Vrekrer_scpi_parser.h"
19#include "stepper.h"
20
26class SCPI {
27 public:
28
33 SCPI();
34
39 void setup();
40
45 void processEthernet();
46
51 void processSerial();
52
58 void PrintDebugInfo(Stream& interface);
59
66 void logError(int code, const char* message);
67
73 const char* getError();
74
80 int getPort();
81
87 void reinitEthernet();
88
94 void printSessionList(Stream& interface);
95
101 void printSessionLog(Stream& interface);
102
109 void printSocketList(Stream& interface);
110
116 void printSystemDiagnostics(Stream& interface);
117
123 void printAllHeaders(Stream& interface);
124
132 void printHeaderSyntax(Stream& interface, const char* header);
133
134 private:
139 void registerCommands();
140
141 EthernetServer* scpi_server = nullptr;
142 SCPI_Parser my_instrument;
143
144 // Hardware ceiling: the W5500 has 8 sockets, one of which is reserved for LISTEN.
145 static const int MAX_SCPI_CLIENTS = 4;
146 struct SCPIClient {
147 EthernetClient client;
148 bool active = false;
149 unsigned long connectedAt = 0; // millis() when this slot was last assigned, for oldest-session eviction
150 unsigned long lastActivity = 0; // millis() of last byte transferred either direction
151 unsigned long rxBytes = 0;
152 unsigned long txBytes = 0;
153 IPAddress remoteIP; // cached at accept time — client.remoteIP() is unusable once stopped
154 uint16_t remotePort = 0; // cached at accept time
155 };
156 SCPIClient scpi_clients[MAX_SCPI_CLIENTS];
157
163 class SessionStream : public Stream {
164 public:
165 SessionStream(SCPIClient& session) : session_(session) {}
166 int available() override { return session_.client.available(); }
167 int read() override {
168 int c = session_.client.read();
169 if (c >= 0) {
170 session_.rxBytes++;
171 session_.lastActivity = millis();
172 }
173 return c;
174 }
175 int peek() override { return session_.client.peek(); }
176 void flush() override { session_.client.flush(); }
177 size_t write(uint8_t b) override {
178 size_t n = session_.client.write(b);
179 if (n) {
180 session_.txBytes += n;
181 session_.lastActivity = millis();
182 }
183 return n;
184 }
185 size_t write(const uint8_t* buf, size_t size) override {
186 size_t n = session_.client.write(buf, size);
187 if (n) {
188 session_.txBytes += n;
189 session_.lastActivity = millis();
190 }
191 return n;
192 }
193 private:
194 SCPIClient& session_;
195 };
196
197 // Ring buffer of the last SESSION_LOG_SIZE session closures, for DIAGnostic:SESSion:LOG?.
198 static const int SESSION_LOG_SIZE = 16;
200 unsigned long closedAt = 0;
201 IPAddress remoteIP;
202 uint16_t remotePort = 0;
203 char reason[16] = "";
204 unsigned long durationMs = 0;
205 unsigned long idleMsAtClose = 0;
206 unsigned long rxBytes = 0;
207 unsigned long txBytes = 0;
208 };
209 SessionCloseLog sessionLog[SESSION_LOG_SIZE];
210 int sessionLogHead = 0;
211 int sessionLogCount = 0;
212 void logSessionClose(const SCPIClient& slot, const char* reason);
213
214 unsigned long linkFlapCount = 0; // Ethernet PHY link drops observed (LinkON -> not LinkON)
215 unsigned long reinitCount = 0; // number of times reinitEthernet() has been called
216 EthernetLinkStatus lastLinkStatus = Unknown;
217
218 bool server_started = false;
219
224 struct SCPI_Error {
225 int code;
226 char message[100];
227 };
228
229 // Initialize error queue with a fixed size
230 SCPI_Error errorQueue[ERROR_QUEUE_SIZE];
231 int errorHead = 0;
232 int errorTail = 0;
233 int errorCount = 0;
234 bool queueOverflow = false;
235
236 int port = 0;
237
238};
239
240#endif // SCPI_H
int errorCount
Tracks number of stored errors.
Definition scpi.h:233
int getPort()
Get the port number of the SCPI server.
Definition scpi.cpp:471
void registerCommands()
Register SCPI commands from scpi_commands.h/cpp.
Definition scpi.cpp:274
void printHeaderSyntax(Stream &interface, const char *header)
Print syntax help for a single command header (SYSTem:HELP:SYNTax?).
Definition scpi.cpp:567
int errorTail
Points to the oldest error.
Definition scpi.h:232
void printSocketList(Stream &interface)
Print the raw state of all 8 W5500 hardware sockets, not just the ones tracked as SCPI sessions (DIAG...
Definition scpi.cpp:525
int errorHead
Points to the next free slot.
Definition scpi.h:231
void printSessionLog(Stream &interface)
Print the session close-reason ring buffer (DIAGnostic:SESSion:LOG?).
Definition scpi.cpp:505
void PrintDebugInfo(Stream &interface)
Print debug information about the SCPI parser to the specified interface.
Definition scpi.cpp:431
void logError(int code, const char *message)
Log an error message to the SCPI error queue, compliant with section 21.8 of the SCPI standard.
Definition scpi.cpp:435
void processEthernet()
Process incoming SCPI commands over Ethernet.
Definition scpi.cpp:305
void printSessionList(Stream &interface)
Print the live SCPI session table (DIAGnostic:SESSion:LIST?).
Definition scpi.cpp:475
const char * getError()
Get the oldest error message from the error queue, compliant with section 21.8 of the SCPI standard.
Definition scpi.cpp:449
void processSerial()
Process incoming SCPI commands over Serial.
Definition scpi.cpp:427
void setup()
Set up the SCPI server and register all available commands.
Definition scpi.cpp:280
void printAllHeaders(Stream &interface)
Print every registered SCPI command header, one per line (SYSTem:HELP:HEADers?).
Definition scpi.cpp:557
SCPI()
Construct a new SCPI object.
Definition scpi.cpp:43
bool queueOverflow
Tracks if queue overflowed.
Definition scpi.h:234
int port
Store SCPI port number at time of initialization.
Definition scpi.h:236
void reinitEthernet()
Re-initialize the Ethernet stack (re-reads IP/MAC from flash, re-begins Ethernet and the SCPI server,...
Definition scpi.cpp:406
void printSystemDiagnostics(Stream &interface)
Print system/reset health diagnostics (DIAGnostic:SYStem?).
Definition scpi.cpp:546
Configuration file for the entire project.
Struct to represent an error in the SCPI error queue.
Definition scpi.h:224