altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosIgnite.java
1 /*
2  * Copyright © 2010 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.util.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24
25 public class AltosIgnite {
26         AltosLink       link;
27         boolean         remote;
28         boolean         close_on_exit;
29         boolean         link_started;
30         boolean         has_pyro_info = false;
31         boolean         has_standard = false;
32         int             npyro;
33         AltosConfigData config_data;
34
35         public final static String      None = null;
36         public final static String      Apogee = "drogue";
37         public final static String      Main = "main";
38
39         public final static int Unknown = 0;
40         public final static int Ready = 1;
41         public final static int Active = 2;
42         public final static int Open = 3;
43
44         private void start_link() throws InterruptedException, TimeoutException {
45                 link_started = true;
46                 if (remote)
47                         link.start_remote();
48         }
49
50         private void stop_link() throws InterruptedException {
51                 if (!link_started)
52                         return;
53                 link_started = false;
54                 if (link == null)
55                         return;
56                 if (remote)
57                         link.stop_remote();
58         }
59
60         class string_ref {
61                 String  value;
62
63                 public String get() {
64                         return value;
65                 }
66                 public void set(String i) {
67                         value = i;
68                 }
69                 public string_ref() {
70                         value = null;
71                 }
72         }
73
74         /*
75         private boolean get_string(String line, String label, string_ref s) {
76                 if (line.startsWith(label)) {
77                         String  quoted = line.substring(label.length()).trim();
78
79                         if (quoted.startsWith("\""))
80                                 quoted = quoted.substring(1);
81                         if (quoted.endsWith("\""))
82                                 quoted = quoted.substring(0,quoted.length()-1);
83                         s.set(quoted);
84                         return true;
85                 } else {
86                         return false;
87                 }
88         }
89         */
90
91         private int map_status(String status_name) {
92                 if (status_name.equals("unknown"))
93                         return Unknown;
94                 if (status_name.equals("ready"))
95                         return Ready;
96                 if (status_name.equals("active"))
97                         return Active;
98                 if (status_name.equals("open"))
99                         return Open;
100                 return Unknown;
101         }
102
103         private void get_npyro() throws InterruptedException, TimeoutException {
104                 if (config_data == null)
105                         config_data = new AltosConfigData(link);
106                 if (config_data != null && config_data.npyro != AltosLib.MISSING)
107                         npyro = config_data.npyro;
108                 else
109                         npyro = 0;
110                 if (config_data != null)
111                         has_standard = config_data.ignite_mode != AltosLib.MISSING;
112
113                 has_pyro_info = true;
114         }
115
116         public int npyro() throws InterruptedException, TimeoutException {
117                 if (!has_pyro_info) {
118                         start_link();
119                         get_npyro();
120                         stop_link();
121                 }
122                 return npyro;
123         }
124
125         public boolean has_standard() throws InterruptedException, TimeoutException {
126                 if (!has_pyro_info) {
127                         start_link();
128                         get_npyro();
129                         stop_link();
130                 }
131                 return has_standard;
132         }
133
134         public HashMap<String,Integer> status() throws InterruptedException, TimeoutException {
135                 HashMap<String,Integer> status = new HashMap<String,Integer>();
136
137                 if (link == null)
138                         return status;
139                 try {
140                         start_link();
141                         get_npyro();
142
143                         String last_igniter = Main;
144                         if (npyro > 0)
145                                 last_igniter = String.format("%d", npyro - 1);
146
147                         link.printf("t\n");
148                         for (;;) {
149                                 String line = link.get_reply(5000);
150                                 if (line == null)
151                                         throw new TimeoutException();
152                                 String[] items = line.split("\\s+");
153
154                                 if (items.length < 4)
155                                         continue;
156
157                                 if (!items[0].equals("Igniter:"))
158                                         continue;
159
160                                 if (!items[2].equals("Status:"))
161                                         continue;
162
163                                 status.put(items[1], map_status(items[3]));
164
165                                 if (items[1].equals(last_igniter))
166                                         break;
167                         }
168                 } finally {
169                         stop_link();
170                 }
171                 return status;
172         }
173
174         public static String status_string(int status) {
175                 switch (status) {
176                 case Unknown: return "Unknown";
177                 case Ready: return "Ready";
178                 case Active: return "Active";
179                 case Open: return "Open";
180                 default: return "Unknown";
181                 }
182         }
183
184         public void fire(String igniter) throws InterruptedException {
185                 if (link == null)
186                         return;
187                 try {
188                         start_link();
189                         link.printf("i DoIt %s\n", igniter);
190                         link.flush_output();
191                 } catch (TimeoutException te) {
192                 } finally {
193                         stop_link();
194                 }
195         }
196
197         public void close() throws InterruptedException {
198                 stop_link();
199                 if (close_on_exit)
200                         link.close();
201                 link = null;
202         }
203
204         public AltosIgnite(AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
205                 link = in_link;
206                 remote = in_remote;
207                 close_on_exit = in_close_on_exit;
208         }
209
210         public AltosIgnite(AltosLink in_link, boolean in_remote) {
211                 this(in_link, in_remote, true);
212         }
213 }