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