Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
DBTGattService.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.List;
30 
31 import org.direct_bt.BTDevice;
32 import org.direct_bt.BTGattChar;
33 import org.direct_bt.BTGattDesc;
35 import org.direct_bt.BTObject;
36 import org.direct_bt.BTType;
37 
38 public class DBTGattService extends DBTObject implements BTGattService
39 {
40  /** Service's device weak back-reference */
41  final WeakReference<DBTDevice> wbr_device;
42 
43  private final boolean isPrimary;
44  private final String type_uuid;
45  private final short handleStart;
46  private final short handleEnd;
47  /* pp */ final List<BTGattChar> charList;
48 
49  /* pp */ DBTGattService(final long nativeInstance, final DBTDevice device, final boolean isPrimary,
50  final String type_uuid, final short handleStart, final short handleEnd)
51  {
52  super(nativeInstance, compHash(handleStart, handleEnd));
53  this.wbr_device = new WeakReference<DBTDevice>(device);
54  this.isPrimary = isPrimary;
55  this.type_uuid = type_uuid;
56  this.handleStart = handleStart;
57  this.handleEnd = handleEnd;
58  this.charList = getCharsImpl();
59  }
60 
61  @Override
62  public boolean equals(final Object obj)
63  {
64  if (obj == null || !(obj instanceof DBTGattService)) {
65  return false;
66  }
67  final DBTGattService other = (DBTGattService)obj;
68  return handleStart == other.handleStart && handleEnd == other.handleEnd; /** unique attribute handles */
69  }
70 
71  @Override
72  public String getUUID() { return type_uuid; }
73 
74  @Override
75  public BTType getBluetoothType() { return class_type(); }
76 
77  static BTType class_type() { return BTType.GATT_SERVICE; }
78 
79  @Override
80  public final BTGattService clone()
81  { throw new UnsupportedOperationException(); } // FIXME
82 
83  @Override
84  public BTGattChar find(final String UUID, final long timeoutMS) {
85  if( !checkServiceCache() ) {
86  return null;
87  }
88  return (DBTGattChar) findInCache(UUID, BTType.GATT_CHARACTERISTIC);
89  }
90 
91  @Override
92  public BTGattChar find(final String UUID) {
93  return find(UUID, 0);
94  }
95 
96  @Override
97  public final BTDevice getDevice() { return wbr_device.get(); }
98 
99  @Override
100  public final boolean getPrimary() { return isPrimary; }
101 
102  @Override
103  public final List<BTGattChar> getChars() { return charList; }
104 
105  /**
106  * Returns the service start handle.
107  * <p>
108  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
109  * </p>
110  */
111  public final short getHandleStart() { return handleStart; }
112 
113  /**
114  * Returns the service end handle.
115  * <p>
116  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
117  * </p>
118  */
119  public final short getHandleEnd() { return handleEnd; }
120 
121  @Override
122  public final String toString() {
123  if( !isValid() ) {
124  return "Service" + "\u271D" + "[uuid "+getUUID()+", handles [0x"+Integer.toHexString(handleStart)+".."+Integer.toHexString(handleEnd)+"]]";
125  }
126  return toStringImpl();
127  }
128 
129  /* Native method calls: */
130 
131  private native String toStringImpl();
132 
133  private native List<BTGattChar> getCharsImpl();
134 
135  @Override
136  protected native void deleteImpl(long nativeInstance);
137 
138  /* local functionality */
139 
140  /* pp */ boolean checkServiceCache() {
141  final DBTDevice device = wbr_device.get();
142  return null != device && device.checkServiceCache(false);
143  }
144 
145  /**
146  * Returns the matching {@link DBTObject} from the internal cache if found,
147  * otherwise {@code null}.
148  * <p>
149  * The returned {@link DBTObject} may be of type
150  * <ul>
151  * <li>{@link DBTGattChar}</li>
152  * <li>{@link DBTGattDesc}</li>
153  * </ul>
154  * or alternatively in {@link BTObject} space
155  * <ul>
156  * <li>{@link BTType#GATT_CHARACTERISTIC} -> {@link BTGattChar}</li>
157  * <li>{@link BTType#GATT_DESCRIPTOR} -> {@link BTGattDesc}</li>
158  * </ul>
159  * </p>
160  * @param uuid UUID of the desired
161  * {@link BTType#GATT_CHARACTERISTIC characteristic} or {@link BTType#GATT_DESCRIPTOR descriptor} to be found.
162  * Maybe {@code null}, in which case the first object of the desired type is being returned - if existing.
163  * @param type specify the type of the object to be found, either
164  * {@link BTType#GATT_CHARACTERISTIC characteristic}
165  * or {@link BTType#GATT_DESCRIPTOR descriptor}.
166  * {@link BTType#NONE none} means anything.
167  */
168  /* pp */ DBTObject findInCache(final String uuid, final BTType type) {
169  final boolean anyType = BTType.NONE == type;
170  final boolean charType = BTType.GATT_CHARACTERISTIC== type;
171  final boolean descType = BTType.GATT_DESCRIPTOR == type;
172 
173  if( !anyType && !charType && !descType ) {
174  return null;
175  }
176  final int characteristicSize = charList.size();
177  for(int charIdx = 0; charIdx < characteristicSize; charIdx++ ) {
178  final DBTGattChar characteristic = (DBTGattChar) charList.get(charIdx);
179  if( ( anyType || charType ) && ( null == uuid || characteristic.getUUID().equals(uuid) ) ) {
180  return characteristic;
181  }
182  if( anyType || descType ) {
183  final DBTObject dbtObj = characteristic.findInCache(uuid, type);
184  if( null != dbtObj ) {
185  return dbtObj;
186  }
187  }
188  }
189  return null;
190  }
191 
192 }
jau.direct_bt.DBTObject.DBTObject
DBTObject(final long nativeInstance, final int hashValue)
Definition: DBTObject.java:46
jau.direct_bt.DBTGattService
Definition: DBTGattService.java:39
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
org.direct_bt.BTType.GATT_CHARACTERISTIC
GATT_CHARACTERISTIC
Definition: BTType.java:30
jau.direct_bt.DBTGattService.toString
final String toString()
Definition: DBTGattService.java:122
jau.direct_bt.DBTGattService.getChars
final List< BTGattChar > getChars()
Returns a list of BTGattChar this service exposes.
Definition: DBTGattService.java:103
jau.direct_bt.DBTGattService.getPrimary
final boolean getPrimary()
Returns true if this service is a primary service, false if secondary.
Definition: DBTGattService.java:100
jau.direct_bt.DBTGattService.deleteImpl
native void deleteImpl(long nativeInstance)
Deletes the native instance.
jau.direct_bt.DBTGattService.find
BTGattChar find(final String UUID)
Find a BTGattChar.
Definition: DBTGattService.java:92
jau.direct_bt.DBTGattChar
Definition: DBTGattChar.java:42
org.direct_bt.BTGattService
Provides access to Bluetooth GATT characteristic.
Definition: BTGattService.java:41
jau.direct_bt.DBTDevice
Definition: DBTDevice.java:58
org.direct_bt.BTType.GATT_SERVICE
GATT_SERVICE
Definition: BTType.java:30
jau.direct_bt.DBTGattService.clone
final BTGattService clone()
Definition: DBTGattService.java:80
jau.direct_bt.DBTGattService.getHandleEnd
final short getHandleEnd()
Returns the service end handle.
Definition: DBTGattService.java:119
jau.direct_bt.DBTGattService.equals
boolean equals(final Object obj)
Definition: DBTGattService.java:62
jau.direct_bt.DBTGattService.find
BTGattChar find(final String UUID, final long timeoutMS)
Find a BTGattChar.
Definition: DBTGattService.java:84
org.direct_bt.BTGattChar
Provides access to Bluetooth GATT characteristic.
Definition: BTGattChar.java:40
jau.direct_bt.DBTGattService.getHandleStart
final short getHandleStart()
Returns the service start handle.
Definition: DBTGattService.java:111
org.direct_bt.BTDevice
Provides access to Bluetooth adapters.
Definition: BTDevice.java:42
org.direct_bt.BTType.NONE
NONE
Definition: BTType.java:29
org.direct_bt.BTGattDesc
Provides access to Bluetooth GATT descriptor.
Definition: BTGattDesc.java:38
jau.direct_bt.DBTGattService.getDevice
final BTDevice getDevice()
Returns the device to which this service belongs to.
Definition: DBTGattService.java:97
org
org.direct_bt.BTType
Definition: BTType.java:28
jau.direct_bt.DBTGattService.getBluetoothType
BTType getBluetoothType()
Returns the BluetoothType of this object.
Definition: DBTGattService.java:75
org.direct_bt.BTObject
Definition: BTObject.java:31
jau.direct_bt.DBTGattService.getUUID
String getUUID()
Get the UUID of this service.
Definition: DBTGattService.java:72
jau.direct_bt.DBTObject
Definition: DBTObject.java:32