Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBGattDesc.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
28/**
29 * Representing a Gatt Characteristic Descriptor object from the GATT server perspective.
30 *
31 * A list of shared DBGattChar instances are passed at DBGattChar construction
32 * and are retrievable via DBGattChar::getDescriptors().
33 *
34 * See [Direct-BT Overview](namespaceorg_1_1direct__bt.html#details).
35 *
36 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
37 *
38 * @since 2.4.0
39 */
40public final class DBGattDesc implements AutoCloseable
41{
42 private volatile long nativeInstance;
43 /* pp */ long getNativeInstance() { return nativeInstance; }
44
45 /**
46 * Selected standard GATT descriptor numbers in UUID16 format as defined.
47 */
48 public static class UUID16 {
49 /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.1 Characteristic Extended Properties */
50 public static final String EXT_PROP = "2900";
51 /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description (Characteristic Descriptor, optional, single, string) */
52 public static final String USER_DESC = "2901";
53 /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
54 public static final String CCC_DESC = "2902";
55 }
56
57 /**
58 * Characteristic Descriptor Handle
59 * <p>
60 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
61 * </p>
62 */
63 public native short getHandle();
64
65 private final String type;
66
67 /** Type of descriptor UUID (lower-case) */
68 public String getType() { return type; }
69
70 /**
71 * Return a copy of this characteristic descriptor's native {@link DBGattValue} value.
72 *
73 * Its capacity defines the maximum writable variable length
74 * and its size defines the maximum writable fixed length.
75 */
76 public native DBGattValue getValue();
77
78 /**
79 * Set this characteristic descriptor's native value.
80 *
81 * Methods won't exceed the value's capacity if it is of hasVariableLength()
82 * or the value's size otherwise.
83 *
84 * @param source data to be written to this value
85 * @param source_len length of the source data to be written
86 * @param dest_pos position where the source data shall be written to the value
87 * @return true if successful, otherwise false for exceeding the value's limits or passing invalid parameter.
88 */
89 public native boolean setValue(final byte[] source, final int source_pos, final int source_len, final int dest_pos);
90
91 /**
92 *
93 * The value's {@link DBGattValue#hasVariableLength()} is forced to false if {@link #isExtendedProperties()} or {@link #isClientCharConfig()}.
94 * @param type_
95 * @param value_
96 */
97 public DBGattDesc(final String type_, final DBGattValue value_)
98 {
99 type = type_;
100
101 if( value_.hasVariableLength() && ( isExtendedProperties() || isClientCharConfig() ) ) {
102 value_.setVariableLength(false);
103 }
104 nativeInstance = ctorImpl(type_, value_.data(), value_.capacity(), value_.hasVariableLength());
105 }
106 private native long ctorImpl(final String type,
107 final byte[] value, final int capacity, boolean variable_length);
108
109 @Override
110 public void close() {
111 final long handle;
112 synchronized( this ) {
113 handle = nativeInstance;
114 nativeInstance = 0;
115 }
116 if( 0 != handle ) {
117 dtorImpl(handle);
118 }
119 }
120 private static native void dtorImpl(final long nativeInstance);
121
122 @Override
123 public void finalize() {
124 close();
125 }
126
127 /** Fill value with zero bytes. */
128 public native void bzero();
129
130 /**
131 * Return a newly constructed Client Characteristic Configuration
132 * with a zero uint16_t value of fixed length.
133 * @see isClientCharConfig()
134 */
136 final byte[] p = { (byte)0, (byte)0 };
137 return new DBGattDesc( UUID16.CCC_DESC, new DBGattValue(p, p.length, false /* variable_length */) );
138 }
139
140 /** Value is uint16_t bitfield */
141 public boolean isExtendedProperties() { return UUID16.EXT_PROP.equals(type); }
142
143 /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
144 public boolean isClientCharConfig() { return UUID16.CCC_DESC.equals(type); }
145
146 /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description */
147 public boolean isUserDescription() { return UUID16.USER_DESC.equals(type); }
148
149 @Override
150 public boolean equals(final Object other) {
151 if( this == other ) {
152 return true;
153 }
154 if( !(other instanceof DBGattDesc) ) {
155 return false;
156 }
157 final DBGattDesc o = (DBGattDesc)other;
158 return getHandle() == o.getHandle(); /** unique attribute handles */
159 }
160
161 @Override
162 public native String toString();
163}
Selected standard GATT descriptor numbers in UUID16 format as defined.
Definition: DBGattDesc.java:48
static final String CCC_DESC
Definition: DBGattDesc.java:54
static final String USER_DESC
Definition: DBGattDesc.java:52
static final String EXT_PROP
Definition: DBGattDesc.java:50
Representing a Gatt Characteristic Descriptor object from the GATT server perspective.
Definition: DBGattDesc.java:41
native String toString()
native boolean setValue(final byte[] source, final int source_pos, final int source_len, final int dest_pos)
Set this characteristic descriptor's native value.
static DBGattDesc createClientCharConfig()
Return a newly constructed Client Characteristic Configuration with a zero uint16_t value of fixed le...
native DBGattValue getValue()
Return a copy of this characteristic descriptor's native DBGattValue value.
native short getHandle()
Characteristic Descriptor Handle.
String getType()
Type of descriptor UUID (lower-case)
Definition: DBGattDesc.java:68
DBGattDesc(final String type_, final DBGattValue value_)
The value's DBGattValue#hasVariableLength() is forced to false if isExtendedProperties() or isClientC...
Definition: DBGattDesc.java:97
boolean equals(final Object other)
boolean isExtendedProperties()
Value is uint16_t bitfield.
native void bzero()
Fill value with zero bytes.
A copy of the native GATT value of DBGattChar or DBGattDesc.
int capacity()
Return the set capacity for this value.
void setVariableLength(final boolean v)
byte[] data()
Returns the actual data of this value.
boolean hasVariableLength()
Returns true if this value has variable length.