#include "../include/PayloadWindow.h" #include "../include/ConnectionWindow.h" #include #include "imgui.h" #include #include #include 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 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(byte_val)); } } if (bytes.empty()) { connectionWindow.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] << " "; } 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(); }