Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTGattDesc.cpp
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 #include <cstring>
27 #include <string>
28 #include <memory>
29 #include <cstdint>
30 #include <vector>
31 #include <cstdio>
32 
33 #include <algorithm>
34 
35 #include <jau/debug.hpp>
36 
37 #include "BTDevice.hpp"
38 #include "BTGattDesc.hpp"
39 #include "BTGattHandler.hpp"
40 
41 using namespace direct_bt;
42 using namespace jau;
43 
47 
48 std::shared_ptr<BTGattChar> BTGattDesc::getGattCharChecked() const {
49  std::shared_ptr<BTGattChar> ref = wbr_char.lock();
50  if( nullptr == ref ) {
51  throw IllegalStateException("GATTDescriptor's characteristic already destructed: "+toShortString(), E_FILE_LINE);
52  }
53  return ref;
54 }
55 
56 std::shared_ptr<BTGattHandler> BTGattDesc::getGattHandlerChecked() const {
57  return getGattCharChecked()->getGattHandlerChecked();
58 }
59 
60 std::shared_ptr<BTDevice> BTGattDesc::getDeviceChecked() const {
61  return getGattCharChecked()->getDeviceChecked();
62 }
63 
64 bool BTGattDesc::readValue(int expectedLength) {
65  std::shared_ptr<BTDevice> device = getDeviceChecked();
66  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
67  if( nullptr == gatt ) {
68  throw IllegalStateException("Descriptor's device GATTHandle not connected: "+toShortString(), E_FILE_LINE);
69  }
70  return gatt->readDescriptorValue(*this, expectedLength);
71 }
72 
74  std::shared_ptr<BTDevice> device = getDeviceChecked();
75  std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
76  if( nullptr == gatt ) {
77  throw IllegalStateException("Descriptor's device GATTHandle not connected: "+toShortString(), E_FILE_LINE);
78  }
79  return gatt->writeDescriptorValue(*this);
80 }
81 
82 std::string BTGattDesc::toString() const noexcept {
83  return "[type 0x"+type->toString()+", handle "+to_hexstring(handle)+", value["+value.toString()+"] ]";
84 }
85 
86 std::string BTGattDesc::toShortString() const noexcept {
87  return "[handle "+to_hexstring(handle)+", value["+value.toString()+"] ]";
88 }
direct_bt::BTGattHandler::writeDescriptorValue
bool writeDescriptorValue(const BTGattDesc &cd)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
Definition: BTGattHandler.cpp:835
BTGattHandler.hpp
direct_bt::BTGattDesc::TYPE_EXT_PROP
static const uuid16_t TYPE_EXT_PROP
Definition: BTGattDesc.hpp:70
direct_bt::CHARACTERISTIC_EXTENDED_PROPERTIES
@ CHARACTERISTIC_EXTENDED_PROPERTIES
Definition: GattTypes.hpp:78
direct_bt::BTGattHandler::readDescriptorValue
bool readDescriptorValue(BTGattDesc &cd, int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
Definition: BTGattHandler.cpp:743
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTGattDesc::toString
virtual std::string toString() const noexcept override
Definition: BTGattDesc.cpp:82
direct_bt::CHARACTERISTIC_USER_DESCRIPTION
@ CHARACTERISTIC_USER_DESCRIPTION
Definition: GattTypes.hpp:80
direct_bt::BTGattDesc::writeValue
bool writeValue()
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
Definition: BTGattDesc.cpp:73
direct_bt::BTGattDesc::getGattCharChecked
std::shared_ptr< BTGattChar > getGattCharChecked() const
Definition: BTGattDesc.cpp:48
direct_bt::uuid16_t
Definition: UUID.hpp:98
jau
Definition: basic_algos.hpp:34
direct_bt::BTGattDesc::TYPE_USER_DESC
static const uuid16_t TYPE_USER_DESC
Definition: BTGattDesc.hpp:71
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
direct_bt::BTGattDesc::readValue
bool readValue(int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
Definition: BTGattDesc.cpp:64
debug.hpp
direct_bt::BTGattDesc::TYPE_CCC_DESC
static const uuid16_t TYPE_CCC_DESC
Definition: BTGattDesc.hpp:72
BTGattDesc.hpp
jau::IllegalStateException
Definition: basic_types.hpp:117
direct_bt::BTGattDesc::getDeviceChecked
std::shared_ptr< BTDevice > getDeviceChecked() const
Definition: BTGattDesc.cpp:60
direct_bt::CLIENT_CHARACTERISTIC_CONFIGURATION
@ CLIENT_CHARACTERISTIC_CONFIGURATION
Definition: GattTypes.hpp:82
jau::to_hexstring
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
Definition: string_util.hpp:104
direct_bt::BTDevice::getGattHandler
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
Definition: BTDevice.cpp:1520
direct_bt::BTGattDesc::getGattHandlerChecked
std::shared_ptr< BTGattHandler > getGattHandlerChecked() const
Definition: BTGattDesc.cpp:56
BTDevice.hpp