telegps: Add scan UI
[fw/altos] / altosuilib / 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 org.altusmetrum.altosuilib_2;
19
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         public boolean isAltusMetrum() {
54                 if (getVendor() != AltosUILib.vendor_altusmetrum)
55                         return false;
56                 if (getProduct() < AltosUILib.product_altusmetrum_min)
57                         return false;
58                 if (getProduct() > AltosUILib.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 == AltosUILib.product_any)
69                         return true;
70
71                 int have_product = getProduct();
72
73                 if (want_product == AltosUILib.product_basestation)
74                         return have_product == AltosUILib.product_teledongle ||
75                                 have_product == AltosUILib.product_teleterra ||
76                                 have_product == AltosUILib.product_telebt ||
77                                 have_product == AltosUILib.product_megadongle;
78
79                 if (want_product == AltosUILib.product_altimeter)
80                         return have_product == AltosUILib.product_telemetrum ||
81                                 have_product == AltosUILib.product_telemega ||
82                                 have_product == AltosUILib.product_telegps ||
83                                 have_product == AltosUILib.product_easymini ||
84                                 have_product == AltosUILib.product_telemini;
85
86                 if (have_product == AltosUILib.product_altusmetrum)     /* old devices match any request */
87                         return true;
88
89                 if (want_product == have_product)
90                         return true;
91
92                 return false;
93         }
94
95         static public java.util.List<AltosDevice> list(int product) {
96                 if (!AltosUILib.load_library())
97                         return null;
98
99                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
100
101                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
102                 if (list != null) {
103                         for (;;) {
104                                 AltosUSBDevice device = new AltosUSBDevice();
105                                 if (libaltos.altos_list_next(list, device) == 0)
106                                         break;
107                                 if (device.matchProduct(product))
108                                         device_list.add(device);
109                         }
110                         libaltos.altos_list_finish(list);
111                 }
112
113                 return device_list;
114         }
115 }