-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
201 lines (168 loc) · 4.69 KB
/
main.c
File metadata and controls
201 lines (168 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "usbms.h"
#include "config.h"
#define VERSION "1.0.0"
static int setpassword(const char *password) {
unsigned long slen = strlen(password);
if (slen < 2 || slen > 12) {
fprintf(
stderr,
"Password has to be at least 2 but no more than 12 characters long.\n");
return 1;
}
for (int i = 0; i < slen; i++) {
switch (password[i]) {
case '/':
case ':':
case '@':
case '[':
case '`':
case '{':
fprintf(stderr,
"Use of invalid character in password. Do not use `%c'.\n",
password[i]);
fprintf(stderr, "Invalid character for passwords are \"/:@[`{\".\n");
return 1;
}
}
printf("Updating password to `%s'... ", password);
if (send_command(ACTIONPRO_OPCODE_SETPASSWORD, (const uint8_t *)password,
slen) == ACTIONPRO_CMD_OK)
printf("OK\n");
else
printf("ERROR\n");
return 0;
}
static int setssid(const char *ssid) {
unsigned long slen = strlen(ssid);
if (slen < 2 || slen > 12) {
fprintf(stderr,
"SSID has to be at least 2 but no more than 12 characters long.\n");
return 1;
}
for (int i = 0; i < slen; i++) {
switch (ssid[i]) {
case ' ':
case '~':
fprintf(stderr, "Use of invalid character in SSID. Do not use `%c'.\n",
ssid[i]);
fprintf(stderr, "Invalid character for SSID are \" ~\".\n");
return 1;
}
}
printf("Updating SSID to `%s'... ", ssid);
if (send_command(ACTIONPRO_OPCODE_SETSSID, (const uint8_t *)ssid, slen) ==
ACTIONPRO_CMD_OK)
printf("OK\n");
else
printf("ERROR\n");
return 0;
}
static int settime() {
printf("Setting device time... ");
time_t curtime;
struct tm *timeinfo;
if (time(&curtime)) {
timeinfo = localtime(&curtime);
if (timeinfo) {
uint8_t newtime[8] = {0};
const uint16_t year =
(timeinfo->tm_year + 1900) >> 8 | (timeinfo->tm_year + 1900) << 8;
const uint8_t month = timeinfo->tm_mon + 1;
memcpy(newtime + 0, &year, 2);
memcpy(newtime + 2, &month, 1);
memcpy(newtime + 3, &timeinfo->tm_mday, 1);
memcpy(newtime + 4, &timeinfo->tm_hour, 1);
memcpy(newtime + 5, &timeinfo->tm_min, 1);
memcpy(newtime + 6, &timeinfo->tm_sec, 1);
if (send_command(ACTIONPRO_OPCODE_SETTIME, newtime, 8) ==
ACTIONPRO_CMD_OK)
printf("OK\n");
else
printf("ERROR\n");
return 0;
}
}
return 1;
}
int main(int argc, char *argv[]) {
int c;
bool show_help = false;
char *new_password = NULL;
char *new_ssid = NULL;
bool sync_time = false;
bool show_version = false;
printf("ACTIONPRO configuration utility\n");
while (1) {
static struct option long_options[] = {
{"help", no_argument, 0, 'p'},
{"password", required_argument, 0, 'p'},
{"ssid", required_argument, 0, 's'},
{"time", no_argument, 0, 't'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}};
int option_index = 0;
c = getopt_long(argc, argv, "hp:s:tv", long_options, &option_index);
if (argc < 2) {
fprintf(stderr, "At least one option is required. See -h, --help for "
"instructions.\n");
exit(EXIT_FAILURE);
}
if (c == -1)
break;
switch (c) {
case 'h':
show_help = true;
break;
case 'p':
new_password = optarg;
break;
case 's':
new_ssid = optarg;
break;
case 't':
sync_time = true;
break;
case 'v':
show_version = true;
break;
case '?':
fprintf(stderr, "Use option -h, --help for instructions.\n");
exit(EXIT_FAILURE);
default:
abort();
}
}
if (show_help) {
printf("Usage: %s [OPTION]\n", argv[0]);
printf(" -h, --help give this help list\n");
printf(" -p, --password=PASSWORD sets the access point authentication "
"PASSWORD\n");
printf(" -s, --ssid=SSID sets the access point SSID\n");
printf(" -t, --time synchronize the camera time\n");
printf(" -v, --version display version number\n");
printf("\nThis program requires write access to usb devices and might be "
"run as root.\n");
exit(EXIT_SUCCESS);
}
if (show_version) {
printf("Actionpro-cli version: %s\n", VERSION);
exit(EXIT_SUCCESS);
}
if (open_device() == ACTIONPRO_OK) {
if (new_password)
setpassword(new_password);
if (new_ssid)
setssid(new_ssid);
if (sync_time)
settime();
}
close_device();
exit(EXIT_SUCCESS);
}