24 #include <boost/date_time.hpp> 26 #include "dccl/codec.h" 27 #include "dccl/field_codec.h" 28 #include "dccl/ccl/ccl_compatibility.h" 33 using dccl::operator<<;
35 bool double_cmp(
double a,
double b,
int precision)
40 int a_part = (a-a_whole)*pow(10.0, precision);
41 int b_part = (b-b_whole)*pow(10.0, precision);
43 return (a_whole == b_whole) && (a_part == b_part);
50 codec.
info<N>(&dccl::dlog);
51 N normal_msg, normal_msg_out;
52 normal_msg.set_a(123);
53 normal_msg.set_b(321);
56 codec.
encode(&encoded, normal_msg);
59 codec.
decode(encoded, &normal_msg_out);
61 assert(normal_msg.SerializeAsString() == normal_msg_out.SerializeAsString());
65 int main(
int argc,
char* argv[])
67 dccl::dlog.
connect(dccl::logger::ALL, &std::cerr);
69 dccl::Codec codec(
"dccl.ccl.id", DCCL_CCL_COMPAT_NAME);
71 check_normal_dccl<NormalDCCL1Byte>(codec);
72 check_normal_dccl<NormalDCCL2Byte>(codec);
77 std::string test_state_encoded =
"0e86fa11ad20c9011b4432bf47d10000002401042f0e7d87fa111620c95a200a";
79 state_in.set_latitude(25.282416667);
80 state_in.set_longitude(-77.164266667);
81 state_in.set_fix_age(4);
83 boost::gregorian::date today = boost::gregorian::day_clock::universal_day();
84 boost::posix_time::ptime time_date(
85 boost::gregorian::date(today.year(), boost::date_time::Mar, 4),
86 boost::posix_time::time_duration(17,1,44));
88 state_in.set_time_date(dccl::legacyccl::TimeDateCodec::to_uint64_time(time_date));
89 state_in.set_heading(270);
90 state_in.set_depth(2323);
91 state_in.set_mission_mode(dccl::legacyccl::protobuf::CCLMDATState::NORMAL);
94 state_in.set_mission_leg(4);
95 state_in.set_est_velocity(1.88);
97 state_in.set_watts(500);
98 state_in.set_lat_goal(25.282440815262891);
99 state_in.set_lon_goal(-77.167505880296929);
100 state_in.set_battery_percent(90);
101 state_in.mutable_gfi_pitch_oil()->set_gfi(0);
102 state_in.mutable_gfi_pitch_oil()->set_pitch(6);
103 state_in.mutable_gfi_pitch_oil()->set_oil(55);
106 assert(double_cmp(state_in.latitude(), state_out.latitude(), 4));
107 assert(double_cmp(state_in.longitude(), state_out.longitude(), 4));
108 assert(state_in.fix_age() == state_out.fix_age());
109 assert(state_in.time_date() == state_out.time_date());
110 assert(dccl::round(state_in.heading(),0) ==
111 dccl::round(state_out.heading(),0));
112 assert(double_cmp(state_in.depth(), state_out.depth(), 1));
113 assert(state_in.mission_mode() == state_out.mission_mode());
118 state_in_2->CopyFrom(state_in);
120 std::string state_encoded;
121 codec.
encode(&state_encoded, *state_in_2);
124 codec.
decode(state_encoded, &state_out_2);
126 std::cout <<
"in:" << state_in << std::endl;
127 std::cout << test_state_encoded << std::endl;
129 std::cout << std::setprecision(16) << state_out.lon_goal() << std::endl;
130 std::cout <<
"out:" << state_out << std::endl;
131 std::cout <<
"out2: " << state_out_2 << std::endl;
133 assert(state_out.SerializeAsString() == state_out_2.SerializeAsString());
143 std::string test_redirect_encoded =
"07522cf9113d20c99964003d6464003d640be60014142035f911ef21c9000000";
145 redirect_in.set_message_number(82);
146 redirect_in.set_latitude(25.274995002149939);
147 redirect_in.set_longitude(-77.166669030984522);
148 redirect_in.set_transit_vertical_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::ALTITUDE);
149 redirect_in.set_transit_thrust_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::METERS_PER_SECOND);
150 redirect_in.set_survey_vertical_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::ALTITUDE);
151 redirect_in.set_survey_thrust_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::METERS_PER_SECOND);
153 redirect_in.set_depth_goal_transit(10.0);
154 redirect_in.set_speed_transit(2.0333333);
155 redirect_in.set_device_cmd_transit(100);
157 redirect_in.set_depth_goal_survey(10.0);
158 redirect_in.set_speed_survey(2.0333333);
159 redirect_in.set_device_cmd_survey(100);
161 redirect_in.set_num_rows(11);
162 redirect_in.set_row_length(230);
163 redirect_in.set_spacing_0(20);
164 redirect_in.set_spacing_1(20);
165 redirect_in.set_heading(45.176472);
167 redirect_in.set_lat_start(25.275183333);
168 redirect_in.set_lon_start(-77.15735);
170 redirect_in.set_spare(std::string(3,
'\0'));
172 std::string redirect_encoded;
173 codec.
encode(&redirect_encoded, redirect_in);
176 codec.
decode(redirect_encoded, &redirect_out_2);
178 std::cout <<
"in:" << redirect_in << std::endl;
179 std::cout << test_redirect_encoded << std::endl;
181 std::cout <<
"out:" << redirect_out << std::endl;
182 std::cout <<
"out2: " << redirect_out_2 << std::endl;
184 assert(redirect_out.SerializeAsString() == redirect_out_2.SerializeAsString());
195 std::cout <<
"all tests passed" << std::endl;
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
static GoogleProtobufMessagePointer new_protobuf_message(const std::string &protobuf_type_name)
Create a new (empty) Google Protobuf message of a given type by name.
void load()
All messages must be explicited loaded and validated (size checks, option extensions checks...
void info(std::ostream *os=0) const
Writes a human readable summary (including field sizes) of the provided DCCL type to the stream provi...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar) ...
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
CharIterator decode(CharIterator begin, CharIterator end, google::protobuf::Message *msg, bool header_only=false)
Decode a DCCL message when the type is known at compile time.
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
Encodes a DCCL message.