Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / ao-tools / altosui / AltosDevice.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 AltosDevice extends altos_device {
24
25         static public boolean initialized = false;
26         static public boolean loaded_library = false;
27
28         public static boolean load_library() {
29                 if (!initialized) {
30                         try {
31                                 System.loadLibrary("altos");
32                                 libaltos.altos_init();
33                                 loaded_library = true;
34                         } catch (UnsatisfiedLinkError e) {
35                                 loaded_library = false;
36                         }
37                         initialized = true;
38                 }
39                 return loaded_library;
40         }
41
42         static int usb_product_altusmetrum() {
43                 if (load_library())
44                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM;
45                 return 0x000a;
46         }
47
48         static int usb_product_altusmetrum_min() {
49                 if (load_library())
50                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MIN;
51                 return 0x000a;
52         }
53
54         static int usb_product_altusmetrum_max() {
55                 if (load_library())
56                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MAX;
57                 return 0x000d;
58         }
59
60         static int usb_product_telemetrum() {
61                 if (load_library())
62                         return libaltosConstants.USB_PRODUCT_TELEMETRUM;
63                 return 0x000b;
64         }
65
66         static int usb_product_teledongle() {
67                 if (load_library())
68                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM;
69                 return 0x000c;
70         }
71
72         static int usb_product_teleterra() {
73                 if (load_library())
74                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM;
75                 return 0x000d;
76         }
77
78         public final static int AltusMetrum = usb_product_altusmetrum();
79         public final static int TeleMetrum = usb_product_telemetrum();
80         public final static int TeleDongle = usb_product_teledongle();
81         public final static int TeleTerra = usb_product_teleterra();
82         public final static int AltusMetrumMin = usb_product_altusmetrum_min();
83         public final static int AltusMetrumMax = usb_product_altusmetrum_max();
84
85
86         public final static int Any = 0x10000;
87         public final static int BaseStation = 0x10000 + 1;
88
89         public String toString() {
90                 String  name = getName();
91                 if (name == null)
92                         name = "Altus Metrum";
93                 return String.format("%-20.20s %4d %s",
94                                      getName(), getSerial(), getPath());
95         }
96
97         public boolean isAltusMetrum() {
98                 if (getVendor() != AltusMetrum)
99                         return false;
100                 if (getProduct() < AltusMetrumMin)
101                         return false;
102                 if (getProduct() > AltusMetrumMax)
103                         return false;
104                 return true;
105         }
106
107         public boolean matchProduct(int want_product) {
108
109                 if (!isAltusMetrum())
110                         return false;
111
112                 if (want_product == Any)
113                         return true;
114
115                 if (want_product == BaseStation)
116                         return matchProduct(TeleDongle) || matchProduct(TeleTerra);
117
118                 int have_product = getProduct();
119
120                 if (have_product == AltusMetrum)        /* old devices match any request */
121                         return true;
122
123                 if (want_product == have_product)
124                         return true;
125
126                 return false;
127         }
128
129         static AltosDevice[] list(int product) {
130                 if (!load_library())
131                         return null;
132
133                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
134
135                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
136                 if (list != null) {
137                         SWIGTYPE_p_altos_file file;
138
139                         for (;;) {
140                                 AltosDevice device = new AltosDevice();
141                                 if (libaltos.altos_list_next(list, device) == 0)
142                                         break;
143                                 if (device.matchProduct(product))
144                                         device_list.add(device);
145                         }
146                         libaltos.altos_list_finish(list);
147                 }
148
149                 AltosDevice[] devices = new AltosDevice[device_list.size()];
150                 for (int i = 0; i < device_list.size(); i++)
151                         devices[i] = device_list.get(i);
152                 return devices;
153         }
154 }