27#include "wasim_version.h"
37#define WSMCMND_COMMON_NAME_PREFIX WSMCMND_PROJECT_NAME "."
38#define WSMCMND_EVENT_NAME_CONNECT "Connect"
39#define WSMCMND_EVENT_NAME_PING "Ping"
40#define WSMCMND_CDA_NAME_COMMAND "Command"
41#define WSMCMND_CDA_NAME_RESPONSE "Response"
42#define WSMCMND_CDA_NAME_DATA "Data"
43#define WSMCMND_CDA_NAME_KEYEVENT "KeyEvent"
44#define WSMCMND_CDA_NAME_LOG "Log"
86 static void setCharArrayValue(
char *dest,
const size_t size,
const char *str) {
87 std::strncpy(dest, str, size-1);
90 #define WSE WASimCommander::Enums
108 explicit Command(WSE::CommandId
id = WSE::CommandId::None, uint32_t uData = 0,
const char *str =
nullptr,
double fData = 0.0, int32_t token = 0) :
109 token(token), uData(uData), fData(fData), commandId(id)
111 if (str) setStringData(str);
119 const char *cmdName = (size_t)c.
commandId < WSE::CommandIdNames.size() ? WSE::CommandIdNames.at((
size_t)c.
commandId) :
"Invalid";
120 return os <<
"Command{" << cmdName <<
"; token: " << c.
token <<
"; uData: " << c.
uData
121 <<
"; fData: " << std::fixed << std::setprecision(9) << c.
fData <<
"; sData: " << std::quoted(c.
sData) <<
'}';
150 uint32_t valueSize =
sizeof(
double),
151 WSE::RequestType requestType = WSE::RequestType::Calculated,
152 WSE::CalcResultType calcResultType = WSE::CalcResultType::Double,
153 WSE::UpdatePeriod period = WSE::UpdatePeriod::Tick,
154 const char * nameOrCode =
nullptr,
155 const char * unitName =
nullptr,
156 char varTypePrefix =
'L',
157 float deltaEpsilon = 0.0f,
158 uint8_t interval = 0,
159 uint8_t simVarIndex = 0
161 requestId(requestId), valueSize(valueSize), deltaEpsilon(deltaEpsilon), interval(interval), period(period),
162 requestType(requestType), calcResultType(calcResultType), simVarIndex(simVarIndex), varTypePrefix(varTypePrefix)
165 setNameOrCode(nameOrCode);
167 setUnitName(unitName);
168 if (!valueSize && requestType == WSE::RequestType::Calculated)
169 this->valueSize = (calcResultType == WSE::CalcResultType::Double ?
DATA_TYPE_DOUBLE : calcResultType == WSE::CalcResultType::Integer ?
DATA_TYPE_INT32 : 256);
173 explicit DataRequest(uint32_t requestId,
char variableType,
const char *variableName, uint32_t valueSize,
174 WSE::UpdatePeriod period = WSE::UpdatePeriod::Tick, uint32_t interval = 0,
float deltaEpsilon = 0.0f) :
175 DataRequest(requestId, valueSize, WSE::RequestType::Named, WSE::CalcResultType::None, period, variableName, nullptr, variableType, deltaEpsilon, interval)
178 explicit DataRequest(uint32_t requestId,
const char *simVarName,
const char *unitName, uint8_t simVarIndex, uint32_t valueSize,
179 WSE::UpdatePeriod period = WSE::UpdatePeriod::Tick, uint32_t interval = 0,
float deltaEpsilon = 0.0f) :
180 DataRequest(requestId, valueSize, WSE::RequestType::Named, WSE::CalcResultType::None, period, simVarName, unitName,
'A', deltaEpsilon, interval, simVarIndex)
183 explicit DataRequest(uint32_t requestId, WSE::CalcResultType resultType,
const char *calculatorCode, uint32_t valueSize,
184 WSE::UpdatePeriod period = WSE::UpdatePeriod::Tick, uint32_t interval = 0,
float deltaEpsilon = 0.0f) :
185 DataRequest(requestId, valueSize, WSE::RequestType::Calculated, resultType, period, calculatorCode, nullptr,
'Q', deltaEpsilon, interval)
192 explicit DataRequest(uint32_t requestId, WSE::CalcResultType resultType,
const char *calculatorCode,
193 WSE::UpdatePeriod period = WSE::UpdatePeriod::Tick, uint32_t interval = 0,
float deltaEpsilon = 0.0f) :
194 DataRequest(requestId, 0, WSE::RequestType::Calculated, resultType, period, calculatorCode, nullptr,
'Q', deltaEpsilon, interval)
203 const char *perName = (size_t)r.
period < WSE::UpdatePeriodNames.size() ? WSE::UpdatePeriodNames.at((
size_t)r.
period) :
"Invalid";
206 return os <<
"; type: None; }";
209 const char *typeName = (size_t)r.
calcResultType < WSE::CalcResultTypeNames.size() ? WSE::CalcResultTypeNames.at((
size_t)r.
calcResultType) :
"Invalid";
210 return os <<
"; type: Calculated; code: " << std::quoted(r.
nameOrCode) <<
" resultType: " << typeName <<
'}';
221 uint32_t values[5] = {0};
227 explicit KeyEvent(uint32_t eventId = 0, std::initializer_list<uint32_t> values = {}, uint32_t token = 0) :
228 eventId(eventId), token(token)
231 for (
const uint32_t v : values) {
232 this->values[i++] = v;
241 os <<
"KeyEvent{eventId: " << c.
eventId <<
"; token: " << c.
token <<
';';
242 for (
short i=0; i < 5; ++i)
243 os <<
" v" << i <<
": " << c.
values[i] <<
';';
258 explicit LogRecord(
const WSE::LogLevel level = WSE::LogLevel::None,
const char *msg =
nullptr,
const std::chrono::system_clock::time_point &tp = std::chrono::system_clock::now()) :
259 timestamp{std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()).count()}, level{level}
261 if (msg) setMessage(msg);
270 const tm *tim = std::localtime(&secs);
271 const char *levelName = (size_t)l.
level < WSE::LogLevelNames.size() ? WSE::LogLevelNames.at((
size_t)l.
level) :
"Invalid";
273 return os <<
"LogRecord{" << levelName << std::put_time(tim,
" [(%z) %y-%m-%d %H:%M:%S.") << (l.
timestamp - time_t(secs * 1000)) <<
"] " << l.
message <<
'}';
274 return os <<
"LogRecord{" << levelName <<
" [" << l.
timestamp <<
"] " << l.
message <<
'}';
WASimCommander main namespace. Defines constants and structs used in Client-Server interactions....
static const size_t STRSZ_ENAME
Maximum size of custom event name in Enums::CommandId::Register command.
static const size_t STRSZ_LOG
Size of log entry message in LogRecord::message. Size optimizes alignment of LogRecord struct.
static const uint32_t DATA_TYPE_INT32
32-bit integer number (signed or unsigned)
static const uint32_t DATA_TYPE_INT16
16-bit integer number (signed or unsigned)
static const size_t STRSZ_UNIT
Maximum Unit name size. Size is of longest known unit name + 1.
static const size_t STRSZ_REQ
Maximum size for request calculator string or variable name. Size optimizes alignment of DataRequest ...
static const uint32_t DATA_TYPE_FLOAT
32-bit floating-point number
static const time_t CONN_TIMEOUT_SEC
Number of seconds after which a non-responsive client is considered disconnected. Client must respond...
static const size_t STRSZ_CMD
Maximum size of Command::sData member. Size optimizes alignment of Command struct.
static const uint32_t DATA_TYPE_INT64
64-bit integer number (signed or unsigned)
static const time_t TICK_PERIOD_MS
Minimum update period for data Requests in milliseconds. Also dictates rate of some other client-spec...
static const uint32_t DATA_TYPE_INT8
8-bit integer number (signed or unsigned)
static const uint32_t DATA_TYPE_DOUBLE
64-bit floating-point number
Command data structure. The member contents depend on the command type as described in each command t...
Command(WASimCommander::Enums::CommandId id=WASimCommander::Enums::CommandId::None, uint32_t uData=0, const char *str=nullptr, double fData=0.0, int32_t token=0)
Default constructor with all parameters optional.
char sData[STRSZ_CMD]
String command parameter value, meaning depends on the command being issued.
WASimCommander::Enums::CommandId commandId
What to do.
void setStringData(const char *str)
Set the sData member using a const char array.
friend std::ostream & operator<<(std::ostream &os, const Command &c)
ostream operator for logging purposes
double fData
double-precision floating point command parameter value, meaning depends on the command being issued.
uint32_t token
A unique ID for this command instance. Echoed back by server in command responses....
uint32_t uData
DWORD command parameter value, meaning depends on the command being issued.
Structure for variable value subscription requests.
uint8_t simVarIndex
Some SimVars require an index for access, default is 0.
uint32_t requestId
Unique ID for the request, subsequent use of this ID overwrites any previous request definition (but ...
DataRequest(uint32_t requestId, const char *simVarName, const char *unitName, uint8_t simVarIndex, uint32_t valueSize, WASimCommander::Enums::UpdatePeriod period=WASimCommander::Enums::UpdatePeriod::Tick, uint32_t interval=0, float deltaEpsilon=0.0f)
Constructs a request for a named Simulator Variable (requestType = RequestType::Named and ‘varTypePre...
void setUnitName(const char *name)
Set the unitName member using a const char array.
char unitName[STRSZ_UNIT]
Unit name for named variables (optional to override variable's default units). Only 'L' and 'A' varia...
DataRequest(uint32_t requestId, WASimCommander::Enums::CalcResultType resultType, const char *calculatorCode, WASimCommander::Enums::UpdatePeriod period=WASimCommander::Enums::UpdatePeriod::Tick, uint32_t interval=0, float deltaEpsilon=0.0f)
Constructs a calculator code request (requestType = RequestType::Calculated) with optional update per...
WASimCommander::Enums::UpdatePeriod period
How often to read/calculate this value.
friend std::ostream & operator<<(std::ostream &os, const DataRequest &r)
ostream operator for logging purposes
WASimCommander::Enums::RequestType requestType
Named variable or calculated value.
DataRequest(uint32_t requestId, char variableType, const char *variableName, uint32_t valueSize, WASimCommander::Enums::UpdatePeriod period=WASimCommander::Enums::UpdatePeriod::Tick, uint32_t interval=0, float deltaEpsilon=0.0f)
Constructs a request for a named variable (requestType = RequestType::Named) with optional update per...
uint32_t valueSize
Byte size of stored value; can also be one of the predefined DATA_TYPE_* constants.
DataRequest(uint32_t requestId, WASimCommander::Enums::CalcResultType resultType, const char *calculatorCode, uint32_t valueSize, WASimCommander::Enums::UpdatePeriod period=WASimCommander::Enums::UpdatePeriod::Tick, uint32_t interval=0, float deltaEpsilon=0.0f)
Constructs a calculator code request (requestType = RequestType::Calculated) with optional update per...
float deltaEpsilon
Minimum change in numeric value required to trigger an update. The default of 0.0 is to send updates ...
char nameOrCode[STRSZ_REQ]
Variable name or full calculator string.
void setNameOrCode(const char *name)
Set the nameOrCode member using a const char array.
DataRequest(uint32_t requestId, uint32_t valueSize=sizeof(double), WASimCommander::Enums::RequestType requestType=WASimCommander::Enums::RequestType::Calculated, WASimCommander::Enums::CalcResultType calcResultType=WASimCommander::Enums::CalcResultType::Double, WASimCommander::Enums::UpdatePeriod period=WASimCommander::Enums::UpdatePeriod::Tick, const char *nameOrCode=nullptr, const char *unitName=nullptr, char varTypePrefix='L', float deltaEpsilon=0.0f, uint8_t interval=0, uint8_t simVarIndex=0)
Constructor with required request ID, all other parameters optional. See member documentation for exp...
uint32_t interval
How many UpdatePeriod period's should elapse between checks. eg. 500ms or 10 ticks....
char varTypePrefix
Variable type prefix for named variables. Types: 'L' (local), 'A' (SimVar) and 'T' (Token,...
WASimCommander::Enums::CalcResultType calcResultType
Expected calculator result type.
Data structure for sending Key Events to the sim with up to 5 event values. Events are specified usin...
uint32_t eventId
The event ID to trigger. Value is one of KEY_* macro values in MSFS/Legacy/gauges....
uint32_t token
A unique ID for this event trigger. Echoed back by server in command Ack/Nak responses....
uint32_t values[5]
Up to 5 values to pass to the event handler. All are optional, defaults are zero.
friend std::ostream & operator<<(std::ostream &os, const KeyEvent &c)
ostream operator for logging purposes
KeyEvent(uint32_t eventId=0, std::initializer_list< uint32_t > values={}, uint32_t token=0)
Default constructor with all parameters optional. The values initializer list may contain up to 5 mem...
uint32_t reserved
Padding for alignment, unused.
void setMessage(const char *msg)
Set the message member using a const char array.
LogRecord(const WASimCommander::Enums::LogLevel level=WASimCommander::Enums::LogLevel::None, const char *msg=nullptr, const std::chrono::system_clock::time_point &tp=std::chrono::system_clock::now())
Default constructor with all parameters optional.
friend std::ostream & operator<<(std::ostream &os, const LogRecord &l)
ostream operator for logging purposes
time_t timestamp
ms since epoch.
WASimCommander::Enums::LogLevel level
Message severity.
char message[STRSZ_LOG]
The log message text.