Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
DBTGattDesc.java
Go to the documentation of this file.
1 /**
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  * Copyright (c) 2020 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 
26 package jau.direct_bt;
27 
28 import java.lang.ref.WeakReference;
29 import java.util.Arrays;
30 
32 import org.direct_bt.BTGattDesc;
33 import org.direct_bt.BTType;
34 
35 public class DBTGattDesc extends DBTObject implements BTGattDesc
36 {
37  /** Descriptor's characteristic weak back-reference */
38  final WeakReference<DBTGattChar> wbr_characteristic;
39 
40  /** Type of Descriptor */
41  private final String type_uuid;
42 
43  /**
44  * Characteristic Descriptor Handle
45  * <p>
46  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
47  * </p>
48  */
49  private final short handle;
50 
51  private byte[] cachedValue;
52 
53  private void updateCachedValue(final byte[] value) {
54  if( null == cachedValue || cachedValue.length != value.length ) {
55  cachedValue = new byte[value.length];
56  }
57  System.arraycopy(value, 0, cachedValue, 0, value.length);
58  }
59 
60  /* pp */ DBTGattDesc(final long nativeInstance, final DBTGattChar characteristic,
61  final String type_uuid, final short handle, final byte[] value)
62  {
63  super(nativeInstance, handle /* hash */);
64  this.wbr_characteristic = new WeakReference<DBTGattChar>(characteristic);
65  this.type_uuid = type_uuid;
66  this.handle = handle;
67  this.cachedValue = value;
68  }
69 
70  @Override
71  public synchronized void close() {
72  if( !isValid() ) {
73  return;
74  }
75  super.close();
76  }
77 
78  @Override
79  public boolean equals(final Object obj)
80  {
81  if (obj == null || !(obj instanceof DBTGattDesc)) {
82  return false;
83  }
84  final DBTGattDesc other = (DBTGattDesc)obj;
85  return handle == other.handle; /** unique attribute handles */
86  }
87 
88  @Override
89  public String getUUID() { return type_uuid; }
90 
91  @Override
92  public BTType getBluetoothType() { return class_type(); }
93 
94  static BTType class_type() { return BTType.GATT_DESCRIPTOR; }
95 
96  @Override
97  public final BTGattDesc clone()
98  { throw new UnsupportedOperationException(); } // FIXME
99 
100  @Override
101  public final DBTGattChar getCharacteristic() { return wbr_characteristic.get(); }
102 
103  @Override
104  public final byte[] getValue() { return cachedValue; }
105 
106  @Override
107  public final byte[] readValue() {
108  final byte[] value = readValueImpl();
109  updateCachedValue(value);
110  return cachedValue;
111  }
112 
113  @Override
114  public final boolean writeValue(final byte[] value) throws BTException {
115  final boolean res = writeValueImpl(value);
116  if( res ) {
117  updateCachedValue(value);
118  }
119  return res;
120  }
121 
122  /**
123  * Characteristic Descriptor Handle
124  * <p>
125  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
126  * </p>
127  */
128  public final short getHandle() { return handle; }
129 
130  @Override
131  public final String toString() {
132  if( !isValid() ) {
133  return "Descriptor" + "\u271D" + "[uuid "+getUUID()+", handle 0x"+Integer.toHexString(handle)+"]";
134  }
135  return toStringImpl();
136  }
137 
138  /* Native method calls: */
139 
140  private native String toStringImpl();
141 
142  private native byte[] readValueImpl();
143 
144  private native boolean writeValueImpl(byte[] argValue) throws BTException;
145 
146  @Override
147  protected native void deleteImpl(long nativeInstance);
148 }
jau.direct_bt.DBTGattDesc.getValue
final byte[] getValue()
Returns the cached value of this descriptor, if any.
Definition: DBTGattDesc.java:104
org.direct_bt
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2020 Gothel Software e.K.
org.direct_bt.BTType.GATT_DESCRIPTOR
GATT_DESCRIPTOR
Definition: BTType.java:31
jau.direct_bt.DBTGattChar
Definition: DBTGattChar.java:42
jau.direct_bt.DBTGattDesc.getUUID
String getUUID()
Get the UUID of this descriptor.
Definition: DBTGattDesc.java:89
jau.direct_bt.DBTGattDesc.equals
boolean equals(final Object obj)
Definition: DBTGattDesc.java:79
jau.direct_bt.DBTGattDesc.writeValue
final boolean writeValue(final byte[] value)
Writes the value of this descriptor.
Definition: DBTGattDesc.java:114
jau.direct_bt.DBTGattDesc.close
synchronized void close()
Release the native memory associated with this object The object should not be used following a call ...
Definition: DBTGattDesc.java:71
jau.direct_bt.DBTGattDesc.clone
final BTGattDesc clone()
Definition: DBTGattDesc.java:97
jau.direct_bt.DBTGattDesc.deleteImpl
native void deleteImpl(long nativeInstance)
Deletes the native instance.
jau.direct_bt.DBTGattDesc.toString
final String toString()
Definition: DBTGattDesc.java:131
jau.direct_bt.DBTGattDesc.getCharacteristic
final DBTGattChar getCharacteristic()
Returns the characteristic to which this descriptor belongs to.
Definition: DBTGattDesc.java:101
org.direct_bt.BTGattDesc
Provides access to Bluetooth GATT descriptor.
Definition: BTGattDesc.java:38
jau.direct_bt.DBTGattDesc.readValue
final byte[] readValue()
Reads the value of this descriptor.
Definition: DBTGattDesc.java:107
org
org.direct_bt.BTType
Definition: BTType.java:28
org.direct_bt.BTException
Definition: BTException.java:32
jau.direct_bt.DBTGattDesc.getHandle
final short getHandle()
Characteristic Descriptor Handle.
Definition: DBTGattDesc.java:128
jau.direct_bt.DBTGattDesc
Definition: DBTGattDesc.java:36
jau.direct_bt.DBTGattDesc.getBluetoothType
BTType getBluetoothType()
Returns the BluetoothType of this object.
Definition: DBTGattDesc.java:92
jau.direct_bt.DBTObject
Definition: DBTObject.java:32