The xplane_io (X-Plane I/O) program is used to establish network communications, via UDP, with the X-Plane flight simulator running on a computer. The code consists of class libraries that abstract the lower-level UDP packet encoding and decoding details, according to the UDP protocol specifications in X-Plane version 9. Any X-Plane DATA packets can be sent and received, and any X-Plane DataRefs can be set by sending DREF packets to X-Plane.
Dependencies: EthernetNetIf mbed ConfigFile
Diff: XPlaneUdp/XPlaneUdpDecoder.cpp
- Revision:
- 0:a5d13af495af
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XPlaneUdp/XPlaneUdpDecoder.cpp Wed Dec 21 22:29:59 2011 +0000 @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <string.h> +#include "XPlaneUdp.h" +#include "XPlaneUdpDecoder.h" +#include "XPlaneUdpDATA.h" + +void XPlaneUdpDecoder::setDebug(bool debug) { + _debug = debug; +} + +void XPlaneUdpDecoder::setReverseByteOrder(bool reverseByteOrder) { + _reverseByteOrder = reverseByteOrder; +} + +XPlaneUdpMessageType XPlaneUdpDecoder::setUdpBuffer(char * buf, int bufLen) { + _udpPacketMsgType = UNKNOWN; + _udpBuffer = buf; + _udpBufferLen = bufLen; + //_udpBuffer[bufLen] = 0; // only need to do this if treating buffer like a string, e.g. printing it + + if (_udpBufferLen >= 5) { + if (strncmp(_udpBuffer, "DATA", 4) == 0) { + if (_debug) printf("Got DATA pkt \n"); + _udpPacketMsgType = DATA; + } + } + + return _udpPacketMsgType; +} + +int XPlaneUdpDecoder::putIntoDATAMap(XPlaneUdpDATAMap & DATAMap, bool filter) { + int updateCount = 0; + + if (_udpPacketMsgType != DATA) { + return 0; + } + + if (filter && (DATAMap.size() == 0)) { + return 0; + } + + int dataMsgCount = (_udpBufferLen - 5) / XPLANE_DATA_MSG_LENGTH; + if (_debug) printf("%d msgs in pkt \n", dataMsgCount); + int bufOffset = 5; + for (int msgNbr = 0; msgNbr < dataMsgCount; msgNbr++) { + + int index; + memcpy(&index, _udpBuffer + bufOffset, 4); + if (_reverseByteOrder) { + reverse4Bytes((char*) &index); + } + + XPlaneUdpDATA *DATAmsg = getXPlaneUdpDATA(DATAMap, index); + if (DATAmsg != NULL) { + DATAmsg->setAllData(_udpBuffer + bufOffset); + updateCount++; + } + else if (! filter) { + DATAmsg = new XPlaneUdpDATA(index, _reverseByteOrder); + DATAmsg->setAllData(_udpBuffer + bufOffset); + DATAMap[index] = DATAmsg; + updateCount++; + } + + bufOffset += XPLANE_DATA_MSG_LENGTH; + } + + return updateCount; +}