int main(int argc, char **argv) { NarrowString<1024> usage; usage = "DRIVER PORT BAUD FILE.igc [FLIGHT NR]\n\n" "Where DRIVER is one of:"; { const DeviceRegister *driver; for (unsigned i = 0; (driver = GetDriverByIndex(i)) != NULL; ++i) { if (driver->IsLogger()) { NarrowPathName driver_name(driver->name); usage.AppendFormat("\n\t%s", (const char *)driver_name); } } } Args args(argc, argv, usage); PathName driver_name(args.ExpectNext()); PathName port_name(args.ExpectNext()); DeviceConfig config; config.Clear(); config.baud_rate = atoi(args.ExpectNext()); PathName path(args.ExpectNext()); unsigned flight_id = args.IsEmpty() ? 0 : atoi(args.GetNext()); args.ExpectEnd(); #ifdef HAVE_POSIX TTYPort port(port_name, config.baud_rate, *(Port::Handler *)NULL); #else SerialPort port(port_name, config.baud_rate, *(Port::Handler *)NULL); #endif if (!port.Open()) { fprintf(stderr, "Failed to open COM port\n"); return EXIT_FAILURE; } ConsoleOperationEnvironment env; const struct DeviceRegister *driver = FindDriverByName(driver_name); if (driver == NULL) { fprintf(stderr, "No such driver: %s\n", argv[1]); args.UsageError(); } if (!driver->IsLogger()) { fprintf(stderr, "Not a logger driver: %s\n", argv[1]); args.UsageError(); } assert(driver->CreateOnPort != NULL); Device *device = driver->CreateOnPort(config, port); assert(device != NULL); if (!device->Open(env)) { delete device; fprintf(stderr, "Failed to open driver: %s\n", argv[1]); return EXIT_FAILURE; } if (!device->EnableDownloadMode()) { delete device; fprintf(stderr, "Failed to enable download mode\n"); return EXIT_FAILURE; } RecordedFlightList flight_list; if (!device->ReadFlightList(flight_list, env)) { device->DisableDownloadMode(); delete device; fprintf(stderr, "Failed to download flight list\n"); return EXIT_FAILURE; } if (flight_list.empty()) { device->DisableDownloadMode(); delete device; fprintf(stderr, "Logger is empty\n"); return EXIT_FAILURE; } PrintFlightList(flight_list); if (flight_id >= flight_list.size()) { device->DisableDownloadMode(); delete device; fprintf(stderr, "Flight id not found\n"); return EXIT_FAILURE; } if (!device->DownloadFlight(flight_list[flight_id], path, env)) { device->DisableDownloadMode(); delete device; fprintf(stderr, "Failed to download flight\n"); return EXIT_FAILURE; } device->DisableDownloadMode(); delete device; printf("Flight downloaded successfully\n"); return EXIT_SUCCESS; }
int main(int argc, char **argv) { if (argc != 4) { fprintf(stderr, "Usage: %s DRIVER PORT BAUD\n" "Where DRIVER is one of:\n", argv[0]); const struct DeviceRegister *driver; for (unsigned i = 0; (driver = GetDriverByIndex(i)) != NULL; ++i) if (driver->IsLogger()) _ftprintf(stderr, _T("\t%s\n"), driver->name); return EXIT_FAILURE; } PathName driver_name(argv[1]); PathName port_name(argv[2]); DeviceConfig config; config.Clear(); config.baud_rate = atoi(argv[3]); #ifdef HAVE_POSIX TTYPort port(port_name, config.baud_rate, *(Port::Handler *)NULL); #else SerialPort port(port_name, config.baud_rate, *(Port::Handler *)NULL); #endif if (!port.Open()) { fprintf(stderr, "Failed to open COM port\n"); return EXIT_FAILURE; } ConsoleOperationEnvironment env; RecordedFlightList flight_list; const struct DeviceRegister *driver = FindDriverByName(driver_name); if (driver == NULL) { fprintf(stderr, "No such driver: %s\n", argv[1]); return EXIT_FAILURE; } if (!driver->IsLogger()) { fprintf(stderr, "Not a logger driver: %s\n", argv[1]); return EXIT_FAILURE; } assert(driver->CreateOnPort != NULL); Device *device = driver->CreateOnPort(config, port); assert(device != NULL); if (!device->Open(env)) { delete device; fprintf(stderr, "Failed to open driver: %s\n", argv[1]); return EXIT_FAILURE; } if (!device->EnableDownloadMode()) { delete device; fprintf(stderr, "Failed to enable download mode\n"); return EXIT_FAILURE; } if (!device->ReadFlightList(flight_list, env)) { device->DisableDownloadMode(); delete device; fprintf(stderr, "Failed to download flight list\n"); return EXIT_FAILURE; } device->DisableDownloadMode(); delete device; for (auto i = flight_list.begin(); i != flight_list.end(); ++i) { const RecordedFlightInfo &flight = *i; printf("%04u/%02u/%02u %02u:%02u-%02u:%02u\n", flight.date.year, flight.date.month, flight.date.day, flight.start_time.hour, flight.start_time.minute, flight.end_time.hour, flight.end_time.minute); } }