20026c6d33139a76b4c8014f15e284defd0c3b48
[fw/altos] / 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_1;
19
20 import java.text.*;
21 import java.util.concurrent.*;
22
23 /*
24  * Extract a bit of information from an eeprom-stored flight log.
25  */
26
27 public class AltosEepromLog {
28         public int              serial;
29         public boolean          has_flight;
30         public int              flight;
31         public int              start_block;
32         public int              end_block;
33
34         public int              year, month, day;
35
36         public boolean          selected;
37
38         public AltosEepromLog(AltosConfigData config_data,
39                               AltosLink link,
40                               int in_flight, int in_start_block,
41                               int in_end_block)
42                 throws InterruptedException, TimeoutException {
43
44                 int             block;
45                 boolean         has_date = false;
46
47                 flight = in_flight;
48                 if (flight != 0)
49                         has_flight = true;
50                 start_block = in_start_block;
51                 end_block = in_end_block;
52                 serial = config_data.serial;
53
54                 /*
55                  * Select all flights for download
56                  */
57                 selected = true;
58
59                 /*
60                  * Look in TeleMetrum log data for date
61                  */
62                 if (config_data.log_format == AltosLib.AO_LOG_FORMAT_UNKNOWN ||
63                     config_data.log_format == AltosLib.AO_LOG_FORMAT_FULL)
64                 {
65                         /*
66                          * Only look in the first two blocks so that this
67                          * process doesn't take a long time
68                          */
69                         if (in_end_block > in_start_block + 2)
70                                 in_end_block = in_start_block + 2;
71
72                         for (block = in_start_block; block < in_end_block; block++) {
73                                 AltosEepromChunk eechunk = new AltosEepromChunk(link, block, block == in_start_block);
74
75                                 for (int i = 0; i < AltosEepromChunk.chunk_size; i += AltosEepromRecord.record_length) {
76                                         try {
77                                                 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
78
79                                                 if (r.cmd == AltosLib.AO_LOG_FLIGHT) {
80                                                         flight = r.b;
81                                                         has_flight = true;
82                                                 }
83                                                 if (r.cmd == AltosLib.AO_LOG_GPS_DATE) {
84                                                         year = 2000 + (r.a & 0xff);
85                                                         month = (r.a >> 8) & 0xff;
86                                                         day = (r.b & 0xff);
87                                                         has_date = true;
88                                                 }
89                                         } catch (ParseException pe) {
90                                         }
91                                 }
92                                 if (has_date && has_flight)
93                                         break;
94                         }
95                 }
96         }
97 }