Modified parseCTRL and readCTRL to return a struct.
- Defined a new struct that contains the information sent by the CTRL command - Added some additional validation to parseCTRL. - Changed the return type of parseCTRL and removed now redundant pointer parameters. - Propagated return type change throughout the plugin code base.
This commit is contained in:
@@ -509,17 +509,21 @@ short readPOSI(struct xpcSocket recfd, float resultArray[], int arraySize, floa
|
||||
return -1;
|
||||
}
|
||||
|
||||
float readCTRL(struct xpcSocket recfd, float resultArray[4], short *gear)
|
||||
xpcCtrl readCTRL(struct xpcSocket recfd)
|
||||
{
|
||||
char buf[5000] = {0};
|
||||
readUDP(recfd,buf, NULL);
|
||||
|
||||
xpcCtrl result;
|
||||
char buf[5000] = { 0 };
|
||||
readUDP(recfd, buf, NULL);
|
||||
|
||||
if (buf[0] != '\0') // Buffer is not empty
|
||||
{
|
||||
return parseCTRL(buf, resultArray, gear);
|
||||
result = parseCTRL(buf);
|
||||
}
|
||||
|
||||
return NAN;
|
||||
else
|
||||
{
|
||||
result.aircraft = -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//PARSE
|
||||
@@ -610,21 +614,38 @@ short parsePOSI(const char my_message[], float resultArray[], int arraySize, flo
|
||||
return my_message[5]; // Aircraft
|
||||
}
|
||||
|
||||
float parseCTRL(const char my_message[], float resultArray[], short *gear)
|
||||
xpcCtrl parseCTRL(const char data[])
|
||||
{
|
||||
int i;
|
||||
float flaps = 0;
|
||||
|
||||
// Controls
|
||||
for (i = 0; i < 4; i++)
|
||||
xpcCtrl result;
|
||||
unsigned char len = data[4];
|
||||
//Preconditions
|
||||
//Validate message prefix to ensure we are looking at the right kind of packet.
|
||||
if (strncmp(data, "CTRL", 4) != 0)
|
||||
{
|
||||
memcpy(&resultArray[i],&my_message[5+4*i],4);
|
||||
result.aircraft = -1;
|
||||
}
|
||||
|
||||
// Gear, Flaps
|
||||
*gear = (short) my_message[21];
|
||||
|
||||
memcpy(&flaps,&my_message[22],4);
|
||||
|
||||
return flaps;
|
||||
//Legacy packets that don't specify an aircraft number should be 22 bytes long.
|
||||
//Packets specifying an A/C num should be 24 bytes.
|
||||
else if (len != 26 && len != 27)
|
||||
{
|
||||
result.aircraft = -1;
|
||||
}
|
||||
//Everything checks out, so we can skip over the header and copy the raw data
|
||||
//into the struct.
|
||||
else
|
||||
{
|
||||
//NOTE: It's tempting to just do a single memcpy here, but we can't do that because the
|
||||
// compiler is allowed to add padding to the struct type.
|
||||
result.pitch = *((float*)(data + 5));
|
||||
result.roll = *((float*)(data + 9));
|
||||
result.yaw = *((float*)(data + 13));
|
||||
result.throttle = *((float*)(data + 17));
|
||||
result.gear = data[21];
|
||||
result.flaps = *((float*)(data + 22));
|
||||
if (len == 27)
|
||||
{
|
||||
result.aircraft = data[26];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,17 @@
|
||||
int sock;
|
||||
#endif
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float pitch;
|
||||
float roll;
|
||||
float yaw;
|
||||
float throttle;
|
||||
char gear;
|
||||
float flaps;
|
||||
char aircraft;
|
||||
} xpcCtrl;
|
||||
|
||||
// Basic Functions
|
||||
struct xpcSocket openUDP(unsigned short port, const char *xpIP, unsigned short xpPort);
|
||||
@@ -58,8 +69,8 @@
|
||||
short sendPOSI(struct xpcSocket recfd, short ACNum, short numArgs, float valueArray[]);
|
||||
|
||||
// Controls
|
||||
float parseCTRL(const char my_message[], float resultArray[4], short *gear);
|
||||
float readCTRL(struct xpcSocket recfd, float resultArray[4], short *gear);
|
||||
xpcCtrl parseCTRL(const char data[]);
|
||||
xpcCtrl readCTRL(struct xpcSocket recfd);
|
||||
short sendCTRL(struct xpcSocket recfd, short numArgs, float valueArray[]);
|
||||
|
||||
// DREF Manipulation
|
||||
|
||||
@@ -719,45 +719,43 @@ int handlePOSI(char buf[])
|
||||
int handleCTRL(char buf[])
|
||||
{
|
||||
char logmsg[100];
|
||||
float flaps;
|
||||
float controls[4] = {0.0};
|
||||
float throtArray[8] = {0};
|
||||
xpcCtrl ctrl;
|
||||
float thr[8] = { 0 };
|
||||
short i;
|
||||
short gear = -1;
|
||||
|
||||
// UPDATE LOG
|
||||
sprintf(logmsg,"[CTRL] Message Received (Conn %i)", current_connection+1);
|
||||
updateLog(logmsg, strlen(logmsg));
|
||||
|
||||
flaps = parseCTRL(buf,controls,&gear);
|
||||
ctrl = parseCTRL(buf);
|
||||
|
||||
if ( flaps != flaps ) // Is NaN
|
||||
if (ctrl.aircraft == -1) //parseCTRL failed
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
// SET CONTROLS
|
||||
XPLMSetDataf(XPLMDataRefs[11][0],controls[0]);
|
||||
XPLMSetDataf(XPLMDataRefs[11][1],controls[1]);
|
||||
XPLMSetDataf(XPLMDataRefs[11][2],controls[2]);
|
||||
XPLMSetDataf(XPLMDataRefs[11][0], ctrl.pitch);
|
||||
XPLMSetDataf(XPLMDataRefs[11][1], ctrl.roll);
|
||||
XPLMSetDataf(XPLMDataRefs[11][2], ctrl.yaw);
|
||||
|
||||
// SET Throttle
|
||||
for ( i=0; i<8; i++ )
|
||||
{
|
||||
throtArray[i]=controls[3];
|
||||
thr[i] = ctrl.throttle;
|
||||
}
|
||||
XPLMSetDatavf(XPLMDataRefs[25][0],throtArray,0,8);
|
||||
XPLMSetDatavf(XPLMDataRefs[26][0],throtArray,0,8);
|
||||
setDREF(XPLMFindDataRef("sim/flightmodel/engine/ENGN_thro_override"),controls,3,1);
|
||||
XPLMSetDatavf(XPLMDataRefs[25][0], thr, 0, 8);
|
||||
XPLMSetDatavf(XPLMDataRefs[26][0], thr, 0, 8);
|
||||
setDREF(XPLMFindDataRef("sim/flightmodel/engine/ENGN_thro_override"), thr, 0, 1);
|
||||
|
||||
// SET Gear/Flaps
|
||||
if ( gear != -1 )
|
||||
if (ctrl.gear != -1)
|
||||
{
|
||||
setGEAR(0, gear, 0); // Gear
|
||||
setGEAR(0, ctrl.gear, 0); // Gear
|
||||
}
|
||||
if ( flaps < -999.5 || flaps > -997.5 ) // Flaps
|
||||
if (ctrl.flaps < -999.5 || ctrl.flaps > -997.5) // Flaps
|
||||
{
|
||||
XPLMSetDataf(XPLMDataRefs[13][3],flaps);
|
||||
XPLMSetDataf(XPLMDataRefs[13][3], ctrl.flaps);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -378,12 +378,9 @@ int printBufferToLog(struct XPCMessage & msg)
|
||||
}
|
||||
else if (strncmp(msg.head,"CTRL",4)==0)
|
||||
{// Header = CTRL (Control)
|
||||
float flaps;
|
||||
float controls[4];
|
||||
short gear;
|
||||
flaps = parseCTRL(msg.msg,controls,&gear);
|
||||
xpcCtrl ctrl = parseCTRL(msg.msg);
|
||||
|
||||
sprintf(logmsg,"%s (%f %f %f) %f %hi %f",logmsg, controls[0], controls[1], controls[2], controls[3] , gear, flaps);
|
||||
sprintf(logmsg,"%s (%f %f %f) %f %hi %f",logmsg, ctrl.pitch, ctrl.roll, ctrl.yaw, ctrl.throttle, ctrl.gear, ctrl.flaps);
|
||||
|
||||
updateLog(logmsg,strlen(logmsg));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user