Direct-BT v3.3.0-1-gc2d430c
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
41using namespace direct_bt;
42
43const std::shared_ptr<jau::uuid_t> BTGattDesc::TYPE_EXT_PROP( std::make_shared<jau::uuid16_t>(Type::CHARACTERISTIC_EXTENDED_PROPERTIES) );
44const std::shared_ptr<jau::uuid_t> BTGattDesc::TYPE_USER_DESC( std::make_shared<jau::uuid16_t>(Type::CHARACTERISTIC_USER_DESCRIPTION) );
45const std::shared_ptr<jau::uuid_t> BTGattDesc::TYPE_CCC_DESC( std::make_shared<jau::uuid16_t>(Type::CLIENT_CHARACTERISTIC_CONFIGURATION) );
46
47std::shared_ptr<BTGattChar> BTGattDesc::getGattCharChecked() const {
48 std::shared_ptr<BTGattChar> ref = wbr_char.lock();
49 if( nullptr == ref ) {
50 throw jau::IllegalStateException("GATTDescriptor's characteristic already destructed: "+toShortString(), E_FILE_LINE);
51 }
52 return ref;
53}
54
55std::shared_ptr<BTGattHandler> BTGattDesc::getGattHandlerUnchecked() const noexcept {
56 std::shared_ptr<BTGattChar> c = getGattCharUnchecked();
57 if( nullptr != c ) {
58 return c->getGattHandlerUnchecked();
59 }
60 return nullptr;
61}
62
63std::shared_ptr<BTDevice> BTGattDesc::getDeviceUnchecked() const noexcept {
64 std::shared_ptr<BTGattChar> c = getGattCharUnchecked();
65 if( nullptr != c ) {
66 return c->getDeviceUnchecked();
67 }
68 return nullptr;
69}
70
71bool BTGattDesc::readValue(int expectedLength) noexcept {
72 std::shared_ptr<BTDevice> device = getDeviceUnchecked();
73 if( nullptr == device ) {
74 ERR_PRINT("Descriptor's device null: %s", toShortString().c_str());
75 return false;
76 }
77 std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
78 if( nullptr == gatt ) {
79 ERR_PRINT("Descriptor's device GATTHandle not connected: %s", toShortString().c_str());
80 return false;
81 }
82 return gatt->readDescriptorValue(*this, expectedLength);
83}
84
85bool BTGattDesc::writeValue() noexcept {
86 std::shared_ptr<BTDevice> device = getDeviceUnchecked();
87 if( nullptr == device ) {
88 ERR_PRINT("Descriptor's device null: %s", toShortString().c_str());
89 return false;
90 }
91 std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
92 if( nullptr == gatt ) {
93 ERR_PRINT("Descriptor's device GATTHandle not connected: %s", toShortString().c_str());
94 return false;
95 }
96 return gatt->writeDescriptorValue(*this);
97}
98
99std::string BTGattDesc::toString() const noexcept {
100 return "Desc[type 0x"+type->toString()+", handle "+jau::to_hexstring(handle)+
101 ", value["+value.toString()+
102 " '" + jau::dfa_utf8_decode( value.get_ptr(), value.size() ) + "'"+
103 "]]";
104}
105
106std::string BTGattDesc::toShortString() const noexcept {
107 return "Desc[handle "+jau::to_hexstring(handle)+", value["+value.toString()+"]]";
108}
#define E_FILE_LINE
bool writeValue() noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
Definition: BTGattDesc.cpp:85
const uint16_t handle
Characteristic Descriptor Handle.
Definition: BTGattDesc.hpp:118
std::shared_ptr< BTGattChar > getGattCharUnchecked() const noexcept
Definition: BTGattDesc.hpp:136
std::unique_ptr< const jau::uuid_t > type
Type of descriptor.
Definition: BTGattDesc.hpp:110
bool readValue(int expectedLength=-1) noexcept
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
Definition: BTGattDesc.cpp:71
std::string toString() const noexcept override
Definition: BTGattDesc.cpp:99
std::shared_ptr< BTGattHandler > getGattHandlerUnchecked() const noexcept
Definition: BTGattDesc.cpp:55
std::shared_ptr< BTDevice > getDeviceUnchecked() const noexcept
Definition: BTGattDesc.cpp:63
std::string toString() const
Definition: octets.hpp:932
constexpr nsize_t size() const noexcept
Returns the used memory size for read and write operations, may be zero.
Definition: octets.hpp:162
constexpr uint8_t const * get_ptr() const noexcept
Definition: octets.hpp:272
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
Definition: debug.hpp:109
uint32_t dfa_utf8_decode(uint32_t &state, uint32_t &codep, const uint32_t byte_value)
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.