aboutsummaryrefslogtreecommitdiffstats
path: root/java/jau/direct_bt/DBTGattChar.java
blob: b3ec322224b5e86fad7d29fdccb4420325869385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package jau.direct_bt;

import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.List;

import org.direct_bt.BTException;
import org.direct_bt.BTFactory;
import org.direct_bt.BTGattChar;
import org.direct_bt.BTGattDesc;
import org.direct_bt.BTGattService;
import org.direct_bt.BTNotification;
import org.direct_bt.BTObject;
import org.direct_bt.BTType;
import org.direct_bt.BTUtils;
import org.direct_bt.BTGattCharListener;

public class DBTGattChar extends DBTObject implements BTGattChar
{
    private static final boolean DEBUG = DBTManager.DEBUG;

    /** Characteristics's service weak back-reference */
    final WeakReference<DBTGattService> wbr_service;

    /**
     * Characteristic Handle of this instance.
     * <p>
     * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
     * </p>
     */
    private final short handle;

    /* Characteristics Property */
    private final String[] properties;
    private final boolean hasNotify;
    private final boolean hasIndicate;

    /* Characteristics Value Type UUID */
    private final String value_type_uuid;

    /**
     * Characteristics Value Handle.
     * <p>
     * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
     * </p>
     */
    private final short value_handle;

    /* Optional Client Characteristic Configuration index within descriptorList */
    private final int clientCharacteristicsConfigIndex;

    /* pp */ final List<BTGattDesc> descriptorList;

    private final boolean supCharValueCacheNotification;

    boolean enabledNotifyState = false;
    boolean enabledIndicateState = false;

    private byte[] cachedValue = null;
    private BTNotification<byte[]> valueNotificationCB = null;

    private boolean updateCachedValue(final byte[] value, final boolean notify) {
        boolean valueChanged = false;
        if( null == cachedValue || cachedValue.length != value.length ) {
            cachedValue = new byte[value.length];
            valueChanged = true;
        } else if( !Arrays.equals(value, cachedValue) ) {
            valueChanged = true;
        }
        if( valueChanged ) {
            System.arraycopy(value, 0, cachedValue, 0, value.length);
            if( notify && null != valueNotificationCB ) {
                valueNotificationCB.run(cachedValue);
            }
        }
        return valueChanged;
    }

   /* pp */ DBTGattChar(final long nativeInstance, final DBTGattService service,
                                  final short handle, final String[] properties,
                                  final boolean hasNotify, final boolean hasIndicate,
                                  final String value_type_uuid, final short value_handle,
                                  final int clientCharacteristicsConfigIndex)
    {
        super(nativeInstance, handle /* hash */);
        this.wbr_service = new WeakReference<DBTGattService>(service);
        this.handle = handle;

        this.properties = properties;
        this.hasNotify = hasNotify;
        this.hasIndicate = hasIndicate;
        this.value_type_uuid = value_type_uuid;
        this.value_handle = value_handle;
        this.clientCharacteristicsConfigIndex = clientCharacteristicsConfigIndex;
        this.descriptorList = getDescriptorsImpl();
        this.supCharValueCacheNotification = DBTManager.getManager().getSettings().isCharValueCacheNotificationSupported();

        if( ( BTFactory.DEBUG || supCharValueCacheNotification ) &&
            ( hasNotify || hasIndicate )
          )
        {
            // This characteristicListener serves TinyB 'enableValueNotification(..)' and 'getValue()' (cached value)
            // backwards compatibility only!
            final Listener characteristicListener = new Listener() {
                @Override
                public void notificationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp) {
                    final DBTGattChar cd = (DBTGattChar)charDecl;
                    if( !cd.equals(DBTGattChar.this) ) {
                        throw new InternalError("Filtered GATTCharacteristicListener.notificationReceived: Wrong Characteristic: Got "+charDecl+
                                                ", expected "+DBTGattChar.this.toString());
                    }
                    final boolean valueChanged;
                    if( supCharValueCacheNotification ) {
                        valueChanged = updateCachedValue(value, true);
                    } else {
                        valueChanged = true;
                    }
                    if( DEBUG ) {
                        System.err.println("GATTCharacteristicListener.notificationReceived: "+charDecl+
                                           ", value[changed "+valueChanged+", len "+value.length+": "+BTUtils.bytesHexString(value, 0, -1, true)+"]");
                    }
                }
                @Override
                public void indicationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp,
                                               final boolean confirmationSent) {
                    final DBTGattChar cd = (DBTGattChar)charDecl;
                    if( !cd.equals(DBTGattChar.this) ) {
                        throw new InternalError("Filtered GATTCharacteristicListener.indicationReceived: Wrong Characteristic: Got "+charDecl+
                                                ", expected "+DBTGattChar.this.toString());
                    }
                    final boolean valueChanged;
                    if( supCharValueCacheNotification ) {
                        valueChanged = updateCachedValue(value, true);
                    } else {
                        valueChanged = true;
                    }
                    if( DEBUG ) {
                        System.err.println("GATTCharacteristicListener.indicationReceived: "+charDecl+
                                           ", value[changed "+valueChanged+", len "+value.length+": "+BTUtils.bytesHexString(value, 0, -1, true)+
                                           "], confirmationSent "+confirmationSent);
                    }
                }
            };
            this.addCharListener(characteristicListener); // silent, don't enable native GATT ourselves
        }
    }

    @Override
    public synchronized void close() {
        if( !isValid() ) {
            return;
        }
        removeAllAssociatedCharListener(true);
        super.close();
    }

    @Override
    public boolean equals(final Object obj)
    {
        if (obj == null || !(obj instanceof DBTGattChar)) {
            return false;
        }
        final DBTGattChar other = (DBTGattChar)obj;
        return handle == other.handle; /** unique attribute handles */
    }

    @Override
    public String getUUID() { return value_type_uuid; }

    @Override
    public BTType getBluetoothType() { return class_type(); }

    static BTType class_type() { return BTType.GATT_CHARACTERISTIC; }

    @Override
    public BTGattChar clone()
    { throw new UnsupportedOperationException(); } // FIXME

    @Override
    public BTGattDesc find(final String UUID, final long timeoutMS) {
        if( !checkServiceCache() ) {
            return null;
        }
        return (DBTGattDesc) findInCache(UUID, BTType.GATT_DESCRIPTOR);
    }

    @Override
    public BTGattDesc find(final String UUID) {
        return find(UUID, 0);
    }

    @Override
    public final BTGattService getService() { return wbr_service.get(); }

    @Override
    public final String[] getFlags() { return properties; }

    @Override
    public final byte[] getValue() { return cachedValue; }

    @Override
    public final byte[] readValue() throws BTException {
        if( supCharValueCacheNotification ) {
            final byte[] value = readValueImpl();
            updateCachedValue(value, true);
            return cachedValue;
        } else {
            return readValueImpl();
        }
    }

    @Override
    public final boolean writeValue(final byte[] value, final boolean withResponse) throws BTException {
        final boolean res = writeValueImpl(value, withResponse);
        if( supCharValueCacheNotification && res ) {
            updateCachedValue(value, false);
        }
        return res;
    }

    @Override
    public final List<BTGattDesc> getDescriptors() { return descriptorList; }

    @Override
    public final synchronized boolean configNotificationIndication(final boolean enableNotification, final boolean enableIndication, final boolean enabledState[/*2*/])
            throws IllegalStateException
    {
        if( hasNotify || hasIndicate ) {
            final boolean resEnableNotification = hasNotify && enableNotification;
            final boolean resEnableIndication = hasIndicate && enableIndication;

            if( resEnableNotification == enabledNotifyState &&
                resEnableIndication == enabledIndicateState )
            {
                enabledState[0] = resEnableNotification;
                enabledState[1] = resEnableIndication;
                if( DEBUG ) {
                    System.err.printf("GATTCharacteristic.configNotificationIndication: Unchanged: notification[shall %b, has %b: %b == %b], indication[shall %b, has %b: %b == %b]\n",
                        enableNotification, hasNotify, enabledNotifyState, resEnableNotification,
                        enableIndication, hasIndicate, enabledIndicateState, resEnableIndication);
                }
                return true;
            }

            final boolean res = configNotificationIndicationImpl(enableNotification, enableIndication, enabledState);
            if( DEBUG ) {
                System.err.printf("GATTCharacteristic.configNotificationIndication: res %b, notification[shall %b, has %b: %b -> %b], indication[shall %b, has %b: %b -> %b]\n",
                        res,
                        enableNotification, hasNotify, enabledNotifyState, resEnableNotification,
                        enableIndication, hasIndicate, enabledIndicateState, resEnableIndication);
            }
            if( res ) {
                enabledNotifyState = resEnableNotification;
                enabledIndicateState = resEnableIndication;
            }
            return res;
        } else {
            enabledState[0] = false;
            enabledState[1] = false;
            if( DEBUG ) {
                System.err.println("GATTCharacteristic.configNotificationIndication: FALSE*: hasNotify "+hasNotify+", hasIndicate "+hasIndicate);
            }
            return false;
        }
    }
    private native boolean configNotificationIndicationImpl(boolean enableNotification, boolean enableIndication, final boolean enabledState[/*2*/])
            throws IllegalStateException;

    @Override
    public boolean enableNotificationOrIndication(final boolean enabledState[/*2*/])
            throws IllegalStateException
    {
        final boolean enableNotification = hasNotify;
        final boolean enableIndication = !enableNotification && hasIndicate;

        return configNotificationIndication(enableNotification, enableIndication, enabledState);
    }

    static private class DelegatedBTGattCharListener extends BTGattCharListener {
        private final Listener delegate;

        public DelegatedBTGattCharListener(final BTGattChar characteristicMatch, final Listener l) {
            super(characteristicMatch);
            delegate = l;
        }

        @Override
        public void notificationReceived(final BTGattChar charDecl,
                                         final byte[] value, final long timestamp) {
            delegate.notificationReceived(charDecl, value, timestamp);
        }

        @Override
        public void indicationReceived(final BTGattChar charDecl,
                                       final byte[] value, final long timestamp,
                                       final boolean confirmationSent) {
            delegate.indicationReceived(charDecl, value, timestamp, confirmationSent);
        }
    };

    @Override
    public final boolean addCharListener(final Listener listener) {
        return getService().getDevice().addCharListener( new DelegatedBTGattCharListener(this, listener) );
    }

    @Override
    public final boolean addCharListener(final Listener listener, final boolean enabledState[/*2*/]) {
        if( !enableNotificationOrIndication(enabledState) ) {
            return false;
        }
        return addCharListener( listener );
    }

    @Override
    public final int removeAllAssociatedCharListener(final boolean disableIndicationNotification) {
        if( disableIndicationNotification ) {
            configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
        }
        valueNotificationCB = null;
        return getService().getDevice().removeAllAssociatedCharListener(this);
    }

    @Override
    public final synchronized void enableValueNotifications(final BTNotification<byte[]> callback) {
        if( !configNotificationIndication(true /* enableNotification */, true /* enableIndication */, new boolean[2]) ) {
            valueNotificationCB = null;
        } else {
            valueNotificationCB = callback;
        }
    }

    @Override
    public final synchronized void disableValueNotifications() {
        configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
        valueNotificationCB = null;
    }

    @Override
    public final boolean getNotifying() {
        return null != valueNotificationCB;
    }

    /**
     * Characteristic Handle of this instance.
     * <p>
     * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
     * </p>
     */
    public final short getHandle() { return handle; }

    /**
     * Returns Characteristics Value Handle.
     * <p>
     * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
     * </p>
     */
    public final short getValueHandle() { return value_handle; }

    /** Returns optional Client Characteristic Configuration index within descriptorList */
    public final int getClientCharacteristicsConfigIndex() { return clientCharacteristicsConfigIndex; }

    @Override
    public final String toString() {
        if( !isValid() ) {
            return "Characteristic" + "\u271D" + "[uuid "+getUUID()+", handle 0x"+Integer.toHexString(handle)+"]";
        }
        return toStringImpl();
    }

    /* Native method calls: */

    private native String toStringImpl();

    private native byte[] readValueImpl() throws BTException;

    private native boolean writeValueImpl(byte[] argValue, boolean withResponse) throws BTException;

    private native List<BTGattDesc> getDescriptorsImpl();

    @Override
    protected native void deleteImpl(long nativeInstance);

    /* local functionality */

    /* pp */ boolean checkServiceCache() {
        final DBTGattService service = wbr_service.get();
        if( null == service ) {
            return false;
        }
        final DBTDevice device = service.wbr_device.get();
        return null != device && device.checkServiceCache(false);
    }

    /**
     * Returns the matching {@link DBTObject} from the internal cache if found,
     * otherwise {@code null}.
     * <p>
     * The returned {@link DBTObject} may be of type
     * <ul>
     *   <li>{@link DBTGattDesc}</li>
     * </ul>
     * or alternatively in {@link BTObject} space
     * <ul>
     *   <li>{@link BTType#GATT_DESCRIPTOR} -> {@link BTGattDesc}</li>
     * </ul>
     * </p>
     * @param uuid UUID of the desired
     * {@link BTType#GATT_DESCRIPTOR descriptor} to be found.
     * Maybe {@code null}, in which case the first object of the desired type is being returned - if existing.
     * @param type specify the type of the object to be found, a {@link BTType#GATT_DESCRIPTOR descriptor}.
     * {@link BTType#NONE none} means anything.
     */
    /* pp */ DBTObject findInCache(final String uuid, final BTType type) {
        final boolean anyType = BTType.NONE == type;
        final boolean descType = BTType.GATT_DESCRIPTOR == type;

        if( !anyType && !descType ) {
            return null;
        }
        final int size = descriptorList.size();
        for(int i = 0; i < size; i++ ) {
            final DBTGattDesc descr = (DBTGattDesc) descriptorList.get(i);
            if( null == uuid || descr.getUUID().equals(uuid) ) {
                return descr;
            }
        }
        return null;
    }
}