d671031dc7d90ab66c1cf7e13ddeb42d4d77f9f1
[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_vendor_altusmetrum() {
43                 if (load_library())
44                         return libaltosConstants.USB_VENDOR_ALTUSMETRUM;
45                 return 0x000a;
46         }
47
48         static int usb_product_altusmetrum() {
49                 if (load_library())
50                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM;
51                 return 0x000a;
52         }
53
54         static int usb_product_altusmetrum_min() {
55                 if (load_library())
56                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MIN;
57                 return 0x000a;
58         }
59
60         static int usb_product_altusmetrum_max() {
61                 if (load_library())
62                         return libaltosConstants.USB_PRODUCT_ALTUSMETRUM_MAX;
63                 return 0x000d;
64         }
65
66         static int usb_product_telemetrum() {
67                 if (load_library())
68                         return libaltosConstants.USB_PRODUCT_TELEMETRUM;
69                 return 0x000b;
70         }
71
72         static int usb_product_teledongle() {
73                 if (load_library())
74                         return libaltosConstants.USB_PRODUCT_TELEDONGLE;
75                 return 0x000c;
76         }
77
78         static int usb_product_teleterra() {
79                 if (load_library())
80                         return libaltosConstants.USB_PRODUCT_TELETERRA;
81                 return 0x000d;
82         }
83
84         public final static int vendor_altusmetrum = usb_vendor_altusmetrum();
85         public final static int product_altusmetrum = usb_product_altusmetrum();
86         public final static int product_telemetrum = usb_product_telemetrum();
87         public final static int product_teledongle = usb_product_teledongle();
88         public final static int product_teleterra = usb_product_teleterra();
89         public final static int product_altusmetrum_min = usb_product_altusmetrum_min();
90         public final static int product_altusmetrum_max = usb_product_altusmetrum_max();
91
92
93         public final static int product_any = 0x10000;
94         public final static int product_basestation = 0x10000 + 1;
95
96         public String toString() {
97                 String  name = getName();
98                 if (name == null)
99                         name = "Altus Metrum";
100                 return String.format("%-20.20s %4d %s",
101                                      getName(), getSerial(), getPath());
102         }
103
104         public boolean isAltusMetrum() {
105                 if (getVendor() != vendor_altusmetrum)
106                         return false;
107                 if (getProduct() < product_altusmetrum_min)
108                         return false;
109                 if (getProduct() > product_altusmetrum_max)
110                         return false;
111                 return true;
112         }
113
114         public boolean matchProduct(int want_product) {
115
116                 System.out.printf("vendor %x product %x want %x\n",
117                                   getVendor(), getProduct(), want_product);
118                 System.out.printf("vendor_altusmetrum: %d\n", vendor_altusmetrum);
119                 System.out.printf("telemetrum: %d\n", product_telemetrum);
120
121                 if (!isAltusMetrum())
122                         return false;
123
124                 if (want_product == product_any)
125                         return true;
126
127                 if (want_product == product_basestation)
128                         return matchProduct(product_teledongle) || matchProduct(product_teleterra);
129
130                 int have_product = getProduct();
131
132                 if (have_product == product_altusmetrum)        /* old devices match any request */
133                         return true;
134
135                 if (want_product == have_product)
136                         return true;
137
138                 return false;
139         }
140
141         static AltosDevice[] list(int product) {
142                 if (!load_library()) {
143                         System.out.printf("no library\n");
144                         return null;
145                 }
146
147                 SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
148
149                 ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
150                 if (list != null) {
151                         System.out.printf("got device list\n");
152                         SWIGTYPE_p_altos_file file;
153
154                         for (;;) {
155                                 AltosDevice device = new AltosDevice();
156                                 if (libaltos.altos_list_next(list, device) == 0)
157                                         break;
158                                 System.out.printf("got device\n");
159                                 if (device.matchProduct(product))
160                                         device_list.add(device);
161                         }
162                         libaltos.altos_list_finish(list);
163                 }
164
165                 AltosDevice[] devices = new AltosDevice[device_list.size()];
166                 for (int i = 0; i < device_list.size(); i++)
167                         devices[i] = device_list.get(i);
168                 return devices;
169         }
170 }