03e7cbeca929c97e4030b76c0346df4bd99f6b3d
[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 libaltosJNI.*;
20
21 public class AltosBTDevice extends altos_bt_device implements AltosDevice {
22
23         public String getProductName() {
24                 String  name = getName();
25                 if (name == null)
26                         return "Altus Metrum";
27                 int     dash = name.lastIndexOf("-");
28                 if (dash < 0)
29                         return name;
30                 return name.substring(0,dash);
31         }
32
33         public int getProduct() {
34                 if (Altos.bt_product_telebt.equals(getProductName()))
35                         return Altos.product_telebt;
36                 return 0;
37         }
38
39         public String getPath() {
40                 return getAddr();
41         }
42
43         public String getErrorString() {
44                 altos_error     error = new altos_error();
45
46                 libaltos.altos_get_last_error(error);
47                 return String.format("%s (%d)", error.getString(), error.getCode());
48         }
49
50         public int getSerial() {
51                 String name = getName();
52                 if (name == null)
53                         return 0;
54                 int dash = name.lastIndexOf("-");
55                 if (dash < 0 || dash >= name.length())
56                         return 0;
57                 String sn = name.substring(dash + 1, name.length());
58                 try {
59                         return Integer.parseInt(sn);
60                 } catch (NumberFormatException ne) {
61                         return 0;
62                 }
63         }
64
65         public String toString() {
66                 return String.format("%-20.20s %4d %s",
67                                      getProductName(), getSerial(), getAddr());
68         }
69
70         public String toShortString() {
71                 return String.format("%s %d %s",
72                                      getProductName(), getSerial(), getAddr());
73
74         }
75
76         public SWIGTYPE_p_altos_file open() {
77                 return libaltos.altos_bt_open(this);
78         }
79
80         /*
81         private boolean isAltusMetrum() {
82                 if (getName().startsWith(Altos.bt_product_telebt))
83                         return true;
84                 return false;
85         }
86         */
87
88         public boolean matchProduct(int want_product) {
89
90 //              if (!isAltusMetrum())
91 //                      return false;
92
93                 if (want_product == Altos.product_any)
94                         return true;
95
96                 if (want_product == Altos.product_basestation)
97                         return matchProduct(Altos.product_telebt);
98
99                 if (want_product == getProduct())
100                         return true;
101
102                 return false;
103         }
104
105         public boolean equals(Object o) {
106                 if (!(o instanceof AltosBTDevice))
107                         return false;
108                 AltosBTDevice other = (AltosBTDevice) o;
109                 return getName().equals(other.getName()) && getAddr().equals(other.getAddr());
110         }
111
112         public int hashCode() {
113                 return getName().hashCode() ^ getAddr().hashCode();
114         }
115
116         public AltosBTDevice(String name, String addr) {
117                 Altos.load_library();
118                 libaltos.altos_bt_fill_in(name, addr,this);
119         }
120
121         public AltosBTDevice() {
122         }
123 }