altosui: Move more eeprom stuff to altoslib
[fw/altos] / altosui / altoslib / src / org / altusmetrum / AltosLib / AltosEepromLog.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.util.prefs.*;
24 import java.util.concurrent.*;
25
26 /*
27  * Extract a bit of information from an eeprom-stored flight log.
28  */
29
30 public class AltosEepromLog {
31         public int              serial;
32         public boolean          has_flight;
33         public int              flight;
34         public int              start_block;
35         public int              end_block;
36
37         public int              year, month, day;
38
39         public boolean          download;
40         public boolean          delete;
41
42         public AltosEepromLog(AltosConfigData config_data,
43                               AltosLink link,
44                               int in_flight, int in_start_block,
45                               int in_end_block)
46                 throws InterruptedException, TimeoutException {
47
48                 int             block;
49                 boolean         has_date = false;
50
51                 flight = in_flight;
52                 if (flight != 0)
53                         has_flight = true;
54                 start_block = in_start_block;
55                 end_block = in_end_block;
56                 serial = config_data.serial;
57
58                 /*
59                  * By default, request that every log be downloaded but not deleted
60                  */
61                 download = true;
62                 delete = false;
63
64                 /*
65                  * Look in TeleMetrum log data for date
66                  */
67                 if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN ||
68                     config_data.log_format == AltosLib.AO_LOG_FORMAT_FULL)
69                 {
70                         /*
71                          * Only look in the first two blocks so that this
72                          * process doesn't take a long time
73                          */
74                         if (in_end_block > in_start_block + 2)
75                                 in_end_block = in_start_block + 2;
76
77                         for (block = in_start_block; block < in_end_block; block++) {
78                                 AltosEepromChunk eechunk = new AltosEepromChunk(link, block, block == in_start_block);
79
80                                 for (int i = 0; i < eechunk.chunk_size; i += AltosEepromRecord.record_length) {
81                                         try {
82                                                 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
83
84                                                 if (r.cmd == AltosLib.AO_LOG_FLIGHT) {
85                                                         flight = r.b;
86                                                         has_flight = true;
87                                                 }
88                                                 if (r.cmd == AltosLib.AO_LOG_GPS_DATE) {
89                                                         year = 2000 + (r.a & 0xff);
90                                                         month = (r.a >> 8) & 0xff;
91                                                         day = (r.b & 0xff);
92                                                         has_date = true;
93                                                 }
94                                         } catch (ParseException pe) {
95                                         }
96                                 }
97                                 if (has_date && has_flight)
98                                         break;
99                         }
100                 }
101         }
102 }