From f120659e03578bcec7b20d242d07d1e53608f935 Mon Sep 17 00:00:00 2001 From: Stavros Malakoudis <stavrina25@gmail.com> Date: Thu, 1 Sep 2022 18:41:06 +0000 Subject: [PATCH] Add functionality to send created packets to Yamcs --- inc/Services/OnBoardMonitoringService.hpp | 2 +- src/Platform/x86/Service.cpp | 53 +++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/inc/Services/OnBoardMonitoringService.hpp b/inc/Services/OnBoardMonitoringService.hpp index 6af64f36..d67bf351 100644 --- a/inc/Services/OnBoardMonitoringService.hpp +++ b/inc/Services/OnBoardMonitoringService.hpp @@ -98,4 +98,4 @@ public: void execute(Message& message); }; -#endif // ECSS_SERVICES_ONBOARDMONITORINGSERVICE_HPP +#endif // ECSS_SERVICES_ONBOARDMONITORINGSERVICE_HPP \ No newline at end of file diff --git a/src/Platform/x86/Service.cpp b/src/Platform/x86/Service.cpp index 60786fc6..f316a764 100644 --- a/src/Platform/x86/Service.cpp +++ b/src/Platform/x86/Service.cpp @@ -1,7 +1,47 @@ -#include <iostream> -#include <iomanip> -#include <Logger.hpp> #include "Service.hpp" +#include <Logger.hpp> +#include <MessageParser.hpp> +#include <arpa/inet.h> +#include <iomanip> +#include <iostream> +#include <netinet/in.h> +#include <string> +#include <sys/socket.h> +#include <unistd.h> + +class PacketSender { +private: + const char* hostname = "127.0.0.1"; + const uint16_t port = 10015; + sockaddr_in destination; + int socket; + +public: + PacketSender() { + socket = ::socket(AF_INET, SOCK_DGRAM, 0); + destination.sin_family = AF_INET; + destination.sin_port = htons(port); + destination.sin_addr.s_addr = inet_addr(hostname); + }; + + ~PacketSender() { + ::close(socket); + }; + + void sendPacketToYamcs(Message& message) { + // Add ECSS and CCSDS header + String<CCSDSMaxMessageSize> createdPacket = MessageParser::compose(message); + auto bytesSent = ::sendto(socket, createdPacket.c_str(), createdPacket.length(), 0, reinterpret_cast<sockaddr*>(&destination), sizeof(destination)); + LOG_DEBUG << bytesSent << " bytes sent"; + } +}; + +PacketSender packetSender; + +/** + * If set to true, the created messages will be sent to port 10025 on localhost for testing purposes. + */ +inline const bool SendToYamcs = true; void Service::storeMessage(Message& message) { // appends the remaining bits to complete a byte @@ -14,12 +54,17 @@ void Service::storeMessage(Message& message) { ss << "New " << ((message.packetType == Message::TM) ? "TM" : "TC") << "[" << std::hex << static_cast<int>(message.serviceType) << "," // Ignore-MISRA - << static_cast<int>(message.messageType) // Ignore-MISRA + << static_cast<int>(message.messageType) // Ignore-MISRA << "] message! "; for (unsigned int i = 0; i < message.dataSize; i++) { ss << static_cast<int>(message.data[i]) << " "; // Ignore-MISRA } + + // Send data to YAMCS port + if constexpr (SendToYamcs) { + packetSender.sendPacketToYamcs(message); + } LOG_DEBUG << ss.str(); } -- GitLab