Switch from GPLv2 to GPLv2+
[fw/altos] / micropeak / MicroUSB.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.micropeak;
20
21 import java.util.*;
22 import libaltosJNI.*;
23 import org.altusmetrum.altoslib_11.*;
24 import org.altusmetrum.altosuilib_11.*;
25
26 public class MicroUSB extends altos_device implements AltosDevice {
27
28         static boolean  initialized = false;
29         static boolean  loaded_library = false;
30
31         public static boolean load_library() {
32                 if (!initialized) {
33                         try {
34                                 System.loadLibrary("altos");
35                                 libaltos.altos_init();
36                                 loaded_library = true;
37                         } catch (UnsatisfiedLinkError e) {
38                                 try {
39                                         System.loadLibrary("altos64");
40                                         libaltos.altos_init();
41                                         loaded_library = true;
42                                 } catch (UnsatisfiedLinkError e2) {
43                                         loaded_library = false;
44                                 }
45                         }
46                         initialized = true;
47                 }
48                 return loaded_library;
49         }
50
51         public String toString() {
52                 String  name = getName();
53                 if (name == null)
54                         name = "Altus Metrum";
55                 return String.format("%-24.24s %s",
56                                      name, getPath());
57         }
58
59         public String toShortString() {
60                 String  name = getName();
61                 if (name == null)
62                         name = "Altus Metrum";
63                 return String.format("%s %s",
64                                      name, getPath());
65
66         }
67
68         public String getErrorString() {
69                 altos_error     error = new altos_error();
70
71                 libaltos.altos_get_last_error(error);
72                 return String.format("%s (%d)", error.getString(), error.getCode());
73         }
74
75         public SWIGTYPE_p_altos_file open() {
76                 return libaltos.altos_open(this);
77         }
78
79         private boolean isFTDI() {
80                 int vid = getVendor();
81                 int pid = getProduct();
82                 if (vid == 0x0403 && pid == 0x6015)
83                         return true;
84                 return false;
85         }
86
87         private boolean isMicro() {
88                 int vid = getVendor();
89                 int pid = getProduct();
90                 if (vid == AltosLib.vendor_altusmetrum &&
91                     pid == AltosLib.product_mpusb)
92                         return true;
93                 return false;
94         }
95
96         public boolean matchProduct(int product) {
97                 return isFTDI() || isMicro();
98         }
99
100         static java.util.List<MicroUSB> list() {
101                 if (!load_library())
102                         return null;
103
104                 ArrayList<MicroUSB> device_list = new ArrayList<MicroUSB>();
105
106                 SWIGTYPE_p_altos_list list;
107
108                 list = libaltos.altos_ftdi_list_start();
109
110                 if (list != null) {
111                         for (;;) {
112                                 MicroUSB device = new MicroUSB();
113                                 if (libaltos.altos_list_next(list, device) == 0)
114                                         break;
115                                 if (device.isFTDI())
116                                         device_list.add(device);
117                         }
118                         libaltos.altos_list_finish(list);
119                 }
120
121                 list = libaltos.altos_list_start();
122
123                 if (list != null) {
124                         for (;;) {
125                                 MicroUSB device = new MicroUSB();
126                                 if (libaltos.altos_list_next(list, device) == 0)
127                                         break;
128                                 if (device.isMicro())
129                                         device_list.add(device);
130                         }
131                         libaltos.altos_list_finish(list);
132                 }
133
134                 return device_list;
135         }
136 }