]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-unix.c
Replace O_DIRECT with posix_fadvise(). This is easier to compile
[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("'%s' type(%d): %s, strength: %d\n", net->essid,
74                                 net->type,
75                                 net_type_name(net->type),
76                                 net->strength);
77         }
78 }
79
80 void print_configured_nets(void)
81 {
82         int ret;
83         int i;
84         struct configured_net_list *configured = fetch_configured_nets();
85
86         debug_printf(2, "%s()\n", __func__);
87         ret = issue_noarg_command('l');
88         if (ret) {
89                 printf("error issuing print networks command: %d\n", ret);
90                 return;
91         }
92         configured = eyefi_response();
93         if (configured->nr == 0) {
94                 printf("No wireless networks configured on card\n");
95                 return;
96         }
97         printf("configured wireless networks:\n");
98         for (i=0; i < configured->nr; i++) {
99                 struct configured_net *net = &configured->nets[i];
100                 printf("'%s'\n", net->essid);
101         }
102 }
103
104 int try_connection_to(char *essid, char *ascii_password)
105 {
106         int i;
107         int ret = -1;
108         const char *type;
109
110         eyefi_printf("trying to connect to network: '%s'\n", essid);
111         if (ascii_password)
112                 eyefi_printf(" with passphrase: '%s'\n", ascii_password);
113         fflush(NULL);
114
115         // test network
116         ret = network_action('t', essid, ascii_password);
117         if (ret)
118                 return ret;
119         u8 last_rsp = -1;
120
121         char rsp = '\0';
122         ret = -1;
123         for (i=0; i < 200; i++) {
124                 struct byte_response *r;
125                 issue_noarg_command('s');
126                 r = eyefi_response();
127                 rsp = r->response;
128                 char *state = net_test_state_name(rsp);
129                 if (rsp == last_rsp) {
130                         eyefi_printf(".");
131                         fflush(NULL);;
132                 } else {
133                         if (rsp)
134                                 eyefi_printf("\nTesting connecion to '%s' (%d): %s", essid, rsp, state);
135                         last_rsp = rsp;
136                 }
137                 
138                 if (!strcmp("success", state)) {
139                         ret = 0;
140                         break;
141                 }
142                 if (!strcmp("not scanning", state))
143                         break;
144                 if (!strcmp("unknown", state))
145                         break;
146         }
147         eyefi_printf("\n");
148         if (!ret) {
149                 eyefi_printf("Succeeded connecting to: '%s'\n", essid);
150         } else {
151                 eyefi_printf("Unable to connect to: '%s' (final state: %d/'%s')\n", essid,
152                                 rsp, net_test_state_name(rsp));
153         }
154         return ret;
155 }
156
157 int print_log(void)
158 {
159         int i;
160         u8 *resbuf = malloc(EYEFI_BUF_SIZE*4);
161         int total_bytes;
162
163         total_bytes = get_log_into(resbuf);
164         if (total_bytes < 0) {
165                 debug_printf(1, "%s() error: %d\n", __func__, total_bytes);
166                 return total_bytes;
167         }
168         // The last byte *should* be a null, and the 
169         // official software does not print it.
170         for (i = 0; i < total_bytes-1; i++) {
171                 char c = resbuf[i];
172                 // the official software converts UNIX to DOS-style
173                 // line breaks, so we'll do the same
174                 if (c == '\n')
175                         printf("%c", '\r');
176                 printf("%c", c);
177         }
178         printf("\n");
179         // just some simple sanity checking to make sure what
180         // we are fetching looks valid
181         /* needs to be rethought for the new aligned logs
182         int null_bytes_left = 20;
183         if (resbuf[log_end] != 0) {
184                 debug_printf(2, "error: unexpected last byte (%ld/0x%lx) of log: %02x\n",
185                                 log_end, log_end, resbuf[log_end]);
186                 for (i=0; i<log_size; i++) {
187                         if (resbuf[i])
188                                 continue;
189                         if (null_bytes_left <= 0)
190                                 continue;
191                         null_bytes_left--;
192                         debug_printf(2, "null byte %d\n", i);
193                 }
194         }
195         */
196         return 0;
197 }
198
199 void open_error(char *file, int ret)
200 {
201         fprintf(stderr, "unable to open '%s' (%d)\n", file, ret);
202         fprintf(stderr, "Is the Eye-Fi card inserted and mounted at: %s ?\n", locate_eyefi_mount());
203         fprintf(stderr, "Do you have write permissions to it?\n");
204         fprintf(stderr, "debug information:\n");
205         if (eyefi_debug_level > 0)
206                 system("cat /proc/mounts >&2");
207         if (eyefi_debug_level > 1)
208                 perror("bad open");
209         exit(1);
210 }
211
212 void usage(void)
213 {
214         printf("Usage:\n");
215         printf("  eyefitest [OPTIONS]\n");
216         printf("  -a ESSID      add network (implies test unless --force)\n");
217         printf("  -t ESSID      test network\n");
218         printf("  -p KEY        set WPA key for add/test\n");
219         printf("  -r ESSID      remove network\n");
220         printf("  -s            scan for networks\n");
221         printf("  -c            list configured networks\n");
222         printf("  -b            reboot card\n");
223         printf("  -f            print information about card firmware\n");
224         printf("  -d level      set debugging level (default: 1)\n");
225         printf("  -k            print card unique key\n");
226         printf("  -l            dump card log\n");
227         printf("  -m            print card mac\n");
228         exit(4);
229 }
230
231 int main(int argc, char *argv[])
232 {
233         int option_index;
234         char c;
235         int cint;
236         char *essid = NULL;
237         char *passwd = NULL;
238         char network_action = 0;
239         static int force = 0;
240         static struct option long_options[] = {
241                 //{"wep", 'x', &passed_wep, 1},
242                 //{"wpa", 'y', &passed_wpa, 1},
243                 {"force", 0, &force, 1},
244                 {"help", 'h', NULL, 1},
245                 {0, 0, 0, 0}
246         };
247
248         if (argc == 1)
249                 usage();
250
251         debug_printf(3, "%s starting...\n", argv[0]);
252
253         debug_printf(3, "about to parse arguments\n");
254         debug_printf(4, "argc: %d\n", argc);
255         debug_printf(4, "argv: %p\n", argv);
256         while ((cint = getopt_long_only(argc, argv, "a:bcd:kflmp:r:st:z",
257                         &long_options[0], &option_index)) != -1) {
258                 c = cint;
259                 debug_printf(3, "argument: '%c' %d optarg: '%s'\n", c, c, optarg);
260                 switch (c) {
261                 case 0:
262                         // was a long argument
263                         break;
264                 case 'a':
265                 case 't':
266                 case 'r':
267                         essid = strdup(optarg);
268                         network_action = c;
269                         break;
270                 case 'b':
271                         reboot_card();
272                         break;
273                 case 'c':
274                         print_configured_nets();
275                         break;
276                 case 'd':
277                         eyefi_debug_level = atoi(optarg);
278                         fprintf(stderr, "set debug level to: %d\n", eyefi_debug_level);
279                         break;
280                 case 'f':
281                         print_card_firmware_info();
282                         break;
283                 case 'k':
284                         print_card_key();
285                         break;
286                 case 'l':
287                         print_log();
288                         break;
289                 case 'm':
290                         print_card_mac();
291                         break;
292                 case 'p':
293                         passwd = strdup(optarg);
294                         break;
295                 case 's':
296                         scan_print_nets();
297                         break;
298                 case 'z': {
299                         extern void testit0(void);
300                         testit0();
301                         break;
302                 }
303                 case 'h':
304                 default:
305                         usage();
306                         break;
307                 }
308         }
309
310         debug_printf(3, "after arguments1 essid: '%s' passwd: '%s'\n", essid, passwd);
311         if (network_action && essid) {
312                 int ret = 0;
313                 init_card();
314                 switch (network_action) {
315                 case 't':
316                         ret = try_connection_to(essid, passwd);
317                         break;
318                 case 'a':
319                         if (!force) {
320                                 ret = try_connection_to(essid, passwd);
321                         } else {
322                                 debug_printf(1, "forced: skipping network test\n");
323                         }
324                         if (ret) {
325                                 printf("Error connecting to network '%s', not adding.\n", essid);
326                                 printf("use --force to override\n");
327                                 break;
328                         }
329                         add_network(essid, passwd);
330                         break;
331                 case 'r':
332                         remove_network(essid);
333                         break;
334                 }
335         }
336
337         free(essid);
338         free(passwd);
339         return 0;
340 }
341
342