2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.generic.internal;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Objects;
20 import java.util.UUID;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ScheduledFuture;
23 import java.util.concurrent.TimeUnit;
24 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.bluetooth.BluetoothBindingConstants;
29 import org.openhab.binding.bluetooth.BluetoothCharacteristic;
30 import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState;
31 import org.openhab.binding.bluetooth.ConnectedBluetoothHandler;
32 import org.openhab.core.library.types.StringType;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingStatusDetail;
38 import org.openhab.core.thing.binding.builder.ChannelBuilder;
39 import org.openhab.core.thing.binding.builder.ThingBuilder;
40 import org.openhab.core.thing.type.ChannelTypeUID;
41 import org.openhab.core.types.Command;
42 import org.openhab.core.types.RefreshType;
43 import org.openhab.core.types.State;
44 import org.openhab.core.util.HexUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.sputnikdev.bluetooth.gattparser.BluetoothGattParser;
48 import org.sputnikdev.bluetooth.gattparser.BluetoothGattParserFactory;
49 import org.sputnikdev.bluetooth.gattparser.FieldHolder;
50 import org.sputnikdev.bluetooth.gattparser.GattRequest;
51 import org.sputnikdev.bluetooth.gattparser.GattResponse;
52 import org.sputnikdev.bluetooth.gattparser.spec.Characteristic;
53 import org.sputnikdev.bluetooth.gattparser.spec.Field;
56 * This is a handler for generic connected bluetooth devices that dynamically generates
57 * channels based off of a bluetooth device's GATT characteristics.
59 * @author Connor Petty - Initial contribution
60 * @author Peter Rosenberg - Use notifications
64 public class GenericBluetoothHandler extends ConnectedBluetoothHandler {
66 private final Logger logger = LoggerFactory.getLogger(GenericBluetoothHandler.class);
67 private final Map<BluetoothCharacteristic, CharacteristicHandler> charHandlers = new ConcurrentHashMap<>();
68 private final Map<ChannelUID, CharacteristicHandler> channelHandlers = new ConcurrentHashMap<>();
69 private final BluetoothGattParser gattParser = BluetoothGattParserFactory.getDefault();
70 private final CharacteristicChannelTypeProvider channelTypeProvider;
71 private final Map<CharacteristicHandler, List<ChannelUID>> handlerToChannels = new ConcurrentHashMap<>();
73 private @Nullable ScheduledFuture<?> readCharacteristicJob = null;
75 public GenericBluetoothHandler(Thing thing, CharacteristicChannelTypeProvider channelTypeProvider) {
77 this.channelTypeProvider = channelTypeProvider;
81 public void initialize() {
84 GenericBindingConfiguration config = getConfigAs(GenericBindingConfiguration.class);
85 readCharacteristicJob = scheduler.scheduleWithFixedDelay(() -> {
86 if (device.getConnectionState() == ConnectionState.CONNECTED) {
87 if (device.isServicesDiscovered()) {
88 handlerToChannels.forEach((charHandler, channelUids) -> {
89 // Only read the value manually if notification is not on.
90 // Also read it the first time before we activate notifications below.
91 if (!device.isNotifying(charHandler.characteristic) && charHandler.canRead()) {
92 device.readCharacteristic(charHandler.characteristic);
94 // TODO the ideal solution would be to use locks/conditions and timeouts
95 // Kbetween this code and `onCharacteristicReadComplete` but
96 // that would overcomplicate the code a bit and I plan
97 // on implementing a better more generalized solution later
99 } catch (InterruptedException e) {
103 if (charHandler.characteristic.canNotify()) {
104 // Enabled/Disable notifications dependent on if the channel is linked.
105 // TODO check why isLinked() is true for not linked channels
106 if (channelUids.stream().anyMatch(this::isLinked)) {
107 if (!device.isNotifying(charHandler.characteristic)) {
108 device.enableNotifications(charHandler.characteristic);
111 if (device.isNotifying(charHandler.characteristic)) {
112 device.disableNotifications(charHandler.characteristic);
118 // if we are connected and still haven't been able to resolve the services, try disconnecting and
119 // then connecting again
123 }, 15, config.pollingInterval, TimeUnit.SECONDS);
127 public void dispose() {
128 ScheduledFuture<?> future = readCharacteristicJob;
129 if (future != null) {
134 charHandlers.clear();
135 channelHandlers.clear();
136 handlerToChannels.clear();
140 public void onServicesDiscovered() {
141 super.onServicesDiscovered();
142 logger.trace("Service discovery completed for '{}'", address);
143 updateThingChannels();
147 public void handleCommand(ChannelUID channelUID, Command command) {
148 super.handleCommand(channelUID, command);
150 CharacteristicHandler handler = channelHandlers.get(channelUID);
151 if (handler != null) {
152 handler.handleCommand(channelUID, command);
157 public void onCharacteristicUpdate(BluetoothCharacteristic characteristic, byte[] value) {
158 super.onCharacteristicUpdate(characteristic, value);
159 getCharacteristicHandler(characteristic).handleCharacteristicUpdate(value);
162 private void updateThingChannels() {
163 List<Channel> channels = device.getServices().stream()//
164 .flatMap(service -> service.getCharacteristics().stream())//
165 .flatMap(characteristic -> {
166 logger.trace("{} processing characteristic {}", address, characteristic.getUuid());
167 CharacteristicHandler handler = getCharacteristicHandler(characteristic);
168 List<Channel> chans = handler.buildChannels();
169 List<ChannelUID> chanUids = chans.stream().map(Channel::getUID).collect(Collectors.toList());
170 for (ChannelUID channel : chanUids) {
171 channelHandlers.put(channel, handler);
173 handlerToChannels.put(handler, chanUids);
174 return chans.stream();
176 .collect(Collectors.toList());
178 ThingBuilder builder = editThing();
179 boolean changed = false;
180 for (Channel channel : channels) {
181 logger.trace("{} attempting to add channel {}", address, channel.getLabel());
182 // we only want to add each channel, not replace all of them
183 if (getThing().getChannel(channel.getUID()) == null) {
185 builder.withChannel(channel);
189 updateThing(builder.build());
193 private CharacteristicHandler getCharacteristicHandler(BluetoothCharacteristic characteristic) {
194 return Objects.requireNonNull(charHandlers.computeIfAbsent(characteristic, CharacteristicHandler::new));
197 private void readCharacteristic(BluetoothCharacteristic characteristic) {
198 readCharacteristic(characteristic.getService().getUuid(), characteristic.getUuid()).whenComplete((data, th) -> {
200 logger.warn("Could not read data from characteristic {} of device {}: {}", characteristic.getUuid(),
201 address, th.getMessage());
205 getCharacteristicHandler(characteristic).handleCharacteristicUpdate(data);
210 private void writeCharacteristic(BluetoothCharacteristic characteristic, byte[] data) {
211 writeCharacteristic(characteristic.getService().getUuid(), characteristic.getUuid(), data, false)
212 .whenComplete((r, th) -> {
214 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
215 "Could not write data to characteristic " + characteristic.getUuid() + ": "
221 private class CharacteristicHandler {
223 private BluetoothCharacteristic characteristic;
225 public CharacteristicHandler(BluetoothCharacteristic characteristic) {
226 this.characteristic = characteristic;
229 private String getCharacteristicUUID() {
230 return characteristic.getUuid().toString();
233 public void handleCommand(ChannelUID channelUID, Command command) {
236 if (command == RefreshType.REFRESH) {
238 readCharacteristic(characteristic);
244 if (command instanceof State) {
245 State state = (State) command;
246 String characteristicUUID = getCharacteristicUUID();
248 if (gattParser.isKnownCharacteristic(characteristicUUID)) {
249 String fieldName = getFieldName(channelUID);
250 if (fieldName != null) {
251 updateCharacteristic(fieldName, state);
253 logger.warn("Characteristic has no field name!");
255 } else if (state instanceof StringType) {
256 // unknown characteristic
257 byte[] data = HexUtils.hexToBytes(state.toString());
258 writeCharacteristic(characteristic, data);
260 } catch (RuntimeException ex) {
261 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
262 "Could not update bluetooth device. Error: " + ex.getMessage());
267 private void updateCharacteristic(String fieldName, State state) {
268 // TODO maybe we should check if the characteristic is authenticated?
269 String characteristicUUID = getCharacteristicUUID();
271 if (gattParser.isValidForWrite(characteristicUUID)) {
272 GattRequest request = gattParser.prepare(characteristicUUID);
274 BluetoothChannelUtils.updateHolder(gattParser, request, fieldName, state);
275 byte[] data = gattParser.serialize(request);
277 writeCharacteristic(characteristic, data);
278 } catch (NumberFormatException ex) {
279 logger.warn("Could not parse characteristic value: {} : {}", characteristicUUID, state, ex);
280 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
281 "Could not parse characteristic value: " + characteristicUUID + " : " + state);
286 public void handleCharacteristicUpdate(byte[] data) {
287 String characteristicUUID = getCharacteristicUUID();
288 if (gattParser.isKnownCharacteristic(characteristicUUID)) {
289 GattResponse response = gattParser.parse(characteristicUUID, data);
290 for (FieldHolder holder : response.getFieldHolders()) {
291 Field field = holder.getField();
292 ChannelUID channelUID = getChannelUID(field);
293 updateState(channelUID, BluetoothChannelUtils.convert(gattParser, holder));
296 // this is a raw channel
297 String hex = HexUtils.bytesToHex(data);
298 ChannelUID channelUID = getChannelUID(null);
299 updateState(channelUID, new StringType(hex));
303 public List<Channel> buildChannels() {
304 List<Channel> channels = new ArrayList<>();
305 String charUUID = getCharacteristicUUID();
306 Characteristic gattChar = gattParser.getCharacteristic(charUUID);
307 if (gattChar != null) {
308 List<Field> fields = gattParser.getFields(charUUID);
311 // check if the characteristic has only on field, if so use its name as label
312 if (fields.size() == 1) {
313 label = gattChar.getName();
316 Map<String, List<Field>> fieldsMapping = fields.stream().collect(Collectors.groupingBy(Field::getName));
318 for (List<Field> fieldList : fieldsMapping.values()) {
319 Field field = fieldList.get(0);
320 if (fieldList.size() > 1) {
321 if (field.isFlagField() || field.isOpCodesField()) {
322 logger.debug("Skipping flags/op codes field: {}.", charUUID);
324 logger.warn("Multiple fields with the same name found: {} / {}. Skipping these fields.",
325 charUUID, field.getName());
330 if (isFieldSupported(field)) {
331 Channel channel = buildFieldChannel(field, label, !gattChar.isValidForWrite());
332 if (channel != null) {
333 channels.add(channel);
335 logger.warn("Unable to build channel for field: {}", field.getName());
338 logger.warn("GATT field is not supported: {} / {} / {}", charUUID, field.getName(),
343 channels.add(buildUnknownChannel());
348 private Channel buildUnknownChannel() {
349 ChannelUID channelUID = getChannelUID(null);
350 ChannelTypeUID channelTypeUID = new ChannelTypeUID(BluetoothBindingConstants.BINDING_ID, "char-unknown");
351 return ChannelBuilder.create(channelUID).withType(channelTypeUID).withProperties(getChannelProperties(null))
355 public boolean canRead() {
356 String charUUID = getCharacteristicUUID();
357 if (gattParser.isKnownCharacteristic(charUUID)) {
358 return gattParser.isValidForRead(charUUID);
360 return characteristic.canRead();
363 public boolean canWrite() {
364 String charUUID = getCharacteristicUUID();
365 if (gattParser.isKnownCharacteristic(charUUID)) {
366 return gattParser.isValidForWrite(charUUID);
368 return characteristic.canWrite();
371 private boolean isAdvanced() {
372 return !gattParser.isKnownCharacteristic(getCharacteristicUUID());
375 private boolean isFieldSupported(Field field) {
376 return field.getFormat() != null;
379 private @Nullable Channel buildFieldChannel(Field field, @Nullable String charLabel, boolean readOnly) {
380 String label = charLabel != null ? charLabel : field.getName();
381 String acceptedType = BluetoothChannelUtils.getItemType(field);
382 if (acceptedType == null) {
383 // unknown field format
387 ChannelUID channelUID = getChannelUID(field);
389 logger.debug("Building a new channel for a field: {}", channelUID.getId());
391 ChannelTypeUID channelTypeUID = channelTypeProvider.registerChannelType(getCharacteristicUUID(),
392 isAdvanced(), readOnly, field);
394 return ChannelBuilder.create(channelUID, acceptedType).withType(channelTypeUID)
395 .withProperties(getChannelProperties(field.getName())).withLabel(label).build();
398 private ChannelUID getChannelUID(@Nullable Field field) {
399 StringBuilder builder = new StringBuilder();
400 builder.append("service-")//
401 .append(toBluetoothHandle(characteristic.getService().getUuid()))//
403 .append(toBluetoothHandle(characteristic.getUuid()));
405 builder.append("-").append(BluetoothChannelUtils.encodeFieldName(field.getName()));
407 return new ChannelUID(getThing().getUID(), builder.toString());
410 private String toBluetoothHandle(UUID uuid) {
411 long leastSig = uuid.getLeastSignificantBits();
412 long mostSig = uuid.getMostSignificantBits();
414 if (leastSig == BluetoothBindingConstants.BLUETOOTH_BASE_UUID) {
415 return "0x" + Long.toHexString(mostSig >> 32).toUpperCase();
417 return uuid.toString().toUpperCase();
420 private @Nullable String getFieldName(ChannelUID channelUID) {
421 String channelId = channelUID.getId();
422 int index = channelId.lastIndexOf("-");
424 throw new IllegalArgumentException(
425 "ChannelUID '" + channelUID + "' is not a valid GATT channel format");
427 String encodedFieldName = channelId.substring(index + 1);
428 if (encodedFieldName.isEmpty()) {
431 return BluetoothChannelUtils.decodeFieldName(encodedFieldName);
434 private Map<String, String> getChannelProperties(@Nullable String fieldName) {
435 Map<String, String> properties = new HashMap<>();
436 if (fieldName != null) {
437 properties.put(GenericBindingConstants.PROPERTY_FIELD_NAME, fieldName);
439 properties.put(GenericBindingConstants.PROPERTY_SERVICE_UUID,
440 characteristic.getService().getUuid().toString());
441 properties.put(GenericBindingConstants.PROPERTY_CHARACTERISTIC_UUID, getCharacteristicUUID());