Add shipping products to fat_altos target, note that in Releasing
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import libaltosJNI.*;
22 import org.altusmetrum.altoslib_13.*;
23
24 public class AltosBTDevice extends altos_bt_device implements AltosDevice {
25
26         public String getProductName() {
27                 String  name = getName();
28                 if (name == null)
29                         return "Altus Metrum";
30                 int     dash = name.lastIndexOf("-");
31                 if (dash < 0)
32                         return name;
33                 return name.substring(0,dash);
34         }
35
36         public int getProduct() {
37                 if (AltosLib.bt_product_telebt.equals(getProductName()))
38                         return AltosLib.product_telebt;
39                 return 0;
40         }
41
42         public String getPath() {
43                 return getAddr();
44         }
45
46         public String getErrorString() {
47                 altos_error     error = new altos_error();
48
49                 libaltos.altos_get_last_error(error);
50                 return String.format("%s (%d)", error.getString(), error.getCode());
51         }
52
53         public int getSerial() {
54                 String name = getName();
55                 if (name == null)
56                         return 0;
57                 int dash = name.lastIndexOf("-");
58                 if (dash < 0 || dash >= name.length())
59                         return 0;
60                 String sn = name.substring(dash + 1, name.length());
61                 try {
62                         return Integer.parseInt(sn);
63                 } catch (NumberFormatException ne) {
64                         return 0;
65                 }
66         }
67
68         public String toString() {
69                 return String.format("%-20.20s %4d %s",
70                                      getProductName(), getSerial(), getAddr());
71         }
72
73         public String toShortString() {
74                 return String.format("%s %d %s",
75                                      getProductName(), getSerial(), getAddr());
76
77         }
78
79         public SWIGTYPE_p_altos_file open() {
80                 return libaltos.altos_bt_open(this);
81         }
82
83         /*
84         private boolean isAltusMetrum() {
85                 if (getName().startsWith(Altos.bt_product_telebt))
86                         return true;
87                 return false;
88         }
89         */
90
91         public boolean matchProduct(int want_product) {
92
93 //              if (!isAltusMetrum())
94 //                      return false;
95
96                 if (want_product == AltosLib.product_any)
97                         return true;
98
99                 if (want_product == AltosLib.product_basestation)
100                         return matchProduct(AltosLib.product_telebt);
101
102                 if (want_product == getProduct())
103                         return true;
104
105                 return false;
106         }
107
108         public int hashCode() {
109                 return getName().hashCode() ^ getAddr().hashCode();
110         }
111
112         public boolean equals(Object o) {
113                 if (o == null)
114                         return false;
115
116                 if (!(o instanceof AltosBTDevice))
117                         return false;
118                 AltosBTDevice other = (AltosBTDevice) o;
119                 return getName().equals(other.getName()) && getAddr().equals(other.getAddr());
120         }
121
122         public AltosBTDevice(String name, String addr) {
123                 AltosUILib.load_library();
124                 libaltos.altos_bt_fill_in(name, addr,this);
125         }
126
127         public AltosBTDevice() {
128         }
129 }