ao-tools: build ao-usbload by default
[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.altosuilib_1.*;
23
24 public class MicroUSB extends altos_device implements AltosDevice {
25
26         static boolean  initialized = false;
27         static boolean  loaded_library = false;
28
29         public static boolean load_library() {
30                 if (!initialized) {
31                         try {
32                                 System.loadLibrary("altos");
33                                 libaltos.altos_init();
34                                 loaded_library = true;
35                         } catch (UnsatisfiedLinkError e) {
36                                 try {
37                                         System.loadLibrary("altos64");
38                                         libaltos.altos_init();
39                                         loaded_library = true;
40                                 } catch (UnsatisfiedLinkError e2) {
41                                         loaded_library = false;
42                                 }
43                         }
44                         initialized = true;
45                 }
46                 return loaded_library;
47         }
48
49         public String toString() {
50                 String  name = getName();
51                 if (name == null)
52                         name = "Altus Metrum";
53                 return String.format("%-24.24s %s",
54                                      name, getPath());
55         }
56
57         public String toShortString() {
58                 String  name = getName();
59                 if (name == null)
60                         name = "Altus Metrum";
61                 return String.format("%s %s",
62                                      name, getPath());
63
64         }
65
66         public String getErrorString() {
67                 altos_error     error = new altos_error();
68
69                 libaltos.altos_get_last_error(error);
70                 return String.format("%s (%d)", error.getString(), error.getCode());
71         }
72
73         public SWIGTYPE_p_altos_file open() {
74                 return libaltos.altos_open(this);
75         }
76
77         private boolean isMicro() {
78                 if (getVendor() != 0x0403)
79                         return false;
80                 if (getProduct() != 0x6015)
81                         return false;
82                 return true;
83         }
84
85         public boolean matchProduct(int product) {
86                 return isMicro();
87         }
88
89         static java.util.List<MicroUSB> list() {
90                 if (!load_library())
91                         return null;
92
93                 SWIGTYPE_p_altos_list list = libaltos.altos_ftdi_list_start();
94
95                 ArrayList<MicroUSB> device_list = new ArrayList<MicroUSB>();
96                 if (list != null) {
97                         for (;;) {
98                                 MicroUSB device = new MicroUSB();
99                                 if (libaltos.altos_list_next(list, device) == 0)
100                                         break;
101                                 if (device.isMicro())
102                                         device_list.add(device);
103                         }
104                         libaltos.altos_list_finish(list);
105                 }
106
107                 return device_list;
108         }
109 }