6ffdb6281ea82e0e9a14a68a588675c9101e7667
[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_5.*;
23 import org.altusmetrum.altosuilib_3.*;
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 isMicro() {
79                 int vid = getVendor();
80                 int pid = getProduct();
81                 if (vid == 0x0403 && pid == 0x6015)
82                         return true;
83                 if (vid == AltosLib.vendor_altusmetrum &&
84                     pid == AltosLib.product_mpusb)
85                         return true;
86                 return false;
87         }
88
89         public boolean matchProduct(int product) {
90                 return isMicro();
91         }
92
93         static java.util.List<MicroUSB> list() {
94                 if (!load_library())
95                         return null;
96
97                 SWIGTYPE_p_altos_list list = libaltos.altos_ftdi_list_start();
98
99                 ArrayList<MicroUSB> device_list = new ArrayList<MicroUSB>();
100                 if (list != null) {
101                         for (;;) {
102                                 MicroUSB device = new MicroUSB();
103                                 if (libaltos.altos_list_next(list, device) == 0)
104                                         break;
105                                 if (device.isMicro())
106                                         device_list.add(device);
107                         }
108                         libaltos.altos_list_finish(list);
109                 }
110
111                 return device_list;
112         }
113 }