Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattServer.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.ArrayList;
29import java.util.List;
30
31/**
32 * Representing a complete list of Gatt Service objects from the GATT server perspective,
33 * i.e. the Gatt Server database.
34 *
35 * One instance shall be attached to BTAdapter when advertising via BTAdapter::startAdvertising(),
36 * changing its operating mode to Gatt Server mode.
37 *
38 * The instance can also be retrieved via BTAdapter::getGATTServerData().
39 *
40 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
41 *
42 * @since 2.4.0
43 */
44public final class DBGattServer implements AutoCloseable
45{
46 private volatile long nativeInstance;
47 /* pp */ long getNativeInstance() { return nativeInstance; }
48
49 /**
50 * Listener to remote master device's operations on the local GATT-Server.
51 *
52 * All methods shall return as soon as possible to not block GATT event processing.
53 */
54 public static abstract class Listener implements AutoCloseable {
55 private volatile long nativeInstance;
56 /* pp */ long getNativeInstance() { return nativeInstance; }
57
58 /**
59 *
60 * @param max_att_mtu_
61 * @param services_
62 */
63 public Listener() {
64 nativeInstance = ctorImpl();
65 }
66 private native long ctorImpl();
67
68 @Override
69 public void close() {
70 final long handle;
71 synchronized( this ) {
72 handle = nativeInstance;
73 nativeInstance = 0;
74 }
75 if( 0 != handle ) {
76 dtorImpl(handle);
77 }
78 }
79 private static native void dtorImpl(final long nativeInstance);
80
81 @Override
82 public void finalize() {
83 close();
84 }
85
86 /**
87 * Notification that device got connected.
88 *
89 * Convenient user entry, allowing to setup resources.
90 *
91 * @param device the connected device
92 * @param initialMTU initial used minimum MTU until negotiated.
93 */
94 public abstract void connected(final BTDevice device, final int initialMTU);
95
96 /**
97 * Notification that device got disconnected.
98 *
99 * Convenient user entry, allowing to clean up resources.
100 *
101 * @param device the disconnected device.
102 */
103 public abstract void disconnected(final BTDevice device);
104
105 /**
106 * Notification that the MTU has changed.
107 *
108 * @param device the device for which the MTU has changed
109 * @param mtu the new negotiated MTU
110 */
111 public abstract void mtuChanged(final BTDevice device, final int mtu);
112
113 /**
114 * Signals attempt to read a value.
115 *
116 * Callee shall accept the read request by returning true, otherwise false.
117 *
118 * @param device
119 * @param s
120 * @param c
121 * @return true if master read has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the read action will be allowed.
122 */
123 public abstract boolean readCharValue(final BTDevice device, final DBGattService s, final DBGattChar c);
124
125 /**
126 * Signals attempt to read a value.
127 *
128 * Callee shall accept the read request by returning true, otherwise false.
129 *
130 * @param device
131 * @param s
132 * @param c
133 * @param d
134 * @return true if master read has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the read action will be allowed.
135 */
136 public abstract boolean readDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d);
137
138 /**
139 * Signals attempt to write a single or bulk (prepare) value.
140 *
141 * Callee shall accept the write request by returning true, otherwise false.
142 *
143 * @param device
144 * @param s
145 * @param c
146 * @param value
147 * @param value_offset
148 * @return true if master write has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the write action will be allowed.
149 * @see #writeCharValueDone(BTDevice, DBGattService, DBGattChar)
150 */
151 public abstract boolean writeCharValue(final BTDevice device, final DBGattService s, final DBGattChar c, final byte[] value, final int value_offset);
152
153 /**
154 * Notifies completion of single or bulk {@link #writeCharValue(BTDevice, DBGattService, DBGattChar, byte[], int) writeCharValue()} after having accepted and performed all write requests.
155 *
156 * @param device
157 * @param s
158 * @param c
159 * @see #writeCharValue(BTDevice, DBGattService, DBGattChar, byte[], int)
160 */
161 public abstract void writeCharValueDone(final BTDevice device, final DBGattService s, final DBGattChar c);
162
163 /**
164 * Signals attempt to write a single or bulk (prepare) value.
165 *
166 * Callee shall accept the write request by returning true, otherwise false.
167 *
168 * @param device
169 * @param s
170 * @param c
171 * @param d
172 * @param value
173 * @param value_offset
174 * @return true if master write has been accepted by GATT-Server listener, otherwise false. Only if all listener return true, the write action will be allowed.
175 * @see #writeDescValueDone(BTDevice, DBGattService, DBGattChar, DBGattDesc)
176 */
177 public abstract boolean writeDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d,
178 final byte[] value, final int value_offset);
179
180 /**
181 * Notifies completion of single or bulk {@link #writeDescValue(BTDevice, DBGattService, DBGattChar, DBGattDesc, byte[], int) writeDescValue()} after having accepted and performed all write requests.
182 *
183 * @param device
184 * @param s
185 * @param c
186 * @param d
187 * @see #writeDescValue(BTDevice, DBGattService, DBGattChar, DBGattDesc, byte[], int)
188 */
189 public abstract void writeDescValueDone(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d);
190
191 /**
192 * Notifies a change of the Client Characteristic Configuration Descriptor (CCCD) value.
193 *
194 * @param device
195 * @param s
196 * @param c
197 * @param d
198 * @param notificationEnabled
199 * @param indicationEnabled
200 */
201 public abstract void clientCharConfigChanged(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d,
202 final boolean notificationEnabled, final boolean indicationEnabled);
203
204 /**
205 * Default comparison operator, merely testing for same memory reference.
206 * <p>
207 * Specializations may override.
208 * </p>
209 */
210 @Override
211 public boolean equals(final Object other) {
212 return this == other;
213 }
214 }
215
216 /** Used maximum server Rx ATT_MTU, defaults to 512+1. */
217 public native int getMaxAttMTU();
218
219 /**
220 * Set maximum server Rx ATT_MTU, defaults to 512+1 limit.
221 *
222 * Method can only be issued before passing instance to BTAdapter::startAdvertising()
223 * @see BTAdapter::startAdvertising()
224 */
225 public native void setMaxAttMTU(final int v);
226
227 /* pp */ final List<DBGattService> services;
228
229 /** List of Services. */
230 public List<DBGattService> getServices() { return services; }
231
232 /**
233 *
234 * @param max_att_mtu_
235 * @param services_
236 */
237 public DBGattServer(final int max_att_mtu_, final List<DBGattService> services_) {
238 services = services_;
239
240 final long[] nativeServices = new long[services_.size()];
241 for(int i=0; i < nativeServices.length; i++) {
242 nativeServices[i] = services_.get(i).getNativeInstance();
243 }
244 nativeInstance = ctorImpl(Math.max(512+1, max_att_mtu_), nativeServices);
245 }
246 private native long ctorImpl(final int max_att_mtu, final long[] services);
247
248 @Override
249 public void close() {
250 final long handle;
251 synchronized( this ) {
252 handle = nativeInstance;
253 nativeInstance = 0;
254 }
255 if( 0 != handle ) {
256 dtorImpl(handle);
257 }
258 }
259 private static native void dtorImpl(final long nativeInstance);
260
261 @Override
262 public void finalize() {
263 close();
264 }
265
266 /**
267 * Ctor using default maximum ATT_MTU of 512+1
268 * @param services_
269 */
270 public DBGattServer(final List<DBGattService> services_) {
271 this(512+1, services_);
272 }
273 /**
274 * Default empty ctor
275 */
276 public DBGattServer() {
277 this(512+1, new ArrayList<DBGattService>());
278 }
279
280 public DBGattService findGattService(final String service_uuid) {
281 for(final DBGattService s : services) {
282 if( service_uuid.equals( s.getType() ) ) {
283 return s;
284 }
285 }
286 return null;
287 }
288 public DBGattChar findGattChar(final String service_uuid, final String char_uuid) {
289 final DBGattService service = findGattService(service_uuid);
290 if( null == service ) {
291 return null;
292 }
293 return service.findGattChar(char_uuid);
294 }
295 public DBGattDesc findGattClientCharConfig(final String service_uuid, final String char_uuid) {
296 final DBGattChar c = findGattChar(service_uuid, char_uuid);
297 if( null == c ) {
298 return null;
299 }
300 return c.getClientCharConfig();
301 }
302 public boolean resetGattClientCharConfig(final String service_uuid, final String char_uuid) {
303 final DBGattDesc d = findGattClientCharConfig(service_uuid, char_uuid);
304 if( null == d ) {
305 return false;
306 }
307 d.bzero();
308 return true;
309 }
310
311 public DBGattChar findGattCharByValueHandle(final short char_value_handle) {
312 for(final DBGattService s : services) {
313 final DBGattChar r = s.findGattCharByValueHandle(char_value_handle);
314 if( null != r ) {
315 return r;
316 }
317 }
318 return null;
319 }
320
321 public synchronized boolean addListener(final Listener l) {
322 return addListenerImpl(l);
323 }
324 private native boolean addListenerImpl(Listener l);
325
326 public synchronized boolean removeListener(final Listener l) {
327 return removeListenerImpl(l);
328 }
329 private native boolean removeListenerImpl(Listener l);
330
331 public String toFullString() {
332 final String newline = System.lineSeparator();
333 final StringBuilder res = new StringBuilder(toString()+newline);
334 for(final DBGattService s : services) {
335 res.append(" ").append(s.toString()).append(newline);
336 for(final DBGattChar c : s.characteristics) {
337 res.append(" ").append(c.toString()).append(newline);
338 for(final DBGattDesc d : c.descriptors) {
339 res.append(" ").append(d.toString()).append(newline);
340 }
341 }
342 }
343 return res.toString();
344 }
345
346 @Override
347 public native String toString();
348}
Representing a Gatt Characteristic object from the GATT server perspective.
Definition: DBGattChar.java:46
native String toString()
DBGattDesc getClientCharConfig()
Representing a Gatt Characteristic Descriptor object from the GATT server perspective.
Definition: DBGattDesc.java:41
native String toString()
native void bzero()
Fill value with zero bytes.
Listener to remote master device's operations on the local GATT-Server.
abstract boolean readCharValue(final BTDevice device, final DBGattService s, final DBGattChar c)
Signals attempt to read a value.
abstract void writeDescValueDone(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d)
Notifies completion of single or bulk writeDescValue() after having accepted and performed all write ...
abstract void connected(final BTDevice device, final int initialMTU)
Notification that device got connected.
abstract void clientCharConfigChanged(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d, final boolean notificationEnabled, final boolean indicationEnabled)
Notifies a change of the Client Characteristic Configuration Descriptor (CCCD) value.
abstract boolean writeCharValue(final BTDevice device, final DBGattService s, final DBGattChar c, final byte[] value, final int value_offset)
Signals attempt to write a single or bulk (prepare) value.
abstract void mtuChanged(final BTDevice device, final int mtu)
Notification that the MTU has changed.
abstract void disconnected(final BTDevice device)
Notification that device got disconnected.
abstract boolean writeDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d, final byte[] value, final int value_offset)
Signals attempt to write a single or bulk (prepare) value.
abstract void writeCharValueDone(final BTDevice device, final DBGattService s, final DBGattChar c)
Notifies completion of single or bulk writeCharValue() after having accepted and performed all write ...
abstract boolean readDescValue(final BTDevice device, final DBGattService s, final DBGattChar c, final DBGattDesc d)
Signals attempt to read a value.
boolean equals(final Object other)
Default comparison operator, merely testing for same memory reference.
Representing a complete list of Gatt Service objects from the GATT server perspective,...
native String toString()
DBGattService findGattService(final String service_uuid)
native void setMaxAttMTU(final int v)
Set maximum server Rx ATT_MTU, defaults to 512+1 limit.
DBGattDesc findGattClientCharConfig(final String service_uuid, final String char_uuid)
boolean resetGattClientCharConfig(final String service_uuid, final String char_uuid)
native int getMaxAttMTU()
Used maximum server Rx ATT_MTU, defaults to 512+1.
DBGattChar findGattCharByValueHandle(final short char_value_handle)
DBGattChar findGattChar(final String service_uuid, final String char_uuid)
DBGattServer()
Default empty ctor.
synchronized boolean removeListener(final Listener l)
synchronized boolean addListener(final Listener l)
DBGattServer(final int max_att_mtu_, final List< DBGattService > services_)
List< DBGattService > getServices()
List of Services.
DBGattServer(final List< DBGattService > services_)
Ctor using default maximum ATT_MTU of 512+1.
Representing a Gatt Service object from the ::GATTRole::Server perspective.
DBGattChar findGattChar(final String char_uuid)
BTDevice represents one remote Bluetooth device.
Definition: BTDevice.java:47