Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace AT
const std::string DELIMITER = "\r\n";
const std::string STATUS_OK = "OK";
const std::string RSP_READY = "READY";
const std::string GARBAGE = "\n";

const std::string CHECK_CONN = "AT";
const std::string SBD_SESSION = "AT+SBDIX"; // 5.144
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ custom_interfaces::msg::Path LocalTransceiver::receive()
continue;
}

if (!rcvRsps({message_to_queue_cmd, AT::Line("\n")})) {
if (!rcvRsps({message_to_queue_cmd, AT::Line("\n"), AT::Line("+SBDRB:"), AT::Line("\n")})) {
continue;
}

Expand All @@ -283,21 +283,25 @@ custom_interfaces::msg::Path LocalTransceiver::receive()
continue;
}

std::regex re(
R"(name=\"data\"; filename=\"[^\"]*\"\r?\n(?:.*\r?\n)*\r?\n([\s\S]*?)\r?\n--)", std::regex::ECMAScript);
std::string message_size_str;
std::string message;
std::string checksum;
uint16_t message_size_int = 0;

std::smatch match;

if (std::regex_search(*buffer_data, match, re)) {
*buffer_data = match[1];
std::stringstream ss;
ss << *buffer_data;
std::cout << ss.str() << std::endl;
if (buffer_data && buffer_data->size() >= 2) {
message_size_str = buffer_data->substr(0, 2);
} else {
std::cout << "No match found." << std::endl;
continue;
}

receivedDataBuffer = buffer_data.value();
message_size_int = (static_cast<uint8_t>(message_size_str[0]) << 8) | //NOLINT(readability-magic-numbers)
static_cast<uint8_t>(message_size_str[1]); //NOLINT(readability-magic-numbers)
message = buffer_data->substr(2, message_size_int);

receivedDataBuffer = message;

break;
}

Expand Down Expand Up @@ -367,7 +371,7 @@ std::optional<std::string> LocalTransceiver::readRsp()
error_code ec;

// Caution: will hang if another proccess is reading from serial port
bio::read_until(serial_, buf, AT::DELIMITER, ec);
bio::read_until(serial_, buf, AT::STATUS_OK, ec);
if (ec) {
return std::nullopt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,61 +227,71 @@ TEST_F(TestLocalTransceiver, parseInMsgValid)
EXPECT_EQ(parsed_test.waypoints[1].longitude, holder);
}

// std::mutex port_mutex;

// TEST_F(TestLocalTransceiver, testMailboxBlackbox)
// {
// std::lock_guard<std::mutex> lock(port_mutex); // because same port is being used

// std::string holder = "curl -X POST -F \"test=1234\" http://localhost:8080";
// std::string holder2 = "printf \"at+sbdix\r\" > $LOCAL_TRANSCEIVER_TEST_PORT";

// system(holder.c_str()); //NOLINT
// system(holder2.c_str()); //NOLINT

// std::optional<std::string> response = lcl_trns_->readRsp();
// std::cout << *response << std::endl;
// }

// TEST_F(TestLocalTransceiver, parseReceiveMessageBlackbox)
// {
// std::lock_guard<std::mutex> lock(port_mutex);

// constexpr float holder = 14.3;
// Polaris::GlobalPath sample_data;

// Polaris::Waypoint * waypoint_a = sample_data.add_waypoints();
// waypoint_a->set_latitude(holder);
// waypoint_a->set_longitude(holder);
// Polaris::Waypoint * waypoint_b = sample_data.add_waypoints();
// waypoint_b->set_latitude(holder);
// waypoint_b->set_longitude(holder);

// std::string serialized_data;
// ASSERT_TRUE(sample_data.SerializeToString(&serialized_data));

// std::ofstream outfile("/tmp/serialized_data.bin", std::ios::binary);
// outfile.write(serialized_data.data(), static_cast<std::streamsize>(serialized_data.size()));
// outfile.close();

// std::string holder2 = "curl -X POST -F \"data=@/tmp/serialized_data.bin\" http://localhost:8080";
// std::system(holder2.c_str()); //NOLINT

// custom_interfaces::msg::Path received_data = lcl_trns_->receive();

// Polaris::GlobalPath global_path;
// for (const auto & waypoint : received_data.waypoints) {
// Polaris::Waypoint * new_waypoint = global_path.add_waypoints();
// new_waypoint->set_latitude(waypoint.latitude);
// new_waypoint->set_longitude(waypoint.longitude);
// }

// if (global_path.waypoints_size() > 0) {
// ASSERT_EQ(global_path.waypoints_size(), sample_data.waypoints_size())
// << "Mismatch in number of waypoints received.";
// ASSERT_EQ(global_path.waypoints(0).latitude(), holder);
// ASSERT_EQ(global_path.waypoints(0).longitude(), holder);
// } else {
// std::cout << "No waypoints received." << std::endl;
// }
// }
std::mutex port_mutex;

TEST_F(TestLocalTransceiver, testMailboxBlackbox)
{
std::lock_guard<std::mutex> lock(port_mutex); // because same port is being used

std::string holder = "curl -X POST -F \"test=1234\" http://localhost:8080";
std::string holder2 = "printf \"at+sbdix\r\" > $LOCAL_TRANSCEIVER_TEST_PORT";

system(holder.c_str()); //NOLINT
system(holder2.c_str()); //NOLINT

std::optional<std::string> response = lcl_trns_->readRsp();
std::cout << *response << std::endl;
}

TEST_F(TestLocalTransceiver, parseReceiveMessageBlackbox)
{
std::lock_guard<std::mutex> lock(port_mutex);

constexpr float holder = 10.3;
Polaris::GlobalPath sample_data;

Polaris::Waypoint * waypoint_a = sample_data.add_waypoints();
waypoint_a->set_latitude(holder);
waypoint_a->set_longitude(holder);
Polaris::Waypoint * waypoint_b = sample_data.add_waypoints();
waypoint_b->set_latitude(holder);
waypoint_b->set_longitude(holder);

std::string serialized_data;
ASSERT_TRUE(sample_data.SerializeToString(&serialized_data));

uint16_t message_size = static_cast<uint16_t>(serialized_data.size());
uint16_t message_size_be = htons(message_size); // Convert to big-endian

std::string size_prefix(reinterpret_cast<const char *>(&message_size_be), sizeof(message_size_be));

std::ofstream outfile("/tmp/serialized_data.bin", std::ios::binary);
outfile.write(size_prefix.data(), size_prefix.size()); //NOLINT
outfile.write(serialized_data.data(), static_cast<std::streamsize>(serialized_data.size()));
outfile.close();

outfile.close(); // Close the file after writing

std::string holder2 = "curl -X POST --data-binary @/tmp/serialized_data.bin http://localhost:8080";
std::system(holder2.c_str()); //NOLINT
std::string test_cmd = "hexdump -C /tmp/serialized_data.bin";
std::system(test_cmd.c_str()); //NOLINT

custom_interfaces::msg::Path received_data = lcl_trns_->receive();

Polaris::GlobalPath global_path;
for (const auto & waypoint : received_data.waypoints) {
Polaris::Waypoint * new_waypoint = global_path.add_waypoints();
new_waypoint->set_latitude(waypoint.latitude);
new_waypoint->set_longitude(waypoint.longitude);
}

if (global_path.waypoints_size() > 0) {
ASSERT_EQ(global_path.waypoints_size(), sample_data.waypoints_size())
<< "Mismatch in number of waypoints received.";
ASSERT_EQ(global_path.waypoints(0).latitude(), holder);
ASSERT_EQ(global_path.waypoints(0).longitude(), holder);
} else {
std::cout << "No waypoints received." << std::endl;
}
}
Loading