Files
serial-imgui/sources/PayloadWindow.cpp
T
Ivan I. Ovchinnikov 47b1e70663 initial
2026-07-27 21:15:36 +03:00

68 lines
2.0 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(hex_input_buffer, sizeof(hex_input_buffer), "AA BB 01 02 03 FF");
}
void PayloadWindow::SendHexPayload(const std::string& hex_str, ConnectionWindow& conn_win) {
struct sp_port* port = conn_win.GetActivePort();
if (!conn_win.IsConnected() || !port) return;
std::vector<std::uint8_t> bytes;
std::stringstream ss(hex_str);
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()) {
conn_win.ForceLogMessage("[ERROR] No valid bytes found to send.");
return;
}
int bytes_written = sp_nonblocking_write(port, bytes.data(), bytes.size());
if (bytes_written >= 0) {
std::stringstream log_ss;
log_ss << "[TX] Sent " << bytes_written << " bytes: ";
for (size_t i = 0; i < bytes.size(); ++i) {
log_ss << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << (int)bytes[i] << " ";
}
conn_win.ForceLogMessage(log_ss.str());
} else {
conn_win.ForceLogMessage("[ERROR] Failed to write data to serial port.");
}
}
void PayloadWindow::Render(bool* p_open, ConnectionWindow& conn_win) {
if (!ImGui::Begin("Raw Payload Terminal", p_open)) {
ImGui::End();
return;
}
ImGui::InputText("HEX Bytes", hex_input_buffer, IM_ARRAYSIZE(hex_input_buffer));
ImGui::TextDisabled("Example: AA BB 0F 45");
ImGui::Spacing();
ImGui::BeginDisabled(!conn_win.IsConnected());
if (ImGui::Button("SEND PACKET", ImVec2(-1, 35))) {
SendHexPayload(hex_input_buffer, conn_win);
}
ImGui::EndDisabled();
ImGui::End();
}