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:
Jason Watkins
2015-04-06 12:40:26 -07:00
parent 04166d3bac
commit a05ddb42be
4 changed files with 72 additions and 45 deletions

View File

@@ -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;
}

View File

@@ -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