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

113 lines
3.8 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* main.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: p7os | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/09/28 06:00:10 |___/ */
/* */
/* ************************************************************************** */
#include "main.h"
#include <unistd.h>
/* ************************************************************************** */
/* Error messages */
/* ************************************************************************** */
/* Couldn't initialize connexion to calculator. */
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. */
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. */
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 p7os.\n";
/* Unsupported operation -> OS Update, not receive mode! */
const char error_unsupported[] =
"Required operation was unsupported.\n"
"- If you were using anything else than the *prepare* subcommand, are you\n"
" sure you've run *prepare* before?\n"
"- Otherwise, I gotta be honest, I'd like to know how you've managed that.\n";
/* ************************************************************************** */
/* Main function */
/* ************************************************************************** */
/**
* main:
* User entry point of the program.
*
* @arg ac arguments count
* @arg av arguments values
* @return return code (0 if OK)
*/
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; int err;
if (args.device) err = p7_cinit(&handle, 1, 1, args.device, 0);
else err = p7_init(&handle, 1, 1);
if (err) {
/* displaying error */
switch (err) {
case p7_error_nocalc: log(error_noconnexion);
case p7_error_noaccess: log(error_noaccess);
case p7_error_unsupported: log(error_unsupported);
default: log(error_unplanned, p7_strerror(err));
}
/* closing, removing if necessary */
if (args.localpath) {
fclose(args.local);
remove(args.localpath);
}
return (1);
}
/* check according to menu */
FILE *update_exe;
switch (args.menu) {
/* prepare menu */
case mn_prepare:;
/* open memory stream for the update.exe */
size_t uexe_size = (size_t)&update_exe_end - (size_t)&update_exe_str;
update_exe = fmemopen(update_exe_str, uexe_size, "r");
if (!update_exe) break;
/* send the update.exe */
if ((err = p7_sendexe_stream(handle, update_exe, (p7uint_t)uexe_size,
0x88030000, 0x88030000)))
fprintf(stderr, "An error has occurred: %s\n", p7_strerror(err));
break;
/* get menu */
case mn_get:
/* get the os */
err = !get_os(handle, args.local);
fclose(args.local);
/* if there was an error, delete created file */
if (err) unlink(args.localpath);
break;
}
/* exit libp7
* due to the nature of command 0x56,
* no need to terminate communication! */
p7_exit(handle, 0);
/* then we're good */
return (0);
}