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