From ed8d647935825bce06f62c5faa8a7d96138fad45 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Mon, 2 May 2016 01:24:23 +0200 Subject: [PATCH] fixing binary format in RTCM messages --- src/core/system_parameters/rtcm.cc | 13 ++++++++----- src/tests/formats/rtcm_test.cc | 10 +++++----- src/tests/gnss_block/rtcm_printer_test.cc | 7 ++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/core/system_parameters/rtcm.cc b/src/core/system_parameters/rtcm.cc index cd2aa8821..e716070ca 100644 --- a/src/core/system_parameters/rtcm.cc +++ b/src/core/system_parameters/rtcm.cc @@ -183,7 +183,7 @@ bool Rtcm::check_CRC(const std::string & message) const std::string Rtcm::bin_to_binary_data(const std::string& s) const { std::string s_aux; - int remainder = static_cast(std::fmod(s.length(), 4)); + int remainder = static_cast(std::fmod(s.length(), 8)); unsigned char c[s.length()]; unsigned int k = 0; if (remainder != 0) @@ -191,17 +191,20 @@ std::string Rtcm::bin_to_binary_data(const std::string& s) const s_aux.assign(s, 0 , remainder); boost::dynamic_bitset<> rembits(s_aux); unsigned long int n = rembits.to_ulong(); - c[0] = (unsigned char)n; + c[0] = static_cast(n); k++; } unsigned int start = std::max(remainder, 0); - for(unsigned int i = start; i < s.length() - 1; i = i + 4) + for(unsigned int i = start; i < s.length() - 1; i = i + 8) { s_aux.assign(s, i, 4); std::bitset<4> bs(s_aux); unsigned n = bs.to_ulong(); - c[k] = (unsigned char)n; + s_aux.assign(s, i + 4 , 4); + std::bitset<4> bs2(s_aux); + unsigned n2 = bs2.to_ulong(); + c[k] = static_cast(n * 16) + static_cast(n2); k++; } @@ -218,7 +221,7 @@ std::string Rtcm::binary_data_to_bin(const std::string& s) const for(unsigned int i = 0; i < s.length(); i++) { unsigned char val = static_cast(s.at(i)); - std::bitset<4> bs(val); + std::bitset<8> bs(val); ss << bs; } diff --git a/src/tests/formats/rtcm_test.cc b/src/tests/formats/rtcm_test.cc index 0da51d545..a07fdfc6d 100644 --- a/src/tests/formats/rtcm_test.cc +++ b/src/tests/formats/rtcm_test.cc @@ -158,13 +158,13 @@ TEST(Rtcm_Test, Bin_to_int) TEST(Rtcm_Test, Bin_to_binary_data) { auto rtcm = std::make_shared(); - std::string bin_str("11011010"); + std::string bin_str("1101101011010110"); std::string data_str = rtcm->bin_to_binary_data(bin_str); std::string test_binary = data_str.substr(0,1); std::string test_bin = rtcm->binary_data_to_bin(test_binary); std::string test_hex = rtcm->bin_to_hex(test_bin); - EXPECT_EQ(0, test_hex.compare("D")); + EXPECT_EQ(0, test_hex.compare("DA")); std::string recovered_str = rtcm->binary_data_to_bin(data_str); EXPECT_EQ(0, recovered_str.compare(bin_str)); @@ -283,12 +283,12 @@ TEST(Rtcm_Test, MT1029) unsigned int ref_id = 23; double obs_time = 0; Gps_Ephemeris gps_eph = Gps_Ephemeris(); - std::string m1029 = rtcm->print_MT1029(ref_id, gps_eph, obs_time, s_test); + std::string m1029 = rtcm->bin_to_hex(rtcm->binary_data_to_bin(rtcm->print_MT1029(ref_id, gps_eph, obs_time, s_test))); std::string encoded_text = m1029.substr(24, 60); - std::string expected_encoded_text(rtcm->bin_to_binary_data(rtcm->hex_to_bin("5554462D3820D0BFD180D0BED0B2D0B5D180D0BAD0B02077C3B672746572"))); + std::string expected_encoded_text("5554462D3820D0BFD180D0BED0B2D0B5D180D0BAD0B02077C3B672746572"); EXPECT_EQ(0, expected_encoded_text.compare(encoded_text)); - std::string characters_to_follow = rtcm->bin_to_hex(rtcm->binary_data_to_bin(m1029.substr(22, 2))); + std::string characters_to_follow = m1029.substr(22, 2); std::string expected_characters_to_follow("1E"); EXPECT_EQ(0, expected_characters_to_follow.compare(characters_to_follow)); } diff --git a/src/tests/gnss_block/rtcm_printer_test.cc b/src/tests/gnss_block/rtcm_printer_test.cc index 33e99cc39..3f2e2152d 100644 --- a/src/tests/gnss_block/rtcm_printer_test.cc +++ b/src/tests/gnss_block/rtcm_printer_test.cc @@ -77,11 +77,12 @@ TEST(Rtcm_Printer_Test, Run) /* Convert the reference message to binary data */ std::string reference_msg_binary; unsigned char c[1]; - for(unsigned int i = 0; i < reference_msg.length(); i++) + for(unsigned int i = 0; i < reference_msg.length(); i = i + 2) { - unsigned long int n; + unsigned long int n, n2; std::istringstream(reference_msg.substr(i,1)) >> std::hex >> n; - c[0] = (unsigned char)n; + std::istringstream(reference_msg.substr(i + 1, 1)) >> std::hex >> n2; + c[0] = static_cast(n * 16) + static_cast(n2); std::string ret(c, c+1); reference_msg_binary += ret; }