]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-unix.c
Change filesystem detection to fstypename comparison instead of naked integer
[SourceForge/eyefi-config.git] / eyefi-unix.c
1 /*
2  * eyefi-unix.c
3  *
4  * Copyright (C) 2008 Dave Hansen <dave@sr71.net>
5  *
6  * This software may be redistributed and/or modified under the terms of
7  * the GNU General Public License ("GPL") version 2 as published by the
8  * Free Software Foundation.
9  */
10
11 #include "eyefi-config.h"
12
13 void print_pascal_string(struct pascal_string *str)
14 {
15         int i;
16         for (i = 0; i < str->length; i++)
17                 printf("%c", str->value[i]);
18 }
19
20 void print_mac(struct mac_address *mac)
21 {
22         int i;
23         for (i=0; i < MAC_BYTES-1; i++) {
24                 printf("%02x:", mac->mac[i]);
25         }
26         printf("%02x\n", mac->mac[i]);
27 }
28
29
30 void print_card_mac(void)
31 {
32         debug_printf(2, "%s()\n", __func__);
33         struct mac_address *mac;
34
35         card_info_cmd(MAC_ADDRESS);
36         mac = eyefi_response();
37         debug_printf(3, "%s() mac->length: %d\n", __func__, mac->length);
38         assert(mac->length == MAC_BYTES);
39         printf("card mac address: ");
40         print_mac(mac);
41 }
42
43 void print_card_firmware_info(void)
44 {
45         struct card_firmware_info *info = fetch_card_firmware_info();
46         printf("card firmware (len: %d): '", info->info.length);
47         print_pascal_string(&info->info);
48         printf("'\n");
49 }
50
51 void print_card_key(void)
52 {
53         debug_printf(2, "%s()\n", __func__);
54         struct card_info_rsp_key *foo = fetch_card_key();
55         printf("card key (len: %d): '", foo->key.length);
56         print_pascal_string(&foo->key);
57         printf("'\n");
58 }
59
60 void scan_print_nets(void)
61 {
62         int i;
63
64         debug_printf(2, "%s()\n", __func__);
65         struct scanned_net_list *scanned = scan_nets();
66         if (scanned->nr == 0) {
67                 printf("unable to detect any wireless networks\n");
68                 return;
69         }
70         printf("Scanned wireless networks:\n");
71         for (i=0; i < scanned->nr; i++) {
72                 struct scanned_net *net = &scanned->nets[i];
73                 printf("security: ");
74                 if (eyefi_debug_level > 1)
75                         printf("(%d)", net->type);
76                 printf("%4s, strength: %3d ", net_type_name(net->type),
77                                 net->strength);
78                 printf("essid: '%s'\n", net->essid);
79         }
80 }
81
82 void print_configured_nets(void)
83 {
84         int ret;
85         int i;
86         struct configured_net_list *configured = fetch_configured_nets();
87
88         debug_printf(2, "%s()\n", __func__);
89         ret = issue_noarg_command('l');
90         if (ret) {
91                 printf("error issuing print networks command: %d\n", ret);
92                 return;
93         }
94         configured = eyefi_response();
95         if (configured->nr == 0) {
96                 printf("No wireless networks configured on card\n");
97                 return;
98         }
99         printf("configured wireless networks:\n");
100         for (i=0; i < configured->nr; i++) {
101                 struct configured_net *net = &configured->nets[i];
102                 printf("'%s'\n", net->essid);
103         }
104 }
105
106 int try_connection_to(char *essid, char *ascii_password)
107 {
108         int i;
109         int ret = -1;
110
111         eyefi_printf("trying to connect to network: '%s'\n", essid);
112         if (ascii_password)
113                 eyefi_printf(" with passphrase: '%s'\n", ascii_password);
114         fflush(NULL);
115
116         // test network
117         ret = network_action('t', essid, ascii_password);
118         if (ret)
119                 return ret;
120         u8 last_rsp = -1;
121
122         char rsp = '\0';
123         ret = -1;
124         for (i=0; i < 200; i++) {
125                 struct byte_response *r;
126                 issue_noarg_command('s');
127                 r = eyefi_response();
128                 rsp = r->response;
129                 char *state = net_test_state_name(rsp);
130                 if (rsp == last_rsp) {
131                         eyefi_printf(".");
132                         fflush(NULL);;
133                 } else {
134                         if (rsp)
135                                 eyefi_printf("\nTesting connecion to '%s' (%d): %s", essid, rsp, state);
136                         last_rsp = rsp;
137                 }
138                 
139                 if (!strcmp("success", state)) {
140                         ret = 0;
141                         break;
142                 }
143                 if (!strcmp("not scanning", state))
144                         break;
145                 if (!strcmp("unknown", state))
146                         break;
147         }
148         eyefi_printf("\n");
149         if (!ret) {
150                 eyefi_printf("Succeeded connecting to: '%s'\n", essid);
151         } else {
152                 eyefi_printf("Unable to connect to: '%s' (final state: %d/'%s')\n", essid,
153                                 rsp, net_test_state_name(rsp));
154         }
155         return ret;
156 }
157
158 int print_log(void)
159 {
160         int i;
161         u8 *resbuf = malloc(EYEFI_BUF_SIZE*4);
162         int total_bytes;
163
164         total_bytes = get_log_into(resbuf);
165         if (total_bytes < 0) {
166                 debug_printf(1, "%s() error: %d\n", __func__, total_bytes);
167                 return total_bytes;
168         }
169         // The last byte *should* be a null, and the 
170         // official software does not print it.
171         for (i = 0; i < total_bytes-1; i++) {
172                 char c = resbuf[i];
173                 // the official software converts UNIX to DOS-style
174                 // line breaks, so we'll do the same
175                 if (c == '\n')
176                         printf("%c", '\r');
177                 printf("%c", c);
178         }
179         printf("\n");
180         // just some simple sanity checking to make sure what
181         // we are fetching looks valid
182         /* needs to be rethought for the new aligned logs
183         int null_bytes_left = 20;
184         if (resbuf[log_end] != 0) {
185                 debug_printf(2, "error: unexpected last byte (%ld/0x%lx) of log: %02x\n",
186                                 log_end, log_end, resbuf[log_end]);
187                 for (i=0; i<log_size; i++) {
188                         if (resbuf[i])
189                                 continue;
190                         if (null_bytes_left <= 0)
191                                 continue;
192                         null_bytes_left--;
193                         debug_printf(2, "null byte %d\n", i);
194                 }
195         }
196         */
197         return 0;
198 }
199
200 void open_error(char *file, int ret)
201 {
202         fprintf(stderr, "unable to open '%s' (%d)\n", file, ret);
203         fprintf(stderr, "Is the Eye-Fi card inserted and mounted at: %s ?\n", locate_eyefi_mount());
204         fprintf(stderr, "Do you have write permissions to it?\n");
205         fprintf(stderr, "debug information:\n");
206         if (eyefi_debug_level > 0)
207                 system("cat /proc/mounts >&2");
208         if (eyefi_debug_level > 1)
209                 perror("bad open");
210         exit(1);
211 }
212
213 void usage(void)
214 {
215         printf("Usage:\n");
216         printf("  eyefitest [OPTIONS]\n");
217         printf("  -a ESSID      add network (implies test unless --force)\n");
218         printf("  -t ESSID      test network\n");
219         printf("  -p KEY        set WPA key for add/test\n");
220         printf("  -r ESSID      remove network\n");
221         printf("  -s            scan for networks\n");
222         printf("  -c            list configured networks\n");
223         printf("  -b            reboot card\n");
224         printf("  -f            print information about card firmware\n");
225         printf("  -d level      set debugging level (default: 1)\n");
226         printf("  -k            print card unique key\n");
227         printf("  -l            dump card log\n");
228         printf("  -m            print card mac\n");
229         exit(4);
230 }
231
232 int main(int argc, char *argv[])
233 {
234         int option_index;
235         char c;
236         int cint;
237         char *essid = NULL;
238         char *passwd = NULL;
239         char network_action = 0;
240         static int force = 0;
241         static struct option long_options[] = {
242                 //{"wep", 'x', &passed_wep, 1},
243                 //{"wpa", 'y', &passed_wpa, 1},
244                 {"force", 0, &force, 1},
245                 {"help", 'h', NULL, 1},
246                 {0, 0, 0, 0}
247         };
248
249         if (argc == 1)
250                 usage();
251
252         debug_printf(3, "%s starting...\n", argv[0]);
253
254         debug_printf(3, "about to parse arguments\n");
255         debug_printf(4, "argc: %d\n", argc);
256         debug_printf(4, "argv: %p\n", argv);
257         while ((cint = getopt_long_only(argc, argv, "a:bcd:kflmp:r:st:z",
258                         &long_options[0], &option_index)) != -1) {
259                 c = cint;
260                 debug_printf(3, "argument: '%c' %d optarg: '%s'\n", c, c, optarg);
261                 switch (c) {
262                 case 0:
263                         // was a long argument
264                         break;
265                 case 'a':
266                 case 't':
267                 case 'r':
268                         essid = strdup(optarg);
269                         network_action = c;
270                         break;
271                 case 'b':
272                         reboot_card();
273                         break;
274                 case 'c':
275                         print_configured_nets();
276                         break;
277                 case 'd':
278                         eyefi_debug_level = atoi(optarg);
279                         fprintf(stderr, "set debug level to: %d\n", eyefi_debug_level);
280                         break;
281                 case 'f':
282                         print_card_firmware_info();
283                         break;
284                 case 'k':
285                         print_card_key();
286                         break;
287                 case 'l':
288                         print_log();
289                         break;
290                 case 'm':
291                         print_card_mac();
292                         break;
293                 case 'p':
294                         passwd = strdup(optarg);
295                         break;
296                 case 's':
297                         scan_print_nets();
298                         break;
299                 case 'z': {
300                         extern void testit0(void);
301                         testit0();
302                         break;
303                 }
304                 case 'h':
305                 default:
306                         usage();
307                         break;
308                 }
309         }
310
311         debug_printf(3, "after arguments1 essid: '%s' passwd: '%s'\n", essid, passwd);
312         if (network_action && essid) {
313                 int ret = 0;
314                 init_card();
315                 switch (network_action) {
316                 case 't':
317                         ret = try_connection_to(essid, passwd);
318                         break;
319                 case 'a':
320                         if (!force) {
321                                 ret = try_connection_to(essid, passwd);
322                         } else {
323                                 debug_printf(1, "forced: skipping network test\n");
324                         }
325                         if (ret) {
326                                 printf("Error connecting to network '%s', not adding.\n", essid);
327                                 printf("use --force to override\n");
328                                 break;
329                         }
330                         add_network(essid, passwd);
331                         break;
332                 case 'r':
333                         remove_network(essid);
334                         break;
335                 }
336         }
337
338         free(essid);
339         free(passwd);
340         return 0;
341 }
342
343