]> CyberLeo.Net >> Repos - SourceForge/eyefi-config.git/blob - eyefi-config.h
Change filesystem detection to fstypename comparison instead of naked integer
[SourceForge/eyefi-config.git] / eyefi-config.h
1 #ifndef _EYEFI_CONFIG_H
2 #define _EYEFI_CONFIG_H
3
4 #include <sys/types.h>
5
6 #if defined(LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN)
7 #define __LITTLE_ENDIAN LITTLE_ENDIAN
8 #endif
9
10 #if defined(BIG_ENDIAN) && !defined(__BIG_ENDIAN)
11 #define __BIG_ENDIAN BIG_ENDIAN
12 #endif
13
14 #if !defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
15 #include <endian.h>
16 #include <byteswap.h>
17 #endif
18
19 extern int eyefi_debug_level;
20
21 #ifdef __CHDK__
22
23 #define CONFIG_EYEFI_STANDALONE 1
24 #define printf(...) do{}while(0)
25 #define putchar(...) do{}while(0)
26 #define puts(...) do{}while(0)
27 #define exit(i)                  return
28 #define perror(i)        do{}while(0)
29 #define system(i)        do{}while(0)
30 #define fd_flush(fd) (0)
31 #define assert(x)        do{}while(0)
32 #define output_flush()   do{}while(0)
33
34 #else
35 #define CONFIG_EYEFI_WITH_OS 1
36 #include <assert.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <getopt.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <stdarg.h>
47
48 #define PATHNAME_MAX 4096
49
50 #define output_flush()  fflush(NULL)
51
52 #define debug_printf(level, args...) do {      \
53         if ((level) <= eyefi_debug_level)      \
54                 fprintf(stderr, ## args);      \
55         } while(0)
56
57 #endif
58
59 /*
60  * These are defined in both eyefi-unix.c and eyefi-chdk.c
61  */
62 extern void open_error(char *file, int err);
63 extern int eyefi_printf(const char *fmt, ...);
64
65 /*
66  * These have to be created by the unix variants
67  */
68 extern int fd_flush(int);
69
70 /*
71  * Do some kernel-style types to make
72  * definitions shorter.
73  */
74 typedef unsigned int u32;
75 typedef unsigned char u8;
76
77 #define os_memset memset
78 #define os_memcpy memcpy
79 #define os_strlen strlen
80 #define os_strcpy strcpy
81
82 #define SHA1_MAC_LEN 20
83 #define MD5_MAC_LEN 16
84 void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
85 void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac);
86 void hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
87                      const u8 *addr[], const size_t *len, u8 *mac);
88 void hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
89               u8 *mac);
90 void pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
91                  int iterations, u8 *buf, size_t buflen);
92
93 static inline u32 swap_bytes(u32 src)
94 {
95         u32 dest = 0;
96         dest |= (src & 0xff000000) >> 24;
97         dest |= (src & 0x00ff0000) >>  8;
98         dest |= (src & 0x0000ff00) <<  8;
99         dest |= (src & 0x000000ff) << 24;
100         return dest;
101 }
102
103 #ifdef __LITTLE_ENDIAN
104 #define le_to_host32(n) (n)
105 #define be_to_host32(n) swap_bytes(n)
106 #define host_to_be32(n) swap_bytes(n)
107 #else  // __BIG_ENDIAN
108 #define le_to_host32(n) swap_bytes(n)
109 #define be_to_host32(n) (n)
110 #define host_to_be32(n) (n)
111 #endif
112
113 /*
114  * Just a few functions so that I can't easily forget about
115  * endinness.
116  */
117 struct __be32 {
118         u32 val;
119 } __attribute__((packed));
120 typedef struct __be32 be32;
121
122 /*
123  * These two obviously need to get fixed for
124  * big endian machines.
125  */
126 static inline u32 be32_to_u32(be32 src)
127 {
128         return swap_bytes(src.val);
129 }
130 static inline be32 u32_to_be32(u32 src)
131 {
132         be32 ret;
133         ret.val = swap_bytes(src);
134         return ret;
135 }
136
137 /*
138  * Eye-Fi Card data structures
139  */
140
141 struct card_seq_num {
142         u32 seq;
143 } __attribute__((packed));
144
145 #define EYEFI_BUF_SIZE 16384
146
147 /*
148  * Most of the eyefi strings are pascal-style with
149  * a length byte preceeding content.  (Did pascal
150  * have just a byte for length or more??)
151  */
152 struct pascal_string {
153         u8 length;
154         u8 value[32];
155 } __attribute__((packed));
156
157 /*
158  * The 'o' command has several sub-commands:
159  */
160 enum card_info_subcommand {
161         MAC_ADDRESS   = 1,
162         FIRMWARE_INFO = 2,
163         CARD_KEY      = 3,
164         API_URL       = 4,
165         UNKNOWN_5     = 5, // Chris says these are 
166         UNKNOWN_6     = 6, // checksums
167         LOG_LEN       = 7,
168         WLAN_ENABLED  = 10,
169         UNKNOWN_13    = 13, // Returns an ASCII SSID.  Last connected or
170                             // current WiFi network, maybe?
171                             //
172         
173         UNKNOWN_ff    = 0xff, // The D90 does this, and it looks to
174                               // return a 1-byte response length
175                               // followed by a number of 8-byte responses
176                               // But I've only ever seen a single response
177                               // [000]: 01 04 1d 00 18 56 aa d5 42 00 00 00 00 00 00 00
178 };
179
180 // new code!!: 
181 ///media/NIKON D90/EYEFI/REQM
182 //00000000  4f 0a 01 00 00 00 00 00  00 00 00 00 00 00 00 00  |O...............|
183 //00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
184 // that happens 3 seconds after the card goes into the D90
185
186 struct card_info_req {
187         u8 o;
188         u8 subcommand;
189 } __attribute__((packed));
190
191 struct card_info_rsp_key {
192         struct pascal_string key;
193 };
194
195 struct card_firmware_info {
196         struct pascal_string info;
197 };
198
199 #define MAC_BYTES 6
200 struct mac_address {
201         u8 length;
202         u8 mac[MAC_BYTES];
203 } __attribute__((packed));
204
205 struct card_info_api_url {
206         struct pascal_string key;
207 };
208
209 struct card_info_log_len {
210         u8 len;
211         be32 val;
212 } __attribute__((packed));
213
214 struct byte_response {
215         u8 response;
216 };
217
218 struct var_byte_response {
219         u8 len;
220         struct byte_response responses[0];
221 };
222
223 enum net_type {
224         NET_UNSECURED,
225         NET_WEP,
226         NET_WPA,
227         NET_WPA2
228 };
229
230 enum net_password_type {
231         NET_PASSWORD_ASCII,
232         NET_PASSWORD_RAW, /* raw hex bytes */
233 };
234
235 #define ESSID_LEN 32
236 struct scanned_net {
237         char essid[ESSID_LEN];
238         signed char strength;
239         u8 type;
240 } __attribute__((packed));
241
242 struct scanned_net_list {
243         u8 nr;
244         struct scanned_net nets[100];
245 } __attribute__((packed));
246
247 struct configured_net {
248         char essid[ESSID_LEN];
249 } __attribute__((packed));
250
251 struct configured_net_list {
252         u8 nr;
253         struct configured_net nets[100];
254 } __attribute__((packed));
255
256 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
257
258 #define WPA_KEY_BYTES 32
259 struct wpa_key {
260         u8 key[WPA_KEY_BYTES];
261 } __attribute((packed));
262
263 #define WEP_40_KEY_BYTES 5
264 #define WEP_KEY_BYTES 13
265 struct wep_key {
266         u8 key[WEP_KEY_BYTES];
267 } __attribute((packed));
268
269
270 struct network_key {
271         u8 len;
272         union {
273                 struct wpa_key wpa;
274                 struct wep_key wep;
275         };
276 } __attribute((packed));
277
278 #define KEY_LEN 32
279 struct net_request {
280         char req;
281         u8 essid_len;
282         char essid[ESSID_LEN];
283         struct network_key key;
284 } __attribute((packed));
285
286 struct noarg_request {
287         u8 req;
288 };
289
290 /*
291  * Log structures
292  */
293 struct fetch_log_cmd {
294         char m;
295         be32 offset;
296 } __attribute__((packed));
297
298 /*
299  * When you ask for the log at offset 0x0, you
300  * get back 8 bytes of offsets into the rest of
301  * the data
302  */
303 struct first_log_response {
304         be32 log_end;
305         be32 log_start;
306         u8 data[EYEFI_BUF_SIZE-8];
307 } __attribute__((packed));
308
309 struct rest_log_response {
310         u8 data[EYEFI_BUF_SIZE];
311 } __attribute__((packed));
312
313 /*
314  * Functions that are exported from eyefi-config.c
315  */
316 u32 fetch_log_length(void);
317 int card_info_cmd(enum card_info_subcommand cmd);
318 void *eyefi_response(void);
319 struct card_info_rsp_key *fetch_card_key(void);
320 struct scanned_net_list *scan_nets(void);
321 const char *net_type_name(u8 type);
322 struct configured_net_list *fetch_configured_nets(void);
323 int issue_noarg_command(u8 cmd);
324 char *net_test_state_name(u8 state);
325 int network_action(char cmd, char *essid, char *wpa_ascii);
326 char *locate_eyefi_mount(void);
327 int get_log_into(u8 *resbuf);
328 void reboot_card(void);
329 void init_card(void);
330 void add_network(char *essid, char *ascii_password);
331 void remove_network(char *essid);
332 struct card_firmware_info *fetch_card_firmware_info(void);
333 /*
334  * Only used by the unix variants
335  */
336 enum eyefi_file {
337         REQC,
338         REQM,
339         RSPC,
340         RSPM
341 };
342 char *eyefi_file_on(enum eyefi_file file, char *mnt);
343 int atoh(char c);
344 #endif // _EYEFI_CONFIG_H