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