Files
Ivan I. Ovchinnikov 8c4d40b66a protobuf server
2026-07-28 20:46:41 +03:00

73 lines
2.2 KiB
C++

#include "../include/PayloadWindow.h"
#include "../include/ConnectionWindow.h"
#include <cstdint>
#include "imgui.h"
#include <sstream>
#include <iomanip>
#include <vector>
PayloadWindow::PayloadWindow() {
std::snprintf(hexInputBuffer, sizeof(hexInputBuffer), "AA BB 01 02 03 FF");
}
void PayloadWindow::sendHexPayload(const std::string& hexString, ConnectionWindow& connectionWindow) {
struct sp_port* port = connectionWindow.getActivePort();
if (!connectionWindow.isConnected() || !port) return;
std::vector<std::uint8_t> bytes;
std::stringstream ss(hexString);
std::string byte_string;
while (ss >> byte_string) {
unsigned int byte_val;
std::stringstream converter;
converter << std::hex << byte_string;
if (converter >> byte_val) {
bytes.push_back(static_cast<std::uint8_t>(byte_val));
}
}
if (bytes.empty()) {
connectionWindow.forceLogMessage("[ERROR] No valid bytes found to send.");
return;
}
const int bytesWritten = sp_nonblocking_write(port, bytes.data(), bytes.size());
if (bytesWritten >= 0) {
std::stringstream log_ss;
log_ss << "[TX] Sent " << bytesWritten << " bytes: ";
for (unsigned char byte : bytes) {
log_ss << std::uppercase
<< std::hex
<< std::setw(2)
<< std::setfill('0')
<< static_cast<int>(byte)
<< " ";
}
connectionWindow.forceLogMessage(log_ss.str());
} else {
connectionWindow.forceLogMessage("[ERROR] Failed to write data to serial port.");
}
}
void PayloadWindow::render(bool* p_open, ConnectionWindow& connectionWindow) {
if (!ImGui::Begin("Raw Payload Terminal", p_open)) {
ImGui::End();
return;
}
ImGui::InputText("HEX Bytes", hexInputBuffer, IM_ARRAYSIZE(hexInputBuffer));
ImGui::TextDisabled("Example: AA BB 0F 45");
ImGui::Spacing();
ImGui::BeginDisabled(!connectionWindow.isConnected());
if (ImGui::Button("SEND PACKET", ImVec2(-1, 35))) {
sendHexPayload(hexInputBuffer, connectionWindow);
}
ImGui::EndDisabled();
ImGui::End();
}