summaryrefslogtreecommitdiffstats
path: root/java/jau/direct_bt/DBTGattChar.java
blob: ac8cf2fe80dcf8cb72735df6fa91be2af822a63f (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
/**
 * 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.List;

import org.direct_bt.BTException;
import org.direct_bt.BTGattChar;
import org.direct_bt.BTGattDesc;
import org.direct_bt.BTGattService;
import org.direct_bt.GattCharPropertySet;
import org.jau.util.BasicTypes;
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;

    private final GattCharPropertySet properties;

    /* 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;

    /* Optional Characteristic User Description index within descriptorList */
    private final int userDescriptionIndex;

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

    boolean enabledNotifyState = false;
    boolean enabledIndicateState = false;

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

        this.properties = properties;
        this.value_type_uuid = value_type_uuid;
        this.value_handle = value_handle;
        this.clientCharacteristicsConfigIndex = clientCharacteristicsConfigIndex;
        this.userDescriptionIndex = userDescriptionIndex;
        this.descriptorList = getDescriptorsImpl();

        if( DEBUG ) {
            final boolean hasNotify = properties.isSet(GattCharPropertySet.Type.Notify);
            final boolean hasIndicate = properties.isSet(GattCharPropertySet.Type.Indicate);

            if( hasNotify || hasIndicate ) {
                final BTGattCharListener characteristicListener = new BTGattCharListener() {
                    @Override
                    public void notificationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp) {
                        System.err.println("GATTCharacteristicListener.notificationReceived: "+charDecl+
                                           ", value[len "+value.length+": "+BasicTypes.bytesHexString(value, 0, -1, true)+"]");
                    }
                    @Override
                    public void indicationReceived(final BTGattChar charDecl, final byte[] value, final long timestamp,
                                                   final boolean confirmationSent) {
                        System.err.println("GATTCharacteristicListener.indicationReceived: "+charDecl+
                                           ", value[len "+value.length+": "+BasicTypes.bytesHexString(value, 0, -1, true)+
                                           "], confirmationSent "+confirmationSent);
                    }
                };
                this.addCharListener(characteristicListener); // silent, don't enable native GATT ourselves
            }
        }
    }
    private native List<BTGattDesc> getDescriptorsImpl();

    @Override
    protected native void deleteImpl(long nativeInstance);

    @Override
    public synchronized void close() {
        if( !isNativeValid() ) {
            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 BTGattDesc findGattDesc(final String desc_uuid) {
        final DBTGattService service = wbr_service.get();
        if( null == service ) {
            return null;
        }
        final DBTDevice device = service.wbr_device.get();
        if( null == device ) {
            return null;
        }
        final int size = descriptorList.size();
        for(int i = 0; i < size; i++ ) {
            final DBTGattDesc descr = (DBTGattDesc) descriptorList.get(i);
            if( descr.getUUID().equals(desc_uuid) ) {
                return descr;
            }
        }
        return null;
    }

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

    @Override
    public final boolean getNotifying(final boolean enabledState[/*2*/]) {
        enabledState[0] = enabledNotifyState;
        enabledState[1] = enabledIndicateState;
        return enabledNotifyState || enabledIndicateState;
    }

    @Override
    public final GattCharPropertySet getProperties() { return properties; }

    @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
    {
        final boolean hasNotify = properties.isSet(GattCharPropertySet.Type.Notify);
        final boolean hasIndicate = properties.isSet(GattCharPropertySet.Type.Indicate);

        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( !res ) {
                enabledState[0] = false;
                enabledState[1] = false;
            }
            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, enabledState[0],
                        enableIndication, hasIndicate, enabledIndicateState, enabledState[1]);
            }
            enabledNotifyState = enabledState[0];
            enabledIndicateState = enabledState[1];
            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 = properties.isSet(GattCharPropertySet.Type.Notify);
        final boolean enableIndication = !enableNotification && properties.isSet(GattCharPropertySet.Type.Indicate);

        return configNotificationIndication(enableNotification, enableIndication, enabledState);
    }

    @Override
    public boolean disableIndicationNotification() throws IllegalStateException {
        return configNotificationIndication(false /* enableNotification */, false /* enableIndication */, new boolean[2]);
    }

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

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

    @Override
    public final boolean removeCharListener(final BTGattCharListener listener) {
        return getService().getDevice().removeCharListener( listener );
    }

    @Override
    public final int removeAllAssociatedCharListener(final boolean shallDisableIndicationNotification) {
        if( shallDisableIndicationNotification ) {
            disableIndicationNotification();
        }
        return getService().getDevice().removeAllAssociatedCharListener(this);
    }

    /**
     * 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; }

    @Override
    public final BTGattDesc getClientCharConfig() {
        if( 0 > clientCharacteristicsConfigIndex ) {
            return null;
        }
        return descriptorList.get(clientCharacteristicsConfigIndex); // exception if out of bounds
    }

    @Override
    public final BTGattDesc getUserDescription() {
        if( 0 > userDescriptionIndex ) {
            return null;
        }
        return descriptorList.get(userDescriptionIndex); // exception if out of bounds
    }

    @Override
    public final byte[] readValue() throws BTException {
        return readValueImpl();
    }
    private native byte[] readValueImpl() throws BTException;

    @Override
    public final boolean writeValue(final byte[] value, final boolean withResponse) throws BTException {
        return writeValueImpl(value, withResponse);
    }
    private native boolean writeValueImpl(byte[] argValue, boolean withResponse) throws BTException;

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