8eb18bb94270ca11d9b5d7499b0c7c133dd43e5e
[fw/altos] / altosui / AltosBTDevice.java
1 /*
2  * Copyright © 2011 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 altosui;
19 import java.lang.*;
20 import java.util.*;
21 import libaltosJNI.*;
22
23 public class AltosBTDevice extends altos_bt_device {
24
25         static public boolean initialized = false;
26         static public boolean loaded_library = false;
27
28         public static boolean load_library() {
29                 if (!initialized) {
30                         try {
31                                 System.loadLibrary("altos");
32                                 libaltos.altos_init();
33                                 loaded_library = true;
34                         } catch (UnsatisfiedLinkError e) {
35                                 loaded_library = false;
36                         }
37                         initialized = true;
38                 }
39                 return loaded_library;
40         }
41
42         static String bt_product_telebt() {
43                 if (load_library())
44                         return libaltosConstants.BLUETOOTH_PRODUCT_TELEBT;
45                 return "TeleBT";
46         }
47
48         public final static String bt_product_telebt = bt_product_telebt();
49         public final static String bt_product_any = "Any";
50         public final static String bt_product_basestation = "Basestation";
51
52         public String getProduct() {
53                 String  name = getName();
54                 if (name == null)
55                         return "Altus Metrum";
56                 int     dash = name.lastIndexOf("-");
57                 if (dash < 0)
58                         return name;
59                 return name.substring(0,dash);
60         }
61
62         public int getSerial() {
63                 String name = getName();
64                 if (name == null)
65                         return 0;
66                 int dash = name.lastIndexOf("-");
67                 if (dash < 0 || dash >= name.length())
68                         return 0;
69                 String sn = name.substring(dash + 1, name.length());
70                 try {
71                         return Integer.parseInt(sn);
72                 } catch (NumberFormatException ne) {
73                         return 0;
74                 }
75         }
76
77         public String toString() {
78                 String  name = getName();
79                 if (name == null)
80                         name = "Altus Metrum";
81                 return String.format("%-20.20s %4d %s",
82                                      getProduct(), getSerial(), getAddr());
83         }
84
85         public String toShortString() {
86                 String  name = getName();
87                 if (name == null)
88                         name = "Altus Metrum";
89                 return String.format("%s %d %s",
90                                      getProduct(), getSerial(), getAddr());
91
92         }
93
94         public boolean isAltusMetrum() {
95                 if (getName().startsWith(bt_product_telebt))
96                         return true;
97                 return false;
98         }
99
100         public boolean matchProduct(String want_product) {
101
102                 if (!isAltusMetrum())
103                         return false;
104
105                 if (want_product.equals(bt_product_any))
106                         return true;
107
108                 if (want_product.equals(bt_product_basestation))
109                         return matchProduct(bt_product_telebt);
110
111                 if (want_product.equals(getProduct()))
112                         return true;
113
114                 return false;
115         }
116
117         static AltosBTDevice[] list(String product) {
118                 if (!load_library())
119                         return null;
120
121                 SWIGTYPE_p_altos_bt_list list = libaltos.altos_bt_list_start();
122
123                 ArrayList<AltosBTDevice> device_list = new ArrayList<AltosBTDevice>();
124                 if (list != null) {
125                         SWIGTYPE_p_altos_file file;
126
127                         for (;;) {
128                                 AltosBTDevice device = new AltosBTDevice();
129                                 if (libaltos.altos_bt_list_next(list, device) == 0)
130                                         break;
131                                 if (device.matchProduct(product))
132                                         device_list.add(device);
133                         }
134                         libaltos.altos_bt_list_finish(list);
135                 }
136
137                 AltosBTDevice[] devices = new AltosBTDevice[device_list.size()];
138                 for (int i = 0; i < device_list.size(); i++)
139                         devices[i] = device_list.get(i);
140                 return devices;
141         }
142 }