4 * Copyright (C) 2008 Dave Hansen <dave@sr71.net>
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.
11 #include "eyefi-config.h"
13 void print_pascal_string(struct pascal_string *str)
16 for (i = 0; i < str->length; i++)
17 printf("%c", str->value[i]);
20 void print_mac(struct mac_address *mac)
23 for (i=0; i < MAC_BYTES-1; i++) {
24 printf("%02x:", mac->mac[i]);
26 printf("%02x\n", mac->mac[i]);
30 void print_card_mac(void)
32 debug_printf(2, "%s()\n", __func__);
33 struct mac_address *mac;
35 card_info_cmd(MAC_ADDRESS);
36 mac = eyefi_response();
37 assert(mac->length == MAC_BYTES);
38 printf("card mac address: ");
42 void print_card_firmware_info(void)
44 struct card_firmware_info *info = fetch_card_firmware_info();
45 printf("card firmware (len: %d): '", info->info.length);
46 print_pascal_string(&info->info);
50 void print_card_key(void)
52 debug_printf(2, "%s()\n", __func__);
53 struct card_info_rsp_key *foo = fetch_card_key();
54 printf("card key (len: %d): '", foo->key.length);
55 print_pascal_string(&foo->key);
59 void scan_print_nets(void)
63 debug_printf(2, "%s()\n", __func__);
64 struct scanned_net_list *scanned = scan_nets();
65 if (scanned->nr == 0) {
66 printf("unable to detect any wireless networks\n");
69 printf("Scanned wireless networks:\n");
70 for (i=0; i < scanned->nr; i++) {
71 struct scanned_net *net = &scanned->nets[i];
72 printf("'%s' type(%d): %s, strength: %d\n", net->essid,
74 net_type_name(net->type),
79 void print_configured_nets(void)
83 struct configured_net_list *configured = fetch_configured_nets();
85 debug_printf(2, "%s()\n", __func__);
86 ret = issue_noarg_command('l');
88 printf("error issuing print networks command: %d\n", ret);
91 configured = eyefi_response();
92 if (configured->nr == 0) {
93 printf("No wireless networks configured on card\n");
96 printf("configured wireless networks:\n");
97 for (i=0; i < configured->nr; i++) {
98 struct configured_net *net = &configured->nets[i];
99 printf("'%s'\n", net->essid);
103 int try_connection_to(char *essid, char *wpa_ascii)
109 type = net_type_name(NET_WPA);
111 type = net_type_name(NET_UNSECURED);
113 eyefi_printf("trying to connect to network: '%s'", essid);
114 eyefi_printf("of type: '%s'\n", type);
116 eyefi_printf(" with passphrase: '%s'", wpa_ascii);
120 network_action('t', essid, wpa_ascii);
124 for (i=0; i < 200; i++) {
125 struct byte_response *r;
126 issue_noarg_command('s');
127 r = eyefi_response();
129 char *state = net_test_state_name(rsp);
130 if (rsp == last_rsp) {
135 eyefi_printf("\nTesting connecion to '%s' (%d): %s", essid, rsp, state);
139 if (!strcmp("success", state)) {
143 if (!strcmp("not scanning", state))
145 if (!strcmp("unknown", state))
150 eyefi_printf("Succeeded connecting to: '%s'\n", essid);
152 eyefi_printf("Unable to connect to: '%s' (final state: %d/'%s')\n", essid,
153 rsp, net_test_state_name(rsp));
161 u8 *resbuf = malloc(EYEFI_BUF_SIZE*4);
164 total_bytes = get_log_into(resbuf);
165 if (total_bytes < 0) {
166 debug_printf(1, "%s() error: %d\n", __func__, total_bytes);
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++) {
173 // the official software converts UNIX to DOS-style
174 // line breaks, so we'll do the same
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++) {
190 if (null_bytes_left <= 0)
193 debug_printf(2, "null byte %d\n", i);
200 void open_error(char *file, int ret)
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)
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");
232 int main(int argc, char **argv)
236 int magic0 = 19790111;
239 int magic1 = 1111979;
240 char network_action = 0;
241 static int force = 0;
242 static struct option long_options[] = {
243 //{"wep", 'x', &passed_wep, 1},
244 //{"wpa", 'y', &passed_wpa, 1},
245 {"force", 0, &force, 1},
246 {"help", 'h', NULL, 1},
252 debug_printf(3, "%s starting...\n", argv[0]);
254 debug_printf(3, "about to parse arguments\n");
255 while ((c = getopt_long_only(argc, argv, "a:bcd:kflmp:r:st:z",
256 &long_options[0], &option_index)) != -1) {
257 debug_printf(3, "argument: '%c' %d optarg: '%s'\n", c, c, optarg);
260 // was a long argument
265 essid = strdup(optarg);
272 print_configured_nets();
275 eyefi_debug_level = atoi(optarg);
276 fprintf(stderr, "set debug level to: %d\n", eyefi_debug_level);
279 print_card_firmware_info();
291 passwd = strdup(optarg);
297 extern void testit0(void);
308 debug_printf(3, "after arguments1 essid: '%s' passwd: '%s'\n", essid, passwd);
309 if (network_action && essid) {
312 switch (network_action) {
314 ret = try_connection_to(essid, passwd);
318 ret = try_connection_to(essid, passwd);
320 debug_printf(1, "forced: skipping network test\n");
323 printf("Error connecting to network '%s', not adding.\n", essid);
324 printf("use --force to override\n");
327 add_network(essid, passwd);
330 remove_network(essid);