Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosUSBDevice.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; 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 altosui;
19 import java.util.*;
20 import libaltosJNI.*;
21
22 public class AltosUSBDevice  extends altos_device implements AltosDevice {
23
24         public String toString() {
25                 String  name = getName();
26                 if (name == null)
27                         name = "Altus Metrum";
28                 return String.format("%-20.20s %4d %s",
29                                      name, getSerial(), getPath());
30         }
31
32         public String toShortString() {
33                 String  name = getName();
34                 if (name == null)
35                         name = "Altus Metrum";
36                 return String.format("%s %d %s",
37                                      name, getSerial(), getPath());
38
39         }
40
41         public String getErrorString() {
42                 altos_error     error = new altos_error();
43
44                 libaltos.altos_get_last_error(error);
45                 return String.format("%s (%d)", error.getString(), error.getCode());
46         }
47
48         public SWIGTYPE_p_altos_file open() {
49                 return libaltos.altos_open(this);
50         }
51
52         private boolean isAltusMetrum() {
53                 if (getVendor() != Altos.vendor_altusmetrum)
54                         return false;
55                 if (getProduct() < Altos.product_altusmetrum_min)
56                         return false;
57                 if (getProduct() > Altos.product_altusmetrum_max)
58                         return false;
59                 return true;
60         }
61
62         public boolean matchProduct(int want_product) {
63
64                 if (!isAltusMetrum())
65                         return false;
66
67                 if (want_product == Altos.product_any)
68                         return true;
69
70                 if (want_product == Altos.product_basestation)
71                         return matchProduct(Altos.product_teledongle) ||
72                                 matchProduct(Altos.product_teleterra) ||
73                                 matchProduct(Altos.product_telebt) ||
74                                 matchProduct(Altos.product_megadongle);
75
76                 if (want_product == Altos.product_altimeter)
77                         return matchProduct(Altos.product_telemetrum) ||
78                                 matchProduct(Altos.product_megametrum);
79
80                 int have_product = getProduct();
81
82                 if (have_product == Altos.product_altusmetrum)  /* old devices match any request */
83                         return true;
84
85                 if (want_product == have_product)
86                         return true;
87
88                 return false;
89         }
90
91         static java.util.List<AltosDevice> list(int product) {
92                 if (!Altos.load_library())
93                         return null;
94
95                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
96
97                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
98                 if (list != null) {
99                         for (;;) {
100                                 AltosUSBDevice device = new AltosUSBDevice();
101                                 if (libaltos.altos_list_next(list, device) == 0)
102                                         break;
103                                 if (device.matchProduct(product))
104                                         device_list.add(device);
105                         }
106                         libaltos.altos_list_finish(list);
107                 }
108
109                 return device_list;
110         }
111 }