main.cpp revision 72163587ac7cab93d0daca22a20a018dfa974633
1#include <stdio.h>
2#include <string.h>
3#include <errno.h>
4#include <getopt.h>
5
6#include "hiddevice.h"
7#include "rmi4update.h"
8
9#define RMI4UPDATE_GETOPTS	"hf"
10
11void printHelp(const char *prog_name)
12{
13	fprintf(stdout, "Usage: %s [OPTIONS] DEVICEFILE FIRMWAREFILE\n", prog_name);
14	fprintf(stdout, "\t-h, --help\tPrint this message\n");
15	fprintf(stdout, "\t-f, --force\tForce updating firmware even it the image provided is older\n\t\t\tthen the current firmware on the device.\n");
16}
17
18int main(int argc, char **argv)
19{
20	int rc;
21	HIDDevice rmidevice;
22	FirmwareImage image;
23	int opt;
24	int index;
25	const char *deviceName = NULL;
26	const char *firmwareName = NULL;
27	bool force = false;
28	static struct option long_options[] = {
29		{"help", 0, NULL, 'h'},
30		{"force", 0, NULL, 'f'},
31		{0, 0, 0, 0},
32	};
33
34	while ((opt = getopt_long(argc, argv, RMI4UPDATE_GETOPTS, long_options, &index)) != -1) {
35		switch (opt) {
36			case 'h':
37				printHelp(argv[0]);
38				return 0;
39			case 'f':
40				force = true;
41				break;
42			default:
43				break;
44
45		}
46	}
47
48	if (optind < argc)
49		deviceName = argv[optind++];
50
51	if (optind < argc)
52		firmwareName = argv[optind];
53
54	rc = rmidevice.Open(deviceName);
55	if (rc) {
56		fprintf(stderr, "Failed to open rmi device: %s\n", strerror(errno));
57		return rc;
58	}
59
60	rc = image.Initialize(firmwareName);
61	if (rc != UPDATE_SUCCESS) {
62		fprintf(stderr, "Failed to initialize the firmware image: %s\n", update_err_to_string(rc));
63		return 1;
64	}
65
66
67
68	RMI4Update update(rmidevice, image);
69	rc = update.UpdateFirmware(force);
70	if (rc != UPDATE_SUCCESS) {
71		fprintf(stderr, "Firmware update failed because: %s\n", update_err_to_string(rc));
72		return 1;
73	}
74
75	return 0;
76}
77