diff --git a/xpcPlugin/Drawing.cpp b/xpcPlugin/Drawing.cpp index 23df168..40599c7 100644 --- a/xpcPlugin/Drawing.cpp +++ b/xpcPlugin/Drawing.cpp @@ -173,7 +173,7 @@ namespace XPC glBegin(GL_LINE_STRIP); for (size_t i = 0; i < numWaypoints; ++i) { - LocalPoint* l = &localPoints[i]; + LocalPoint* l = &localPoints[i]; glVertex3f((float)l->x, (float)l->y, (float)l->z); } glEnd(); @@ -226,7 +226,7 @@ namespace XPC // Enable drawing if necessary if (!msgEnabled) { - XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL); + XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL); msgEnabled = true; } } diff --git a/xpcPlugin/Message.cpp b/xpcPlugin/Message.cpp index 03b0bf7..f3401f0 100644 --- a/xpcPlugin/Message.cpp +++ b/xpcPlugin/Message.cpp @@ -11,7 +11,7 @@ namespace XPC { - Message::Message() {} + Message::Message() {} Message Message::ReadFrom(const UDPSocket& sock) { @@ -110,17 +110,17 @@ namespace XPC else if (head == "DREF") { Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); - string dref((char*)buffer + 6, buffer[5]); - Log::FormatLine(LOG_DEBUG, "DBUG", " DREF (size %i) = %s", dref.length(), dref.c_str()); - ss.str(""); - int values = buffer[6 + buffer[5]]; - ss << " Values(size " << values << ") ="; - for (int i = 0; i < values; ++i) - { - ss << " " << *((float*)(buffer + values + 1 + sizeof(float) * i)); + string dref((char*)buffer + 6, buffer[5]); + Log::FormatLine(LOG_DEBUG, "DBUG", " DREF (size %i) = %s", dref.length(), dref.c_str()); + ss.str(""); + int values = buffer[6 + buffer[5]]; + ss << " Values(size " << values << ") ="; + for (int i = 0; i < values; ++i) + { + ss << " " << *((float*)(buffer + values + 1 + sizeof(float) * i)); } Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); - } + } else if (head == "GETC" || head == "GETP") { ss << " Aircraft:" << (int)buffer[5]; @@ -129,15 +129,15 @@ namespace XPC else if (head == "GETD") { Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); - int cur = 6; - for (int i = 0; i < buffer[5]; ++i) - { - string dref((char*)buffer + cur + 1, buffer[cur]); - Log::FormatLine(LOG_DEBUG, "DBUG", " #%i/%i (size:%i) %s", + int cur = 6; + for (int i = 0; i < buffer[5]; ++i) + { + string dref((char*)buffer + cur + 1, buffer[cur]); + Log::FormatLine(LOG_DEBUG, "DBUG", " #%i/%i (size:%i) %s", i + 1, buffer[5], dref.length(), dref.c_str()); - cur += 1 + buffer[cur]; - } - } + cur += 1 + buffer[cur]; + } + } else if (head == "POSI") { char aircraft = buffer[5]; @@ -146,14 +146,14 @@ namespace XPC float orient[3]; memcpy(pos, buffer + 6, 12); memcpy(orient, buffer + 18, 12); - ss << " AC:" << (int)aircraft; + ss << " AC:" << (int)aircraft; ss << " Pos:(" << pos[0] << ' ' << pos[1] << ' ' << pos[2] << ") Orient:("; ss << orient[0] << ' ' << orient[1] << ' ' << orient[2] << ") Gear:"; ss << gear; Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); - } + } else if (head == "SIMU") - { + { ss << ' ' << (int)buffer[5]; Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); } @@ -163,9 +163,9 @@ namespace XPC Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); } else - { + { ss << " UNKNOWN HEADER "; Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); - } + } } } diff --git a/xpcPlugin/MessageHandlers.cpp b/xpcPlugin/MessageHandlers.cpp index 1cb0c18..04310c8 100644 --- a/xpcPlugin/MessageHandlers.cpp +++ b/xpcPlugin/MessageHandlers.cpp @@ -38,8 +38,8 @@ namespace XPC std::string MessageHandlers::connectionKey; MessageHandlers::ConnectionInfo MessageHandlers::connection; UDPSocket* MessageHandlers::sock; - - static sockaddr multicast_address = UDPSocket::GetAddr(MULTICAST_GROUP, MULITCAST_PORT); + + static sockaddr multicast_address = UDPSocket::GetAddr(MULTICAST_GROUP, MULITCAST_PORT); void MessageHandlers::SetSocket(UDPSocket* socket) { @@ -135,28 +135,28 @@ namespace XPC MessageHandlers::HandleUnknown(msg); } } - - void MessageHandlers::SendBeacon(const std::string& pluginVersion, unsigned short pluginReceivePort, int xplaneVersion) { - - unsigned char response[128] = "BECN"; - - std::size_t cur = 5; + + void MessageHandlers::SendBeacon(const std::string& pluginVersion, unsigned short pluginReceivePort, int xplaneVersion) { + + unsigned char response[128] = "BECN"; + + std::size_t cur = 5; - // 2 bytes plugin port - *((uint16_t *)(response + cur)) = pluginReceivePort; - cur += sizeof(uint16_t); + // 2 bytes plugin port + *((uint16_t *)(response + cur)) = pluginReceivePort; + cur += sizeof(uint16_t); - // 4 bytes xplane version - *((uint32_t*)(response + cur)) = xplaneVersion; - cur += sizeof(uint32_t); - - // plugin version - int len = pluginVersion.length(); - memcpy(response + cur, pluginVersion.c_str(), len); - cur += strlen(pluginVersion.c_str()) + len; - - sock->SendTo(response, cur, &multicast_address); - } + // 4 bytes xplane version + *((uint32_t*)(response + cur)) = xplaneVersion; + cur += sizeof(uint32_t); + + // plugin version + int len = pluginVersion.length(); + memcpy(response + cur, pluginVersion.c_str(), len); + cur += strlen(pluginVersion.c_str()) + len; + + sock->SendTo(response, cur, &multicast_address); + } void MessageHandlers::HandleConn(const Message& msg) { @@ -372,7 +372,7 @@ namespace XPC } case 20: // Position { - // TODO: loss of precision here + // TODO: loss of precision here double pos[3]; pos[0] = (double)values[i][2]; pos[1] = (double)values[i][3]; @@ -555,7 +555,7 @@ namespace XPC unsigned char response[34] = "POSI"; response[5] = (char)DataManager::GetInt(DREF_GearHandle, aircraft); - // TODO change lat/lon/h to double? + // TODO change lat/lon/h to double? *((float*)(response + 6)) = (float)DataManager::GetDouble(DREF_Latitude, aircraft); *((float*)(response + 10)) = (float)DataManager::GetDouble(DREF_Longitude, aircraft); *((float*)(response + 14)) = (float)DataManager::GetDouble(DREF_Elevation, aircraft); @@ -584,22 +584,22 @@ namespace XPC float orient[3]; if (size == 34) /* lat/lon/h as 32-bit float */ - { - posd[0] = *((float*)&buffer[6]); - posd[1] = *((float*)&buffer[10]); - posd[2] = *((float*)&buffer[14]); - memcpy(orient, buffer + 18, 12); - } - else if (size == 46) /* lat/lon/h as 64-bit double */ { - memcpy(posd, buffer + 6, 3*8); - memcpy(orient, buffer + 30, 12); + posd[0] = *((float*)&buffer[6]); + posd[1] = *((float*)&buffer[10]); + posd[2] = *((float*)&buffer[14]); + memcpy(orient, buffer + 18, 12); } - else - { + else if (size == 46) /* lat/lon/h as 64-bit double */ + { + memcpy(posd, buffer + 6, 3*8); + memcpy(orient, buffer + 30, 12); + } + else + { Log::FormatLine(LOG_ERROR, "POSI", "ERROR: Unexpected size: %i (Expected 34 or 46)", size); return; - } + } /* convert float to double */ DataManager::SetPosition(posd, aircraftNumber); @@ -716,7 +716,7 @@ namespace XPC Log::WriteLine(LOG_INFO, "TEXT", "[TEXT] Text set"); } } - + void MessageHandlers::HandleView(const Message& msg) { // Update Log @@ -750,9 +750,9 @@ namespace XPC campos.loc[0] = *(double*)(buffer+9); campos.loc[1] = *(double*)(buffer+17); campos.loc[2] = *(double*)(buffer+25); - campos.direction[0] = -998; - campos.direction[1] = -998; - campos.direction[2] = -998; + campos.direction[0] = -998; + campos.direction[1] = -998; + campos.direction[2] = -998; campos.zoom = *(float*)(buffer+33); Log::FormatLine(LOG_TRACE, "VIEW", "Cam pos %f %f %f zoom %f", campos.loc[0], campos.loc[1], campos.loc[2],campos.zoom); @@ -781,42 +781,42 @@ namespace XPC // Log::FormatLine(LOG_TRACE, "CAM", "Cam pos %f %f %f", clat, clon, calt); - if(campos->direction[0] == -998) // calculate camera direction - { - // aircraft position - double x = XPC::DataManager::GetDouble(XPC::DREF_LocalX, 0); - double y = XPC::DataManager::GetDouble(XPC::DREF_LocalY, 0); - double z = XPC::DataManager::GetDouble(XPC::DREF_LocalZ, 0); + if(campos->direction[0] == -998) // calculate camera direction + { + // aircraft position + double x = XPC::DataManager::GetDouble(XPC::DREF_LocalX, 0); + double y = XPC::DataManager::GetDouble(XPC::DREF_LocalY, 0); + double z = XPC::DataManager::GetDouble(XPC::DREF_LocalZ, 0); - // relative position vector cam to plane - double dx = x - cX; - double dy = y - cY; - double dz = z - cZ; + // relative position vector cam to plane + double dx = x - cX; + double dy = y - cY; + double dz = z - cZ; // Log::FormatLine(LOG_TRACE, "CAM", "Cam vect %f %f %f", dx, dy, dz); - double pi = 3.141592653589793; + double pi = 3.141592653589793; - // horizontal distance - double dist = sqrt(dx*dx + dz*dz); + // horizontal distance + double dist = sqrt(dx*dx + dz*dz); - outCameraPosition->pitch = atan2(dy, dist) * 180.0/pi; + outCameraPosition->pitch = atan2(dy, dist) * 180.0/pi; - double angle = atan2(dz, dx) * 180.0/pi; // rel to pos right (pos X) + double angle = atan2(dz, dx) * 180.0/pi; // rel to pos right (pos X) - outCameraPosition->heading = 90 + angle; // rel to north + outCameraPosition->heading = 90 + angle; // rel to north // Log::FormatLine(LOG_TRACE, "CAM", "Cam p %f hdg %f ", outCameraPosition->pitch, outCameraPosition->heading); - outCameraPosition->roll = 0; - } - else - { - outCameraPosition->roll = campos->direction[0]; - outCameraPosition->pitch = campos->direction[1]; - outCameraPosition->heading = campos->direction[2]; - } - + outCameraPosition->roll = 0; + } + else + { + outCameraPosition->roll = campos->direction[0]; + outCameraPosition->pitch = campos->direction[1]; + outCameraPosition->heading = campos->direction[2]; + } + outCameraPosition->zoom = campos->zoom; } diff --git a/xpcPlugin/MessageHandlers.h b/xpcPlugin/MessageHandlers.h index 38fc12a..a065c96 100644 --- a/xpcPlugin/MessageHandlers.h +++ b/xpcPlugin/MessageHandlers.h @@ -38,8 +38,8 @@ namespace XPC /// Sets the socket that message handlers use to send responses. static void SetSocket(UDPSocket* socket); - - static void SendBeacon(const std::string& pluginVersion, unsigned short pluginReceivePort, int xplaneVersion); + + static void SendBeacon(const std::string& pluginVersion, unsigned short pluginReceivePort, int xplaneVersion); private: // One handler per message type. Message types are descripbed on the diff --git a/xpcPlugin/Timer.cpp b/xpcPlugin/Timer.cpp index ea56aa2..db90545 100644 --- a/xpcPlugin/Timer.cpp +++ b/xpcPlugin/Timer.cpp @@ -5,23 +5,23 @@ using namespace std; namespace XPC { - void Timer::start(const chrono::milliseconds interval, const Callback &callback) { - { - running.test_and_set(); - th = thread([=]() - { - while (running.test_and_set()) { - this_thread::sleep_for(interval); - callback(); - } - }); - } - } - - void Timer::stop() { - running.clear(); - if(th.joinable()) { - th.join(); - } - } + void Timer::start(const chrono::milliseconds interval, const Callback &callback) { + { + running.test_and_set(); + th = thread([=]() + { + while (running.test_and_set()) { + this_thread::sleep_for(interval); + callback(); + } + }); + } + } + + void Timer::stop() { + running.clear(); + if(th.joinable()) { + th.join(); + } + } } diff --git a/xpcPlugin/Timer.h b/xpcPlugin/Timer.h index fc31732..8fd909d 100644 --- a/xpcPlugin/Timer.h +++ b/xpcPlugin/Timer.h @@ -11,19 +11,19 @@ #include namespace XPC { - class Timer - { - std::atomic_flag running; - std::thread th; - - public: + class Timer + { + std::atomic_flag running; + std::thread th; + + public: - using Callback = std::function; - - void start(const std::chrono::milliseconds, const Callback &callback); - - void stop(); - }; + using Callback = std::function; + + void start(const std::chrono::milliseconds, const Callback &callback); + + void stop(); + }; } #endif /* Timer_hpp */ diff --git a/xpcPlugin/UDPSocket.cpp b/xpcPlugin/UDPSocket.cpp index 7361339..6eaf3e1 100644 --- a/xpcPlugin/UDPSocket.cpp +++ b/xpcPlugin/UDPSocket.cpp @@ -138,18 +138,18 @@ namespace XPC Log::FormatLine(LOG_INFO, tag, "Send succeeded. (remote: %s)", GetHost(remote).c_str()); } } - - sockaddr UDPSocket::GetAddr(std::string address, unsigned short port) - { - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(address.c_str()); - addr.sin_port = htons(port); + + sockaddr UDPSocket::GetAddr(std::string address, unsigned short port) + { + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr(address.c_str()); + addr.sin_port = htons(port); - sockaddr* sa = reinterpret_cast(&addr); - return *sa; - } + sockaddr* sa = reinterpret_cast(&addr); + return *sa; + } std::string UDPSocket::GetHost(sockaddr* sa) { diff --git a/xpcPlugin/UDPSocket.h b/xpcPlugin/UDPSocket.h index 67eeeb7..a1ee920 100644 --- a/xpcPlugin/UDPSocket.h +++ b/xpcPlugin/UDPSocket.h @@ -56,14 +56,14 @@ namespace XPC /// \param len The number of bytes to send. /// \param remote The destination socket. void SendTo(const unsigned char* buffer, std::size_t len, sockaddr* remote) const; - + /// Gets a string containing the IP address and port contained in the given sockaddr. /// /// \param addr The socket address to parse. /// \returns A string representation of the socket address. static std::string GetHost(sockaddr* addr); - - static sockaddr GetAddr(std::string address, unsigned short port); + + static sockaddr GetAddr(std::string address, unsigned short port); private: #ifdef _WIN32 SOCKET sock;