smrtlink.cpp 4.52 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
//============================================================================
// Name        : smrtlink.cpp
// Author      : jdi
// Version     :
// Copyright   : GPL v2
/jdi/'s avatar
/jdi/ committed
6
// Description : SmrtLink in C++, Ansi-style
/jdi/'s avatar
basics  
/jdi/ committed
7
8
9
10
//============================================================================

#include <cstring>
#include <iostream>
11
#include <sstream>
/jdi/'s avatar
basics  
/jdi/ committed
12
#include <string>
/jdi/'s avatar
/jdi/ committed
13
14
15
16
17
#include <cstdlib>
#include <cstdio>

#include <getopt.h>
#include <unistd.h>
/jdi/'s avatar
basics  
/jdi/ committed
18

/jdi/'s avatar
/jdi/ committed
19
#include "Options.h"
/jdi/'s avatar
/jdi/ committed
20
#include "Host.h"
/jdi/'s avatar
basics  
/jdi/ committed
21
22
23
24
25
26
#include "Program.h"

#define no_argument 0
#define required_argument 1
#define optional_argument 2

/jdi/'s avatar
/jdi/ committed
27
Options options;
/jdi/'s avatar
basics  
/jdi/ committed
28

/jdi/'s avatar
/jdi/ committed
29
30
31
32
constexpr unsigned int caseArg(const char* str, int h = 0) {
	return !str[h] ? 5381 : (caseArg(str, h + 1) * 33) ^ str[h];
}

/jdi/'s avatar
/jdi/ committed
33
34
int main(int argc, char *argv[]) {
	int opt, index;
/jdi/'s avatar
basics  
/jdi/ committed
35
36

	const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
/jdi/'s avatar
/jdi/ committed
37
			"help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, {
/jdi/'s avatar
/jdi/ committed
38
39
			"permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0,
			'd' }, { "password", required_argument, 0, 'p' }, { "user",
/jdi/'s avatar
/jdi/ committed
40
41
	required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, {
			"header", required_argument, 0, 'b' }, { "hex", required_argument,
/jdi/'s avatar
/jdi/ committed
42
			0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout",
/jdi/'s avatar
/jdi/ committed
43
44
	required_argument, 0, 't' }, { "wait",
	required_argument, 0, 'w' }, { 0, 0, 0, 0 }, };
/jdi/'s avatar
basics  
/jdi/ committed
45
46
47

	Program p = Program();

/jdi/'s avatar
/jdi/ committed
48
49
	while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts,
			&index)) != -1) {
/jdi/'s avatar
basics  
/jdi/ committed
50
51
52
		switch (opt) {

		case 'h':
/jdi/'s avatar
/jdi/ committed
53
54
55
56
			fprintf(stderr, VERSION);
			fprintf(stderr, USAGE, argv[0]);
			fprintf(stderr, HELP);
			exit(EXIT_SUCCESS);
/jdi/'s avatar
basics  
/jdi/ committed
57
58
59
			break;

		case 'v':
/jdi/'s avatar
/jdi/ committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
			fprintf(stderr, VERSION);
			exit(EXIT_SUCCESS);
			break;

		case 'r':
			options.flags |= FLAG_REVERSE;
			break;

		case 'b':
			options.flags |= FLAG_HEADER;
			break;

		case 'x':
			options.flags |= FLAG_HEX;
/jdi/'s avatar
basics  
/jdi/ committed
74
75
			break;

/jdi/'s avatar
/jdi/ committed
76
77
78
79
		case 's':
			options.flags |= FLAG_PERMANENT;
			break;

/jdi/'s avatar
/jdi/ committed
80
81
82
83
		case 'w':
			options.flags |= FLAG_WAIT;
			break;

/jdi/'s avatar
/jdi/ committed
84
85
86
87
88
89
		case 'd':
			options.flags |= FLAG_DEBUG;
			if (optarg != NULL)
				options.debug_level = atoi(optarg);
			break;

/jdi/'s avatar
/jdi/ committed
90
		case 't':
/jdi/'s avatar
/jdi/ committed
91
			options.timeout = atol(optarg);
/jdi/'s avatar
/jdi/ committed
92
93
94
95
96
97
			break;

		case 'f':
			options.file = std::string(optarg);
			break;

/jdi/'s avatar
basics  
/jdi/ committed
98
		case 'p':
/jdi/'s avatar
/jdi/ committed
99
			options.password = std::string(optarg);
/jdi/'s avatar
/jdi/ committed
100
101
102
			break;

		case 'u':
/jdi/'s avatar
/jdi/ committed
103
			options.user = std::string(optarg);
/jdi/'s avatar
basics  
/jdi/ committed
104
105
			break;

/jdi/'s avatar
/jdi/ committed
106
		case 'i':
/jdi/'s avatar
/jdi/ committed
107
			options.interface = std::string(optarg);
/jdi/'s avatar
basics  
/jdi/ committed
108
109
110
111
			break;

		default: /* '?' */
			fprintf(stderr, "Unknown option\n");
/jdi/'s avatar
/jdi/ committed
112
			fprintf(stderr, USAGE, argv[0]);
/jdi/'s avatar
basics  
/jdi/ committed
113
114
115
116
			exit(EXIT_FAILURE);
		}
	}

117
118
119
120
121
122
	/*//TODO stdin
	std::ostringstream bucket;
	bucket << std::cin.rdbuf();
	p.input = bucket.str();
	 */

/jdi/'s avatar
/jdi/ committed
123
124
125
	if (optind >= argc) {
		fprintf(stderr, "Command expected\n");
		fprintf(stderr, USAGE, argv[0]);
/jdi/'s avatar
basics  
/jdi/ committed
126
127
128
		exit(EXIT_FAILURE);
	}

/jdi/'s avatar
/jdi/ committed
129
130
	p.init();

/jdi/'s avatar
/jdi/ committed
131
	if (optind < argc) {
/jdi/'s avatar
/jdi/ committed
132
133
134
135
		std::string cmd = std::string(argv[optind++]);

		switch (caseArg(cmd.c_str())) {
		case caseArg("set"):
/jdi/'s avatar
/jdi/ committed
136
137
138
139
140
			if (p.setProperty())
				exit(EXIT_SUCCESS);
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
			break;
/jdi/'s avatar
/jdi/ committed
141
		case caseArg("reboot"):
/jdi/'s avatar
/jdi/ committed
142
143
144
145
146
			if (p.reboot())
				exit(EXIT_SUCCESS);
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
			break;
/jdi/'s avatar
/jdi/ committed
147
		case caseArg("reset"):
/jdi/'s avatar
/jdi/ committed
148
149
150
151
152
			if (p.reset())
				exit(EXIT_SUCCESS);
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
			break;
/jdi/'s avatar
/jdi/ committed
153
		case caseArg("save"):
/jdi/'s avatar
/jdi/ committed
154
155
156
157
158
			if (p.save())
				exit(EXIT_SUCCESS);
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
			break;
/jdi/'s avatar
/jdi/ committed
159
		case caseArg("restore"):
/jdi/'s avatar
/jdi/ committed
160
161
162
163
164
			if (p.restore())
				exit(EXIT_SUCCESS);
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
			break;
/jdi/'s avatar
/jdi/ committed
165
		case caseArg("flash"):
/jdi/'s avatar
/jdi/ committed
166
167
			if (p.flash())
				exit(EXIT_SUCCESS);
/jdi/'s avatar
/jdi/ committed
168
169
			fprintf(stderr, "Not yet implemented.\n");
			exit(EXIT_FAILURE);
/jdi/'s avatar
/jdi/ committed
170
171
			break;

/jdi/'s avatar
/jdi/ committed
172
173
174
175
176
		case caseArg("get"):
			if (p.getProperty())
				exit(EXIT_SUCCESS);
			break;

/jdi/'s avatar
/jdi/ committed
177
		case caseArg("list"):
/jdi/'s avatar
/jdi/ committed
178
179
			if (p.list())
				exit(EXIT_SUCCESS);
/jdi/'s avatar
/jdi/ committed
180
181
182
			break;

		case caseArg("sniff"):
/jdi/'s avatar
/jdi/ committed
183
184
			if (p.sniff())
				exit(EXIT_SUCCESS);
/jdi/'s avatar
/jdi/ committed
185
186
187
			break;

		case caseArg("encode"):
/jdi/'s avatar
/jdi/ committed
188
189
190
191
192
193
194
195
196
			if (optind < argc) {
				std::string s(argv[optind]);
				optind++;
				if (p.encode(s))
					exit(EXIT_SUCCESS);
			} else {
				fprintf(stderr, "Argument expected after encode\n");
				exit(EXIT_FAILURE);
			}
/jdi/'s avatar
/jdi/ committed
197
198
199
200
201
202
203
204
205
206
			break;

		case caseArg("help"):
			fprintf(stderr, VERSION);
			fprintf(stderr, USAGE, argv[0]);
			fprintf(stderr, HELP);
			exit(EXIT_SUCCESS);
			break;

		default:
/jdi/'s avatar
/jdi/ committed
207
			printf("Unknown command: %s\n", cmd.c_str());
/jdi/'s avatar
/jdi/ committed
208
209
210
211
212
			while (optind < argc) {
				printf("->%s\n", argv[optind]);
				optind++;
			}
			exit(EXIT_FAILURE);
/jdi/'s avatar
basics  
/jdi/ committed
213
214
		}
	}
/jdi/'s avatar
/jdi/ committed
215
	exit(EXIT_FAILURE);
/jdi/'s avatar
basics  
/jdi/ committed
216
217
}