fsfw/osal/windows/TmTcWinUdpBridge.cpp

177 lines
5.9 KiB
C++
Raw Normal View History

2020-09-06 11:58:12 +02:00
#include <fsfw/ipc/MutexHelper.h>
2020-09-06 09:33:02 +02:00
#include "TmTcWinUdpBridge.h"
TmTcWinUdpBridge::TmTcWinUdpBridge(object_id_t objectId,
object_id_t tcDestination, object_id_t tmStoreId, object_id_t tcStoreId,
uint16_t serverPort, uint16_t clientPort):
TmTcBridge(objectId, tcDestination, tmStoreId, tcStoreId) {
mutex = MutexFactory::instance()->createMutex();
2020-09-06 10:36:39 +02:00
// Initiates Winsock DLL.
WSAData wsaData;
WORD wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge:"
"WSAStartup failed with error: " << err << std::endl;
return;
}
2020-09-06 09:33:02 +02:00
uint16_t setServerPort = DEFAULT_UDP_SERVER_PORT;
if(serverPort != 0xFFFF) {
setServerPort = serverPort;
}
uint16_t setClientPort = DEFAULT_UDP_CLIENT_PORT;
if(clientPort != 0xFFFF) {
setClientPort = clientPort;
}
// Set up UDP socket: https://man7.org/linux/man-pages/man7/ip.7.html
//clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2020-09-06 11:48:32 +02:00
if(serverSocket == INVALID_SOCKET) {
2020-09-06 09:54:22 +02:00
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not open"
2020-09-06 09:33:02 +02:00
" UDP socket!" << std::endl;
handleSocketError();
return;
}
serverAddress.sin_family = AF_INET;
2020-09-06 12:00:57 +02:00
// Accept packets from any interface. (potentially insecure).
2020-09-06 09:33:02 +02:00
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(setServerPort);
2020-09-06 09:54:22 +02:00
serverAddressLen = sizeof(serverAddress);
2020-09-06 09:33:02 +02:00
setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const char*>(&serverSocketOptions),
sizeof(serverSocketOptions));
clientAddress.sin_family = AF_INET;
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
clientAddress.sin_port = htons(setClientPort);
2020-09-06 09:54:22 +02:00
clientAddressLen = sizeof(clientAddress);
2020-09-06 09:33:02 +02:00
int result = bind(serverSocket,
reinterpret_cast<struct sockaddr*>(&serverAddress),
serverAddressLen);
2020-09-06 10:36:39 +02:00
if(result != 0) {
sif::error << "TmTcWinUdpBridge::TmTcWinUdpBridge: Could not bind "
2020-09-06 09:33:02 +02:00
"local port " << setServerPort << " to server socket!"
<< std::endl;
handleBindError();
}
}
2020-09-06 10:36:39 +02:00
TmTcWinUdpBridge::~TmTcWinUdpBridge() {
WSACleanup();
2020-09-06 09:54:22 +02:00
}
ReturnValue_t TmTcWinUdpBridge::sendTm(const uint8_t *data, size_t dataLen) {
2020-09-06 11:58:12 +02:00
int flags = 0;
//clientAddress.sin_addr.s_addr = htons(INADDR_ANY);
//clientAddressLen = sizeof(serverAddress);
// char ipAddress [15];
// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
ssize_t bytesSent = sendto(serverSocket,
reinterpret_cast<const char*>(data), dataLen, flags,
reinterpret_cast<sockaddr*>(&clientAddress), clientAddressLen);
if(bytesSent == SOCKET_ERROR) {
sif::error << "TmTcWinUdpBridge::sendTm: Send operation failed."
<< std::endl;
handleSendError();
}
// sif::debug << "TmTcUnixUdpBridge::sendTm: " << bytesSent << " bytes were"
// " sent." << std::endl;
return HasReturnvaluesIF::RETURN_OK;
2020-09-06 09:54:22 +02:00
return HasReturnvaluesIF::RETURN_OK;
}
2020-09-06 11:58:12 +02:00
void TmTcWinUdpBridge::checkAndSetClientAddress(sockaddr_in newAddress) {
MutexHelper lock(mutex, MutexIF::TimeoutType::WAITING, 10);
// char ipAddress [15];
// sif::debug << "IP Address Sender: "<< inet_ntop(AF_INET,
// &newAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
// sif::debug << "IP Address Old: " << inet_ntop(AF_INET,
// &clientAddress.sin_addr.s_addr, ipAddress, 15) << std::endl;
// Set new IP address if it has changed.
if(clientAddress.sin_addr.s_addr != newAddress.sin_addr.s_addr) {
clientAddress.sin_addr.s_addr = newAddress.sin_addr.s_addr;
clientAddressLen = sizeof(clientAddress);
}
2020-09-06 09:54:22 +02:00
}
2020-09-06 10:36:39 +02:00
void TmTcWinUdpBridge::handleSocketError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
sif::info << "TmTcWinUdpBridge::handleSocketError: WSANOTINITIALISED: "
<< "WSAStartup(...) call " << "necessary" << std::endl;
break;
}
default: {
2020-09-06 11:58:12 +02:00
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2020-09-06 10:36:39 +02:00
sif::info << "TmTcWinUdpBridge::handleSocketError: Error code: "
<< errCode << std::endl;
break;
}
}
}
void TmTcWinUdpBridge::handleBindError() {
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
sif::info << "TmTcWinUdpBridge::handleBindError: WSANOTINITIALISED: "
<< "WSAStartup(...) call " << "necessary" << std::endl;
break;
}
default: {
2020-09-06 11:58:12 +02:00
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2020-09-06 10:36:39 +02:00
sif::info << "TmTcWinUdpBridge::handleBindError: Error code: "
<< errCode << std::endl;
break;
}
}
}
2020-09-06 09:54:22 +02:00
void TmTcWinUdpBridge::handleSendError() {
2020-09-06 10:36:39 +02:00
int errCode = WSAGetLastError();
switch(errCode) {
case(WSANOTINITIALISED): {
sif::info << "TmTcWinUdpBridge::handleSendError: WSANOTINITIALISED: "
<< "WSAStartup(...) call " << "necessary" << std::endl;
break;
}
2020-09-06 11:58:12 +02:00
case(WSAEADDRNOTAVAIL): {
sif::info << "TmTcWinUdpBridge::handleReadError: WSAEADDRNOTAVAIL: "
<< "Check target address. " << std::endl;
break;
}
2020-09-06 10:36:39 +02:00
default: {
2020-09-06 11:58:12 +02:00
/*
https://docs.microsoft.com/en-us/windows/win32/winsock/
windows-sockets-error-codes-2
*/
2020-09-06 10:36:39 +02:00
sif::info << "TmTcWinUdpBridge::handleSendError: Error code: "
<< errCode << std::endl;
break;
}
}
2020-09-06 09:54:22 +02:00
}
2020-09-06 10:36:39 +02:00