26#include "../../ccl/ccl_compatibility.h"
27#include "../../codec.h"
28#include "../../field_codec.h"
33using dccl::operator<<;
35bool 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);
46template <
typename N>
void check_normal_dccl(
dccl::Codec& codec)
49 codec.info<N>(&dccl::dlog);
50 N normal_msg, normal_msg_out;
51 normal_msg.set_a(123);
52 normal_msg.set_b(321);
55 codec.encode(&encoded, normal_msg);
56 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
dccl::hex_encode(encoded) << std::endl;
58 codec.decode(encoded, &normal_msg_out);
60 assert(normal_msg.SerializeAsString() == normal_msg_out.SerializeAsString());
63int main(
int argc,
char* argv[])
66 for (
int i = 1; i < argc; ++i)
68 if (argv[i] && argv[i][0] ==
'-' && argv[i][1] ==
'v' && argv[i][2] ==
'\0')
72 dccl::dlog.
connect(verbose ? dccl::logger::ALL :
dccl::logger::WARN_PLUS, &std::cerr);
74 dccl::Codec codec(
"dccl.ccl.id", DCCL_CCL_COMPAT_NAME);
76 check_normal_dccl<NormalDCCL1Byte>(codec);
77 check_normal_dccl<NormalDCCL2Byte>(codec);
79 codec.info<dccl::legacyccl::protobuf::CCLMDATState>(&dccl::dlog);
81 dccl::legacyccl::protobuf::CCLMDATState state_in, state_out;
82 std::string test_state_encoded =
83 "0e86fa11ad20c9011b4432bf47d10000002401042f0e7d87fa111620c95a200a";
85 state_in.set_latitude(25.282416667);
86 state_in.set_longitude(-77.164266667);
87 state_in.set_fix_age(4);
89 auto now = std::chrono::system_clock::now();
90 std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
91 std::tm input_tm = *std::gmtime(&now_time_t);
94 input_tm.tm_hour = 17;
97 input_tm.tm_isdst = -1;
98 std::time_t time_date = timegm(&input_tm);
100 state_in.set_time_date(dccl::legacyccl::TimeDateCodec::to_uint64_time(time_date));
101 state_in.set_heading(270);
102 state_in.set_depth(2323);
103 state_in.set_mission_mode(dccl::legacyccl::protobuf::CCLMDATState::NORMAL);
106 state_in.set_mission_leg(4);
107 state_in.set_est_velocity(1.88);
109 state_in.set_watts(500);
110 state_in.set_lat_goal(25.282440815262891);
111 state_in.set_lon_goal(-77.167505880296929);
112 state_in.set_battery_percent(90);
113 state_in.mutable_gfi_pitch_oil()->set_gfi(0);
114 state_in.mutable_gfi_pitch_oil()->set_pitch(6);
115 state_in.mutable_gfi_pitch_oil()->set_oil(55);
117 assert(double_cmp(state_in.latitude(), state_out.latitude(), 4));
118 assert(double_cmp(state_in.longitude(), state_out.longitude(), 4));
119 assert(state_in.fix_age() == state_out.fix_age());
120 assert(state_in.time_date() == state_out.time_date());
121 assert(dccl::round(state_in.heading(), 0) == dccl::round(state_out.heading(), 0));
122 assert(double_cmp(state_in.depth(), state_out.depth(), 1));
123 assert(state_in.mission_mode() == state_out.mission_mode());
126 std::shared_ptr<google::protobuf::Message> state_in_2 =
128 dccl::legacyccl::protobuf::CCLMDATState::descriptor());
129 state_in_2->CopyFrom(state_in);
131 std::string state_encoded;
132 codec.encode(&state_encoded, *state_in_2);
134 dccl::legacyccl::protobuf::CCLMDATState state_out_2;
135 codec.decode(state_encoded, &state_out_2);
137 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"in:" << state_in << std::endl;
138 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << test_state_encoded << std::endl;
139 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
dccl::hex_encode(state_encoded) << std::endl;
140 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << std::setprecision(16) << state_out.lon_goal() << std::endl;
141 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"out:" << state_out << std::endl;
142 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"out2: " << state_out_2 << std::endl;
144 assert(state_out.SerializeAsString() == state_out_2.SerializeAsString());
147 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
dccl::hex_encode(state_out.faults()) << std::endl;
148 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
dccl::hex_encode(state_out.faults_2()) << std::endl;
150 codec.info<dccl::legacyccl::protobuf::CCLMDATRedirect>(&dccl::dlog);
152 dccl::legacyccl::protobuf::CCLMDATRedirect redirect_in, redirect_out;
153 std::string test_redirect_encoded =
154 "07522cf9113d20c99964003d6464003d640be60014142035f911ef21c9000000";
156 redirect_in.set_message_number(82);
157 redirect_in.set_latitude(25.274995002149939);
158 redirect_in.set_longitude(-77.166669030984522);
159 redirect_in.set_transit_vertical_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::ALTITUDE);
160 redirect_in.set_transit_thrust_mode(
161 dccl::legacyccl::protobuf::CCLMDATRedirect::METERS_PER_SECOND);
162 redirect_in.set_survey_vertical_mode(dccl::legacyccl::protobuf::CCLMDATRedirect::ALTITUDE);
163 redirect_in.set_survey_thrust_mode(
164 dccl::legacyccl::protobuf::CCLMDATRedirect::METERS_PER_SECOND);
166 redirect_in.set_depth_goal_transit(10.0);
167 redirect_in.set_speed_transit(2.0333333);
168 redirect_in.set_device_cmd_transit(100);
170 redirect_in.set_depth_goal_survey(10.0);
171 redirect_in.set_speed_survey(2.0333333);
172 redirect_in.set_device_cmd_survey(100);
174 redirect_in.set_num_rows(11);
175 redirect_in.set_row_length(230);
176 redirect_in.set_spacing_0(20);
177 redirect_in.set_spacing_1(20);
178 redirect_in.set_heading(45.176472);
180 redirect_in.set_lat_start(25.275183333);
181 redirect_in.set_lon_start(-77.15735);
183 redirect_in.set_spare(std::string(3,
'\0'));
185 std::string redirect_encoded;
186 codec.encode(&redirect_encoded, redirect_in);
188 dccl::legacyccl::protobuf::CCLMDATRedirect redirect_out_2;
189 codec.decode(redirect_encoded, &redirect_out_2);
191 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"in:" << redirect_in << std::endl;
192 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog << test_redirect_encoded << std::endl;
193 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
dccl::hex_encode(redirect_encoded) << std::endl;
194 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"out:" << redirect_out << std::endl;
195 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"out2: " << redirect_out_2 << std::endl;
197 assert(redirect_out.SerializeAsString() == redirect_out_2.SerializeAsString());
200 codec.info<dccl::legacyccl::protobuf::CCLMDATEmpty>(&dccl::dlog);
201 codec.info<dccl::legacyccl::protobuf::CCLMDATBathy>(&dccl::dlog);
202 codec.info<dccl::legacyccl::protobuf::CCLMDATCTD>(&dccl::dlog);
203 codec.info<dccl::legacyccl::protobuf::CCLMDATError>(&dccl::dlog);
204 codec.info<dccl::legacyccl::protobuf::CCLMDATCommand>(&dccl::dlog);
206 dccl::dlog.
is(dccl::logger::INFO) && dccl::dlog <<
"all tests passed" << std::endl;
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
static GoogleProtobufMessagePointer new_protobuf_message(const std::string &protobuf_type_name, bool user_pool_first=false)
Create a new (empty) Google Protobuf message of a given type by name.
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Dynamic Compact Control Language namespace.
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...
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...