aboutsummaryrefslogtreecommitdiffstats
path: root/api/direct_bt/BTGattDesc.hpp
blob: 516c3f41baf5a580275092e06a3b5fda158df8e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef BT_GATT_DESCRIPTOR_HPP_
#define BT_GATT_DESCRIPTOR_HPP_

#include <jau/octets.hpp>
#include <jau/uuid.hpp>
#include <cstring>
#include <string>
#include <memory>
#include <cstdint>

#include <mutex>
#include <atomic>

#include "BTTypes0.hpp"
#include "ATTPDUTypes.hpp"

#include "BTTypes1.hpp"

/**
 * - - - - - - - - - - - - - - -
 *
 * Module GATTDescriptor:
 *
 * - BT Core Spec v5.2: Vol 3, Part G Generic Attribute Protocol (GATT)
 * - BT Core Spec v5.2: Vol 3, Part G GATT: 2.6 GATT Profile Hierarchy
 */
namespace direct_bt {

    class BTDevice; // forward
    class BTGattHandler; // forward
    class BTGattChar; // forward
    typedef std::shared_ptr<BTGattChar> BTGattCharRef;

    /**
     * Representing a Gatt Characteristic Descriptor object from the ::GATTRole::Client perspective.
     *
     * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
     */
    class BTGattDesc : public BTObject {
        private:
            /** Descriptor's characteristic weak back-reference */
            std::weak_ptr<BTGattChar> wbr_char;

            std::string toShortString() const noexcept;

        public:
            static const std::shared_ptr<jau::uuid_t> TYPE_EXT_PROP;
            static const std::shared_ptr<jau::uuid_t> TYPE_USER_DESC;
            static const std::shared_ptr<jau::uuid_t> TYPE_CCC_DESC;

            /**
             * Following UUID16 GATT profile attribute types are listed under:
             * BT Core Spec v5.2: Vol 3, Part G GATT: 3.4 Summary of GATT Profile Attribute Types
             *
             * See GattAttributeType for further non BTGattDesc related declarations.
             */
            enum Type : uint16_t {
                /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.1 Characteristic Extended Properties */
                CHARACTERISTIC_EXTENDED_PROPERTIES          = 0x2900,
                /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description (Characteristic Descriptor, optional, single, string) */
                CHARACTERISTIC_USER_DESCRIPTION             = 0x2901,
                /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
                CLIENT_CHARACTERISTIC_CONFIGURATION         = 0x2902,
                /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.4 Server Characteristic Configuration (Characteristic Descriptor, optional, single, bitfield) */
                SERVER_CHARACTERISTIC_CONFIGURATION         = 0x2903,
                /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.5 Characteristic Presentation Format (Characteristic Descriptor, optional, single, complex) */
                CHARACTERISTIC_PRESENTATION_FORMAT          = 0x2904,
                CHARACTERISTIC_AGGREGATE_FORMAT             = 0x2905,

                /** Our identifier to mark a custom vendor Characteristic Descriptor */
                CUSTOM_CHARACTERISTIC_DESCRIPTION           = 0x8888
            };

            /** Type of descriptor */
            std::unique_ptr<const jau::uuid_t> type;

            /**
             * Characteristic Descriptor Handle
             * <p>
             * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
             * </p>
             */
            const uint16_t handle;

            /* Characteristics Descriptor's Value */
            jau::POctets value;

            BTGattDesc(const BTGattCharRef & characteristic, std::unique_ptr<const jau::uuid_t> && type_,
                           const uint16_t handle_) noexcept
            : wbr_char(characteristic), type(std::move(type_)), handle(handle_),
              value(jau::endian::little /* intentional zero sized */)
            { }

            std::string get_java_class() const noexcept override {
                return java_class();
            }
            static std::string java_class() noexcept {
                return std::string(JAVA_DBT_PACKAGE "DBTGattDesc");
            }

            std::shared_ptr<BTGattChar> getGattCharUnchecked() const noexcept { return wbr_char.lock(); }
            std::shared_ptr<BTGattChar> getGattCharChecked() const;
            std::shared_ptr<BTGattHandler> getGattHandlerChecked() const;
            std::shared_ptr<BTDevice> getDeviceChecked() const;

            std::string toString() const noexcept override;

            /** Value is uint16_t bitfield */
            bool isExtendedProperties() const noexcept { return *TYPE_EXT_PROP == *type; }

            /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration (Characteristic Descriptor, optional, single, uint16_t bitfield) */
            bool isClientCharConfig() const noexcept{ return *TYPE_CCC_DESC == *type; }

            /* BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.2 Characteristic User Description */
            bool isUserDescription() const noexcept{ return *TYPE_USER_DESC == *type; }

            /**
             * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor
             * <p>
             * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.2 Read Long Characteristic Descriptor
             * </p>
             * <p>
             * If expectedLength = 0, then only one ATT_READ_REQ/RSP will be used.
             * </p>
             * <p>
             * If expectedLength < 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used until
             * the response returns zero. This is the default parameter.
             * </p>
             * <p>
             * If expectedLength > 0, then long values using multiple ATT_READ_BLOB_REQ/RSP will be used
             * if required until the response returns zero.
             * </p>
             * <p>
             * Convenience delegation call to BTGattHandler via BTDevice
             * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown.
             * </p>
             */
            bool readValue(int expectedLength=-1);

            /**
             * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors
             * <p>
             * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor
             * </p>
             * <p>
             * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration
             * </p>
             * <p>
             * Convenience delegation call to BTGattHandler via BTDevice
             * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown.
             * </p>
             */
            bool writeValue();
    };
    typedef std::shared_ptr<BTGattDesc> BTGattDescRef;

    inline bool operator==(const BTGattDesc& lhs, const BTGattDesc& rhs) noexcept
    { return lhs.handle == rhs.handle; /** unique attribute handles */ }

    inline bool operator!=(const BTGattDesc& lhs, const BTGattDesc& rhs) noexcept
    { return !(lhs == rhs); }

} // namespace direct_bt

#endif /* BT_GATT_DESCRIPTOR_HPP_ */