cake
/
p7utils
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
p7utils/src/p7os/main.c

120 lines
3.8 KiB
C
Raw Normal View History

2016-12-22 17:38:55 +01:00
/* ************************************************************************** */
/* _____ _ */
2017-01-17 00:46:42 +01:00
/* p7os/main.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: p7utils | |/ _ \| | | | '_ \ / _ \ | | | */
2016-12-22 17:38:55 +01:00
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
2017-01-17 00:46:42 +01:00
/* Last updated: 2017/01/16 23:55:54 |___/ */
2016-12-22 17:38:55 +01:00
/* */
/* ************************************************************************** */
#include "main.h"
#include <unistd.h>
/* ************************************************************************** */
/* Error messages */
/* ************************************************************************** */
/* Couldn't initialize connexion to calculator. */
2017-01-17 00:46:42 +01:00
static const char error_noconnexion[] =
2016-12-22 17:38:55 +01:00
"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. */
2017-01-17 00:46:42 +01:00
static const char error_noaccess[] =
2016-12-22 17:38:55 +01:00
"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. */
2017-01-17 00:46:42 +01:00
static const char error_unplanned[] =
2016-12-22 17:38:55 +01:00
"The calculator didn't act as planned: %s.\n"
2017-01-11 15:46:18 +01:00
"Stop receive mode on calculator and start it again before re-running " \
QUOTE(BIN) ".\n";
2016-12-22 17:38:55 +01:00
/* Unsupported operation -> OS Update, not receive mode! */
2017-01-17 00:46:42 +01:00
static const char error_unsupported[] =
"Required operation was unsupported by the calculator.\n"
"If you did not prepare, perhaps you should prepare?\n";
2016-12-22 17:38:55 +01:00
/* ************************************************************************** */
/* Main function */
/* ************************************************************************** */
/**
* main:
* User entry point of the program.
*
* @arg ac arguments count
* @arg av arguments values
* @return return code (0 if OK)
*/
2017-01-27 00:10:39 +01:00
#define initflags (P7_ACTIVE | P7_CHECK | P7_TERM)
2016-12-22 17:38:55 +01:00
int main(int ac, char **av)
{
/* parse args */
args_t args;
if (parse_args(ac, av, &args))
return (0);
/* Initialize libp7 and communication */
2017-01-19 15:25:19 +01:00
p7_handle_t *handle = NULL; int err;
2017-01-27 00:10:39 +01:00
if (args.com) err = p7_cominit(&handle, initflags, args.com);
else err = p7_init(&handle, initflags);
2017-01-28 12:53:39 +01:00
if (err) goto fail;
2016-12-22 17:38:55 +01:00
2017-01-17 00:55:06 +01:00
/* prepare */
if (!args.noprepare) {
/* make the preparation thing */
2017-01-28 12:53:39 +01:00
if ((err = prepare_ops(handle, args.uexe)))
goto fail;
2017-01-17 00:55:06 +01:00
handle = NULL;
2016-12-22 17:38:55 +01:00
2017-01-17 00:55:06 +01:00
/* was only about preparing? */
if (args.menu == mn_prepare_only)
return (0);
/* sleep a little, in case */
sleep(1);
/* re-open the handle */
2017-01-27 00:10:39 +01:00
if (args.com) err = p7_cominit(&handle, initflags, args.com);
else err = p7_init(&handle, initflags);
2017-01-28 12:53:39 +01:00
if (err) goto fail;
2017-01-17 00:46:42 +01:00
}
2016-12-22 17:38:55 +01:00
/* check according to menu */
switch (args.menu) {
2017-01-17 00:55:06 +01:00
/* backup the thing menu */
2016-12-22 17:38:55 +01:00
case mn_get:
/* get the os */
2017-01-28 12:53:39 +01:00
err = p7_backup_rom(handle, args.local);
if (err) goto fail;
2016-12-22 17:38:55 +01:00
fclose(args.local);
break;
}
2017-01-17 00:55:06 +01:00
/* exit libp7 */
2017-01-27 00:10:39 +01:00
p7_exit(handle);
2016-12-22 17:38:55 +01:00
/* then we're good */
return (0);
2017-01-17 00:55:06 +01:00
2017-01-28 12:53:39 +01:00
fail:
2017-01-17 00:55:06 +01:00
/* displaying error */
2017-01-28 12:53:39 +01:00
if (err > 0) switch (err) {
2017-01-17 00:55:06 +01:00
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 the handle */
p7_exit(handle);
/* closing the file, removing if necessary */
2017-01-17 00:55:06 +01:00
if (args.localpath) {
fclose(args.local);
remove(args.localpath);
}
return (1);
2016-12-22 17:38:55 +01:00
}