Cleaned up main file.

- Improved formatting in XPCPlugin.cpp to be consistent throughout the file.
 - Formatted copyright and license notices to be consistent with the rest of the codebase.
 - Replaced out of date documentation with a link to the GitHub wiki
 - Moved the writing of the log header to the Log class and expanded it to include useful information.
 - Updated Windows binaries
This commit is contained in:
Jason Watkins
2015-04-14 11:42:25 -07:00
parent 4927b6d1b8
commit 74dc2d22ed
5 changed files with 114 additions and 121 deletions

View File

@@ -13,21 +13,44 @@ namespace XPC
static void WriteTime(FILE* fd)
{
time_t rawtime;
struct tm* timeinfo;
tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char buffer[23] = { 0 };
char buffer[16] = { 0 };
// Format is equivalent to [%F %T], but neither of those specifiers is
// supported on Windows as of Visual Studio 13
strftime(buffer, 23, "[%Y-%m-%d %H:%M:%S] ", timeinfo);
strftime(buffer, 16, "[%H:%M:%S] ", timeinfo);
fprintf(fd, buffer);
}
void Log::Initialize()
void Log::Initialize(std::string version)
{
FILE* fd = fopen("XPCLog.txt", "w");
if (fd != NULL)
{
time_t rawtime;
tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char timeStr[16] = { 0 };
// Format is equivalent to %F, but neither of those specifiers is
// supported on Windows as of Visual Studio 13
strftime(timeStr, 16, "%Y-%m-%d", timeinfo);
fprintf(fd, "X-Plane Connect [Version %s]\n", version.c_str());
fprintf(fd, "Copyright (c) 2013-2015 United States Government as represented by the\n");
fprintf(fd, "Administrator of the National Aeronautics and Space Administration.\n");
fprintf(fd, "All Rights Reserved.\n\n");
fprintf(fd, "This file contains debugging information about the X-Plane Connect plugin.\n");
fprintf(fd, "If you have technical issues with the plugin, please report them by opening\n");
fprintf(fd, "an issue on GitHub (https://github.com/nasa/XPlaneConnect/issues) or by\n");
fprintf(fd, "emailing Christopher Teubert (christopher.a.teubert@nasa.gov).\n\n");
fprintf(fd, "Log file generated on %s.\n", timeStr);
fclose(fd);
}
}
void Log::WriteLine(const std::string& value)
@@ -37,7 +60,7 @@ namespace XPC
void Log::WriteLine(const char* value)
{
FILE* fd = fopen("xpcLog.txt", "a");
FILE* fd = fopen("XPCLog.txt", "a");
if (!fd)
{
return;
@@ -54,7 +77,7 @@ namespace XPC
va_list args;
va_start(args, format);
FILE* fd = fopen("xpcLog.txt", "a");
FILE* fd = fopen("XPCLog.txt", "a");
if (!fd)
{
return;

View File

@@ -21,7 +21,7 @@ namespace XPC
public:
/// Initializes the logging component by deleting old log files,
/// writing header information to the log file.
static void Initialize();
static void Initialize(std::string header);
/// Writes the C string pointed to by format, followed by a line
/// terminator to the XPC log file. If format contains format

View File

@@ -1,91 +1,75 @@
//Copyright (c) 2013-2015 United States Government as represented by the Administrator of the
//National Aeronautics and Space Administration. All Rights Reserved.
//
// XPCPlugin Beta
//DISCLAIMERS
// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND,
// EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY
// THAT THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED,
// WILL CONFORM TO THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN
// ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS, RESULTING DESIGNS,
// HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT
// SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING
// THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS."
//
// Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE UNITED STATES
// GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF
// RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES
// OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING
// FROM, RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE
// UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT,
// TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE
// IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.
//
//X-Plane API
//Copyright(c) 2008, Sandy Barbour and Ben Supnik All rights reserved.
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
//associated documentation files(the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute,
//sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Neither the names of the authors nor that of X - Plane or Laminar Research
// may be used to endorse or promote products derived from this software
// without specific prior written permission from the authors or
// Laminar Research, respectively.
// X-Plane Connect Plugin
//
// DESCRIPTION
// XPCPlugin Facilitates Communication to and from the XPlane
//
// REQUIREMENTS
// 1. X-Plane Version 9.0 or newer (untested on previous versions)
// 2. XPCPlugin.xpl-must be placed in [X-Plane Directory]/Resources/plugins
// 3. OS X 10.8 or newer (untested on previous versions/Windows)
//
// INSTRUCTIONS
// 1. Move xpcPlugin.xpl into [X-Plane Directory]/Resources/plugins
//
// COMMAND TYPES
// DATA: Works like UDP Commands
// SIMU: Pauses/Unpauses Simulation
// CONN: Set Returned Port Number
// DREF: Sets value to specific DREF (see http://www.xsquawkbox.net/xpsdk/docs/DataRefs.html)
// GETD: Requests specific DREF
// POSI: Set Position
// CTRL: Set Control Variables
// WYPT: Add/Edit a Waypoint (PLANNED)
// VIEW: Set Simulation View (PLANNED)
// TEXT: Add Text to Screen (PLANNED)
//
// NOTICES:
// Copyright ã 2013-2014 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. All Rights Reserved.
//
// DISCLAIMERS
// No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT "AS IS."
//
// Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT.
//
// X-Plane API
// Copyright (c) 2008, Sandy Barbour and Ben Supnik All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Neither the names of the authors nor that of X-Plane or Laminar Research may be used to endorse or promote products derived from this software without specific prior written permission from the authors or Laminar Research, respectively.
//
// X-Plane API SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// See Readme.md in the root of this repository or the wiki hosted on GitHub at
// https://github.com/nasa/XPlaneConnect/wiki for requirements, installation instructions,
// and detailed documentation.
//
// CONTACT
// For questions email Christopher Teubert (christopher.a.teubert@nasa.gov)
//
// CONTRIBUTORS
// CT: Christopher Teubert (christopher.a.teubert@nasa.gov)
//
// TO DO
// 1. Handle hitting maxcon
// 2. POSI: Alpha/Beta
// 3. Error Handling
// 4. CTRL: add SpeedBrakes
// 5. SIMU: -1 = switch
// 6. SENDBUF: Add message
// 7. Ability to add text to screen
// 8. Bound checking
//
// BEGIN CODE
#define _WINSOCKAPI_
// JW: Jason Watkins (jason.w.watkins@nasa.gov)
// XPC Includes
#include "Log.h"
#include "DataManager.h"
#include "DataMaps.h"
#include "Drawing.h"
#include "Log.h"
#include "Message.h"
#include "MessageHandlers.h"
#include "UDPSocket.h"
// XPLM Includes
//#include "XPLMPlanes.h"
#include "XPLMProcessing.h"
#include "XPLMGraphics.h"
// System Includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32 /* WIN32 SYSTEM */
#include <windows.h>
#elif (__APPLE__)
#ifdef __APPLE__
#include <mach/mach_time.h>
#endif
#define MAXCONN 50 // Maximum number of dedicated connections
#define RECVPORT 49009 // Port that the plugin receives commands on
#define SENDPORT 49097 // Port that the plugin sends on
#define OPS_PER_CYCLE 20 // Max Number of operations per cycle
@@ -93,37 +77,25 @@
XPC::UDPSocket* recvSocket = nullptr;
XPC::UDPSocket* sendSocket = nullptr;
double start,lap;
static double timeConvert = 0.0;
int benchmarkingSwitch = 0; // 1 = time for operations, 2 = time for op + cycle;
int cyclesToClear = -1; // Clear message bus every n cycles. -1 == dont clear
int counter = 0;
double start,lap;
static double timeConvert = 0.0;
int benchmarkingSwitch = 0; // 1 = time for operations, 2 = time for op + cycle;
int cyclesToClear = -1; // Clear message bus every n cycles. -1 == dont clear
int counter = 0;
PLUGIN_API int XPluginStart(char * outName, char * outSig, char * outDesc);
static float MyFlightLoopCallback(float, float, int inCounter, void * inRefcon);
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc);
PLUGIN_API void XPluginStop(void);
PLUGIN_API void XPluginDisable(void);
PLUGIN_API int XPluginEnable(void);
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFromWho, int inMessage, void * inParam);
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFromWho, int inMessage, void* inParam);
static float XPCFlightLoopCallback(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void* inRefcon);
PLUGIN_API int XPluginStart( char * outName,
char * outSig,
char * outDesc)
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc)
{
static FILE *logFile; // Log File
strcpy(outName, "xplaneConnect");
strcpy(outSig, "NASA.xplaneConnect");
strcpy(outDesc, "X Plane Communications Toolbox");
logFile = fopen("xpcLog.txt","w");
if (logFile != NULL) // If opened correctly
{
fprintf(logFile,"\n");
fclose(logFile);
}
XPC::Log::WriteLine("[EXEC] xpcPlugin Start");
strcpy(outName, "X-Plane Connect [Version 0.2.0]");
strcpy(outSig, "NASA.XPlaneConnect");
strcpy(outDesc, "X Plane Communications Toolbox\nCopyright (c) 2013-2015 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. All Rights Reserved.");
#if (__APPLE__)
if ( abs(timeConvert) <= 1e-9 ) // is about 0
{
@@ -134,20 +106,21 @@ PLUGIN_API int XPluginStart( char * outName,
1000000000.0;
}
#endif
XPC::Log::Initialize("0.2.0");
XPC::Log::WriteLine("[EXEC] Plugin Start");
XPC::DataManager::Initialize();
XPLMRegisterFlightLoopCallback(
MyFlightLoopCallback, /* Callback */
-1, /* Interval (s)*/
NULL); /* refcon not used. */
float interval = -1; // Call every frame
void* refcon = NULL; // Don't pass anything to the callback directly
XPLMRegisterFlightLoopCallback(XPCFlightLoopCallback, interval, refcon);
return 1;
}
PLUGIN_API void XPluginStop(void)
{
XPLMUnregisterFlightLoopCallback(MyFlightLoopCallback, NULL);
XPC::Log::WriteLine("[EXEC] xpcPlugin Shutdown");
XPLMUnregisterFlightLoopCallback(XPCFlightLoopCallback, NULL);
XPC::Log::WriteLine("[EXEC] Plugin Shutdown");
}
PLUGIN_API void XPluginDisable(void)
@@ -164,7 +137,7 @@ PLUGIN_API void XPluginDisable(void)
// Stop rendering waypoints to screen.
XPC::Drawing::ClearWaypoints();
XPC::Log::WriteLine("[EXEC] xpcPlugin Disabled, sockets closed");
XPC::Log::WriteLine("[EXEC] Plugin Disabled, sockets closed");
}
PLUGIN_API int XPluginEnable(void)
@@ -174,7 +147,7 @@ PLUGIN_API int XPluginEnable(void)
sendSocket = new XPC::UDPSocket(SENDPORT);
XPC::MessageHandlers::SetSocket(sendSocket);
XPC::Log::WriteLine("[EXEC] xpcPlugin Enabled, sockets opened");
XPC::Log::WriteLine("[EXEC] Plugin Enabled, sockets opened");
if (benchmarkingSwitch > 0)
{
XPC::Log::FormatLine("[EXEC] Benchmarking Enabled (Verbosity: %i)", benchmarkingSwitch);
@@ -186,29 +159,28 @@ PLUGIN_API int XPluginEnable(void)
return 1;
}
PLUGIN_API void XPluginReceiveMessage( XPLMPluginID inFromWho,
int inMessage,
void * inParam)
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFromWho, int inMessage, void* inParam)
{
// XPC doesn't have anything useful to say to other plugins, so simply ignore
// any messages received.
}
float MyFlightLoopCallback( float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void * inRefcon)
float XPCFlightLoopCallback(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void* inRefcon)
{
int i;
#if (__APPLE__)
double diff_t;
#endif
counter++;
if (benchmarkingSwitch > 1)
{
XPC::Log::FormatLine("Cycle time %.6f", inElapsedSinceLastCall);
}
for (i=0;i<OPS_PER_CYCLE;i++)
for (int i = 0; i < OPS_PER_CYCLE; i++)
{
if (benchmarkingSwitch > 0)
{
@@ -216,6 +188,7 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
start = (double)mach_absolute_time( ) * timeConvert;
#endif
}
XPC::Message msg = XPC::Message::ReadFrom(*recvSocket);
if (msg.GetHead() == "")
{
@@ -228,19 +201,16 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
#if (__APPLE__)
lap = (double)mach_absolute_time( ) * timeConvert;
diff_t = lap-start;
XPC::Log::FormatLine("Runtime %.6f",diff_t);
XPC::Log::FormatLine("[BENCH] Runtime %.6f",diff_t);
#endif
}
}
if (cyclesToClear != -1)
if (cyclesToClear != -1 && counter%cyclesToClear == 0)
{
if (counter%cyclesToClear==0)
{
XPC::Log::WriteLine("[EXEC] Cleared UDP Buffer");
delete recvSocket;
recvSocket = new XPC::UDPSocket(RECVPORT);
}
XPC::Log::WriteLine("[EXEC] Cleared UDP Buffer");
delete recvSocket;
recvSocket = new XPC::UDPSocket(RECVPORT);
}
return -1;
}

Binary file not shown.

Binary file not shown.