dc746a64fb4aa7de038872bbf18f5fde13d7d523
[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 SWIGTYPE_p_altos_file open() {
43                 return libaltos.altos_open(this);
44         }
45
46         private boolean isAltusMetrum() {
47                 if (getVendor() != Altos.vendor_altusmetrum)
48                         return false;
49                 if (getProduct() < Altos.product_altusmetrum_min)
50                         return false;
51                 if (getProduct() > Altos.product_altusmetrum_max)
52                         return false;
53                 return true;
54         }
55
56         public boolean matchProduct(int want_product) {
57
58                 if (!isAltusMetrum())
59                         return false;
60
61                 if (want_product == Altos.product_any)
62                         return true;
63
64                 if (want_product == Altos.product_basestation)
65                         return matchProduct(Altos.product_teledongle) ||
66                                 matchProduct(Altos.product_teleterra) ||
67                                 matchProduct(Altos.product_telebt);
68
69                 int have_product = getProduct();
70
71                 if (have_product == Altos.product_altusmetrum)  /* old devices match any request */
72                         return true;
73
74                 if (want_product == have_product)
75                         return true;
76
77                 return false;
78         }
79
80         static java.util.List<AltosDevice> list(int product) {
81                 if (!Altos.load_library())
82                         return null;
83
84                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
85
86                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
87                 if (list != null) {
88                         for (;;) {
89                                 AltosUSBDevice device = new AltosUSBDevice();
90                                 if (libaltos.altos_list_next(list, device) == 0)
91                                         break;
92                                 if (device.matchProduct(product))
93                                         device_list.add(device);
94                         }
95                         libaltos.altos_list_finish(list);
96                 }
97
98                 return device_list;
99         }
100 }