/* ************************************************************************** */ /* _____ _ */ /* p7os/main.c |_ _|__ _ _| |__ ___ _ _ */ /* | Project: p7utils | |/ _ \| | | | '_ \ / _ \ | | | */ /* | | (_) | |_| | | | | __/ |_| | */ /* By: thomas |_|\___/ \__,_|_| |_|\___|\__, |.fr */ /* Last updated: 2017/01/16 23:55:54 |___/ */ /* */ /* ************************************************************************** */ #include "main.h" #include /* ************************************************************************** */ /* Error messages */ /* ************************************************************************** */ /* Couldn't initialize connexion to calculator. */ static const char error_noconnexion[] = "Could not connect to the calculator.\n" "- Is it plugged in and in Receive mode/OS Update?\n" "- Have you tried changing the cable ?\n"; /* Calculator was found but program wasn't allowed to communicate with it. */ static const char error_noaccess[] = "Could not get access to the calculator.\n" "Install the appropriate udev rule, or run as root.\n"; /* The calculator acted in a weird way. */ static const char error_unplanned[] = "The calculator didn't act as planned: %s.\n" "Stop receive mode on calculator and start it again before re-running " \ QUOTE(BIN) ".\n"; /* Unsupported operation -> OS Update, not receive mode! */ static const char error_unsupported[] = "Required operation was unsupported by the calculator.\n" "If you did not prepare, perhaps you should prepare?\n"; /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ /** * main: * User entry point of the program. * * @arg ac arguments count * @arg av arguments values * @return return code (0 if OK) */ #define initflags (P7_ACTIVE | P7_CHECK | P7_TERM) int main(int ac, char **av) { /* parse args */ args_t args; if (parse_args(ac, av, &args)) return (0); /* Initialize libp7 and communication */ p7_handle_t *handle = NULL; int err; if (args.com) err = p7_cominit(&handle, initflags, args.com); else err = p7_init(&handle, initflags); if (err) goto fail; /* prepare */ if (!args.noprepare) { /* make the preparation thing */ if ((err = prepare_ops(handle, args.uexe))) goto fail; handle = NULL; /* was only about preparing? */ if (args.menu == mn_prepare_only) return (0); /* sleep a little, in case */ sleep(1); /* re-open the handle */ if (args.com) err = p7_cominit(&handle, initflags, args.com); else err = p7_init(&handle, initflags); if (err) goto fail; } /* check according to menu */ switch (args.menu) { /* backup the thing menu */ case mn_get: /* get the os */ err = p7_backup_rom(handle, args.local); if (err) goto fail; fclose(args.local); break; } /* exit libp7 */ p7_exit(handle); /* then we're good */ return (0); fail: /* displaying error */ if (err > 0) switch (err) { case p7_error_nocalc: log(error_noconnexion); break; case p7_error_noaccess: log(error_noaccess); break; case p7_error_unsupported: log(error_unsupported); break; default: log(error_unplanned, p7_strerror(err)); } /* closing, removing if necessary */ if (args.localpath) { fclose(args.local); remove(args.localpath); } return (1); }