247 {
248 google::InitGoogleLogging(argv[0]);
249 google::ParseCommandLineFlags(&argc, &argv, true);
250
257 CANCardParameter can_client_conf_a;
258 std::shared_ptr<TestCanParam> param_ptr_a(new TestCanParam());
259 std::shared_ptr<TestCanParam> param_ptr_b(new TestCanParam());
260
261 auto can_client_factory = CanClientFactory::Instance();
262 can_client_factory->RegisterCanClients();
263
265 &can_client_conf_a)) {
266 AERROR <<
"Unable to load canbus conf file: "
267 << FLAGS_can_client_conf_file_a;
268 return -1;
269 }
270 AINFO <<
"Conf file is loaded: " << FLAGS_can_client_conf_file_a;
271 AINFO << can_client_conf_a.ShortDebugString();
272 auto client_a = can_client_factory->CreateObject(can_client_conf_a.brand());
273 if (!client_a || !client_a->Init(can_client_conf_a) ||
274 client_a->Start() != ErrorCode::OK) {
275 AERROR <<
"Create can client a failed.";
276 return -1;
277 }
278 param_ptr_a->can_client = client_a.get();
279 param_ptr_a->is_first_agent = true;
280 param_ptr_a->conf = can_client_conf_a;
281
282 CANCardParameter can_client_conf_b;
283 std::unique_ptr<CanClient> client_b;
284 if (!FLAGS_only_one_send) {
286 &can_client_conf_b)) {
287 AERROR <<
"Unable to load canbus conf file: "
288 << FLAGS_can_client_conf_file_b;
289 return -1;
290 }
291 AINFO <<
"Conf file is loaded: " << FLAGS_can_client_conf_file_b;
292 AINFO << can_client_conf_b.ShortDebugString();
293 client_b = can_client_factory->CreateObject(can_client_conf_b.brand());
294 if (!client_b || !client_b->Init(can_client_conf_b) ||
295 client_b->Start() != ErrorCode::OK) {
296 AERROR <<
"Create can client b failed.";
297 return -1;
298 }
299 param_ptr_b->can_client = client_b.get();
300 param_ptr_b->conf = can_client_conf_b;
301 }
302
303 CanAgent agent_a(param_ptr_a.get());
304 CanAgent agent_b(param_ptr_b.get());
305 agent_a.AddOtherAgent(&agent_b);
306 agent_b.AddOtherAgent(&agent_a);
307 if (!agent_a.Start()) {
308 AERROR <<
"Agent a start failed.";
309 return -1;
310 }
311 if (FLAGS_only_one_send) {
312 agent_b.is_receiving(true);
313 agent_b.is_sending_finish(true);
314 } else {
315 if (!agent_b.Start()) {
316 AERROR <<
"Agent b start failed.";
317 return -1;
318 }
319 }
320
321 agent_a.WaitForFinish();
322 if (!FLAGS_only_one_send) {
323 agent_b.WaitForFinish();
324 }
325 param_ptr_a->print();
326 if (!FLAGS_only_one_send) {
327 param_ptr_b->print();
328 }
329
330 return 0;
331}
CanClientFactory inherites apollo::common::util::Factory.
The class which defines the CAN client to send and receive message.
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...