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