Skip to content

Commit

Permalink
Add base64 encoding for GCode prefix handling.
Browse files Browse the repository at this point in the history
Implemented a `convertTobase64` function to encode strings to base64, ensuring they can be safely transmitted in JavaScript. Updated `EmscriptenCommunication::sendGCodePrefix` to use this new encoding method and modified relevant tests accordingly to check this functionality.

NP-327
  • Loading branch information
saumyaj3 committed Aug 22, 2024
1 parent 030ee68 commit 5c564a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
28 changes: 28 additions & 0 deletions include/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <cstdio> // sprintf
#include <ctype.h>
#include <sstream> // ostringstream
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/ostream_iterator.hpp>

#include <spdlog/spdlog.h>

Expand All @@ -27,6 +30,31 @@ static inline int stringcasecompare(const char* a, const char* b)
return *a - *b;
}

// Convert string to base64 string.
// This function is useful to forward string through javascript even if they contain any special strings
//
[[maybe_unused]] static std::string convertTobase64(const std::string& input )
{
using namespace boost::archive::iterators;
// prepare the stream to hold the encoded data
std::stringstream output;

// encode data
typedef base64_from_binary<transform_width<std::string::const_iterator, 6, 8>> base64_enc;
std::copy(base64_enc(input.begin()), base64_enc(input.end()),
ostream_iterator<char>(output));

// Retrieve the encoded string
std::string output_encoded = output.str();

// ensure padding if needed
size_t num = (3 - input.length() % 3) % 3;
for(size_t i = 0; i < num; i++)
{
output_encoded.push_back('=');
}
return output_encoded;
}
/*!
* Efficient conversion of micron integer type to millimeter string.
*
Expand Down
4 changes: 2 additions & 2 deletions src/communication/EmscriptenCommunication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <range/v3/algorithm/contains.hpp>
#include <range/v3/iterator/operations.hpp>
#include <spdlog/spdlog.h>

#include "utils/string.h"
#include "Application.h"
#include "FffProcessor.h"
#include "Slice.h"
Expand Down Expand Up @@ -41,7 +41,7 @@ EmscriptenCommunication::EmscriptenCommunication(const std::vector<std::string>&

void EmscriptenCommunication::sendGCodePrefix(const std::string& prefix) const
{
emscripten_run_script(fmt::format("globalThis[\"{}\"](\"{}\")", gcode_header_handler_, prefix).c_str());
emscripten_run_script(fmt::format("globalThis[\"{}\"](\"{}\")", gcode_prefix_handler_, convertTobase64(prefix)).c_str());
}

void EmscriptenCommunication::sendProgress(double progress) const
Expand Down
8 changes: 5 additions & 3 deletions tests/arcus/ArcusCommunicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "geometry/Shape.h"
#include "settings/types/LayerIndex.h"
#include "utils/Coord_t.h"
#include "utils/string.h"

// NOLINTBEGIN(*-magic-numbers)
namespace cura
Expand Down Expand Up @@ -123,15 +124,16 @@ TEST_F(ArcusCommunicationTest, HasSlice)

TEST_F(ArcusCommunicationTest, SendGCodePrefix)
{
const std::string& prefix = "bladibla";
const std::string prefix = ";bladiblhjvouyvu\n;iuboua";
const std::string& encoded_prefix = convertTobase64(prefix);

ac->sendGCodePrefix(prefix);
ac->sendGCodePrefix(encoded_prefix);
ac->flushGCode();
EXPECT_GT(socket->sent_messages.size(), 0);
bool found_prefix = false;
for (const auto& message : socket->sent_messages)
{
if (message->DebugString().find(prefix) != std::string::npos)
if (message->DebugString().find(encoded_prefix) != std::string::npos)
{
found_prefix = true;
break;
Expand Down

0 comments on commit 5c564a3

Please sign in to comment.