f43d6befda46e57ec9ee26f0d105452308afe9b9
[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; 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.altosuilib_13;
20
21 import java.util.*;
22 import libaltosJNI.*;
23
24 public class AltosUSBDevice  extends altos_device implements AltosDevice {
25
26         public String toString() {
27                 String  name = getName();
28                 if (name == null)
29                         name = "Altus Metrum";
30                 return String.format("%-20.20s %4d %s",
31                                      name, getSerial(), getPath());
32         }
33
34         public String toShortString() {
35                 String  name = getName();
36                 if (name == null)
37                         name = "Altus Metrum";
38                 return String.format("%s %d %s",
39                                      name, getSerial(), getPath());
40
41         }
42
43         public String getErrorString() {
44                 altos_error     error = new altos_error();
45
46                 libaltos.altos_get_last_error(error);
47                 return String.format("%s (%d)", error.getString(), error.getCode());
48         }
49
50         public SWIGTYPE_p_altos_file open() {
51                 return libaltos.altos_open(this);
52         }
53
54         public boolean isAltusMetrum() {
55                 if (getVendor() != AltosUILib.vendor_altusmetrum)
56                         return false;
57                 if (getProduct() < AltosUILib.product_altusmetrum_min)
58                         return false;
59                 if (getProduct() > AltosUILib.product_altusmetrum_max)
60                         return false;
61                 return true;
62         }
63
64         public boolean matchProduct(int want_product) {
65
66                 if (!isAltusMetrum())
67                         return false;
68
69                 if (want_product == AltosUILib.product_any)
70                         return true;
71
72                 int have_product = getProduct();
73
74                 if (want_product == AltosUILib.product_basestation)
75                         return have_product == AltosUILib.product_teledongle ||
76                                 have_product == AltosUILib.product_teleterra ||
77                                 have_product == AltosUILib.product_telebt ||
78                                 have_product == AltosUILib.product_megadongle;
79
80                 if (want_product == AltosUILib.product_altimeter)
81                         return have_product == AltosUILib.product_telemetrum ||
82                                 have_product == AltosUILib.product_telemega ||
83                                 have_product == AltosUILib.product_easymega ||
84                                 have_product == AltosUILib.product_telegps ||
85                                 have_product == AltosUILib.product_easymini ||
86                                 have_product == AltosUILib.product_telemini;
87
88                 if (have_product == AltosUILib.product_altusmetrum)     /* old devices match any request */
89                         return true;
90
91                 if (want_product == have_product)
92                         return true;
93
94                 return false;
95         }
96
97         public int hashCode() {
98                 return getVendor() ^ getProduct() ^ getSerial() ^ getPath().hashCode();
99         }
100
101         public boolean equals(Object o) {
102                 if (o == null)
103                         return false;
104
105                 if (!(o instanceof AltosUSBDevice))
106                         return false;
107                 AltosUSBDevice other = (AltosUSBDevice) o;
108
109                 return getVendor() == other.getVendor() &&
110                         getProduct() == other.getProduct() &&
111                         getSerial() == other.getSerial() &&
112                         getPath().equals(other.getPath());
113         }
114
115         static public java.util.List<AltosDevice> list(int product) {
116                 if (!AltosUILib.load_library())
117                         return null;
118
119                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
120
121                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
122                 if (list != null) {
123                         for (;;) {
124                                 AltosUSBDevice device = new AltosUSBDevice();
125                                 if (libaltos.altos_list_next(list, device) == 0)
126                                         break;
127                                 if (device.matchProduct(product))
128                                         device_list.add(device);
129                         }
130                         libaltos.altos_list_finish(list);
131                 }
132
133                 return device_list;
134         }
135 }