Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattChar.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2021 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26package org.direct_bt;
27
28import java.util.List;
29
30/**
31 * Representing a Gatt Characteristic object from the GATT server perspective.
32 *
33 * A list of shared DBGattChar instances are passed at DBGattService construction
34 * and are retrievable via DBGattService::getChars().
35 *
36 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
37 *
38 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3 Characteristic Definition
39 *
40 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.6.1 Discover All Characteristics of a Service
41 *
42 * The handle represents a service's characteristics-declaration
43 * and the value the Characteristics Property, Characteristics Value Handle _and_ Characteristics UUID.
44 */
45public final class DBGattChar implements AutoCloseable
46{
47 private volatile long nativeInstance;
48 /* pp */ long getNativeInstance() { return nativeInstance; }
49
50 private final boolean enabledNotifyState = false;
51 private final boolean enabledIndicateState = false;
52
53 /**
54 * Selected standard GATT characteristic numbers in UUID16 format as defined.
55 */
56 public static class UUID16 {
57 //
58 // GENERIC_ACCESS
59 //
60 public static String DEVICE_NAME = "2a00";
61 public static String APPEARANCE = "2a01";
62 public static String PERIPHERAL_PRIVACY_FLAG = "2a02";
63 public static String RECONNECTION_ADDRESS = "2a03";
64 public static String PERIPHERAL_PREFERRED_CONNECTION_PARAMETERS = "2a04";
65
66 //
67 // GENERIC_ATTRIBUTE
68 //
69 public static String SERVICE_CHANGED = "2a05";
70
71 //
72 // DEVICE_INFORMATION
73 //
74 /** Mandatory: uint40 */
75 public static String SYSTEM_ID = "2a23";
76 public static String MODEL_NUMBER_STRING = "2a24";
77 public static String SERIAL_NUMBER_STRING = "2a25";
78 public static String FIRMWARE_REVISION_STRING = "2a26";
79 public static String HARDWARE_REVISION_STRING = "2a27";
80 public static String SOFTWARE_REVISION_STRING = "2a28";
81 public static String MANUFACTURER_NAME_STRING = "2a29";
82 public static String REGULATORY_CERT_DATA_LIST = "2a2a";
83 public static String PNP_ID = "2a50";
84 }
85
86 /**
87 * Characteristic Handle of this instance.
88 * <p>
89 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
90 * </p>
91 */
92 public native short getHandle();
93
94 /**
95 * Characteristic end handle, inclusive.
96 * <p>
97 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
98 * </p>
99 */
100 public native short getEndHandle();
101
102 /**
103 * Characteristics Value Handle.
104 * <p>
105 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
106 * </p>
107 */
108 public native short getValueHandle();
109
110 private final String value_type;
111
112 /* Characteristics Value Type UUID (lower-case)*/
113 public String getValueType() { return value_type; }
114
115 private final GattCharPropertySet properties;
116
117 /* Characteristics Property */
118 public GattCharPropertySet getProperties() { return properties; }
119
120 /* pp */ final List<DBGattDesc> descriptors;
121
122 /** List of Characteristic Descriptions. */
123 public final List<DBGattDesc> getDescriptors() { return descriptors; }
124
125 /**
126 * Return a copy of this characteristic's native {@link DBGattValue} value.
127 *
128 * Its capacity defines the maximum writable variable length
129 * and its size defines the maximum writable fixed length.
130 */
131 public native DBGattValue getValue();
132
133 /**
134 * Set this characteristic's native value.
135 *
136 * Methods won't exceed the value's capacity if it is of hasVariableLength()
137 * or the value's size otherwise.
138 *
139 * @param source data to be written to this value
140 * @param source_len length of the source data to be written
141 * @param dest_pos position where the source data shall be written to the value
142 * @return true if successful, otherwise false for exceeding the value's limits or passing invalid parameter.
143 */
144 public native boolean setValue(final byte[] source, final int source_pos, final int source_len, final int dest_pos);
145
146 /* Optional Client Characteristic Configuration index within descriptorList */
147 public final int clientCharConfigIndex;
148
149 /* Optional Characteristic User Description index within descriptorList */
150 public final int userDescriptionIndex;
151
152 public DBGattChar(final String value_type_,
153 final GattCharPropertySet properties_,
154 final List<DBGattDesc> descriptors_,
155 final DBGattValue value_)
156 {
157 value_type = value_type_;
158 properties = properties_;
159 descriptors = descriptors_;
160
161 {
162 int clientCharConfigIndex_ = -1;
163 int userDescriptionIndex_ = -1;
164 int i=0;
165 for(final DBGattDesc d : descriptors) {
166 if( 0 > clientCharConfigIndex_ && d.isClientCharConfig() ) {
167 clientCharConfigIndex_=i;
168 } else if( 0 > userDescriptionIndex_ && d.isUserDescription() ) {
169 userDescriptionIndex_=i;
170 }
171 ++i;
172 }
173 clientCharConfigIndex = clientCharConfigIndex_;
174 userDescriptionIndex = userDescriptionIndex_;
175 }
176
177 final long[] nativeDescriptors = new long[descriptors_.size()];
178 for(int i=0; i < nativeDescriptors.length; i++) {
179 nativeDescriptors[i] = descriptors_.get(i).getNativeInstance();
180 }
181 nativeInstance = ctorImpl(value_type_, properties_.mask, nativeDescriptors, value_.data(), value_.capacity(), value_.hasVariableLength());
182 }
183 private native long ctorImpl(final String type,
184 final byte properties, final long[] descriptors,
185 final byte[] value, final int capacity, boolean variable_length);
186
187 @Override
188 public void close() {
189 final long handle;
190 synchronized( this ) {
191 handle = nativeInstance;
192 nativeInstance = 0;
193 }
194 if( 0 != handle ) {
195 dtorImpl(handle);
196 }
197 }
198 private static native void dtorImpl(final long nativeInstance);
199
200 @Override
201 public void finalize() {
202 close();
203 }
204
205 public boolean hasProperties(final GattCharPropertySet.Type bit) {
206 return properties.isSet(bit);
207 }
208
209 /** Fill value with zero bytes. */
210 public native void bzero();
211
213 if( 0 > clientCharConfigIndex ) {
214 return null;
215 }
216 return descriptors.get(clientCharConfigIndex); // abort if out of bounds
217 }
218
220 if( 0 > userDescriptionIndex ) {
221 return null;
222 }
223 return descriptors.get(userDescriptionIndex); // abort if out of bounds
224 }
225
226 @Override
227 public boolean equals(final Object other) {
228 if( this == other ) {
229 return true;
230 }
231 if( !(other instanceof DBGattChar) ) {
232 return false;
233 }
234 final DBGattChar o = (DBGattChar)other;
235 return getHandle() == o.getHandle() && getEndHandle() == o.getEndHandle(); /** unique attribute handles */
236 }
237
238 @Override
239 public native String toString();
240}
Selected standard GATT characteristic numbers in UUID16 format as defined.
Definition: DBGattChar.java:56
static String SOFTWARE_REVISION_STRING
Definition: DBGattChar.java:80
static String PERIPHERAL_PREFERRED_CONNECTION_PARAMETERS
Definition: DBGattChar.java:64
static String SYSTEM_ID
Mandatory: uint40.
Definition: DBGattChar.java:75
static String MANUFACTURER_NAME_STRING
Definition: DBGattChar.java:81
static String FIRMWARE_REVISION_STRING
Definition: DBGattChar.java:78
static String HARDWARE_REVISION_STRING
Definition: DBGattChar.java:79
static String REGULATORY_CERT_DATA_LIST
Definition: DBGattChar.java:82
static String PERIPHERAL_PRIVACY_FLAG
Definition: DBGattChar.java:62
Representing a Gatt Characteristic object from the GATT server perspective.
Definition: DBGattChar.java:46
native String toString()
final List< DBGattDesc > getDescriptors()
List of Characteristic Descriptions.
native short getHandle()
Characteristic Handle of this instance.
boolean hasProperties(final GattCharPropertySet.Type bit)
DBGattChar(final String value_type_, final GattCharPropertySet properties_, final List< DBGattDesc > descriptors_, final DBGattValue value_)
boolean equals(final Object other)
native void bzero()
Fill value with zero bytes.
DBGattDesc getUserDescription()
native DBGattValue getValue()
Return a copy of this characteristic's native DBGattValue value.
GattCharPropertySet getProperties()
DBGattDesc getClientCharConfig()
native short getValueHandle()
Characteristics Value Handle.
native boolean setValue(final byte[] source, final int source_pos, final int source_len, final int dest_pos)
Set this characteristic's native value.
native short getEndHandle()
Characteristic end handle, inclusive.
Representing a Gatt Characteristic Descriptor object from the GATT server perspective.
Definition: DBGattDesc.java:41
A copy of the native GATT value of DBGattChar or DBGattDesc.
int capacity()
Return the set capacity for this value.
byte[] data()
Returns the actual data of this value.
boolean hasVariableLength()
Returns true if this value has variable length.
Bit mask of GATT Characteristic Properties.
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.1.1 Characteristic Properties.