aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt/GATTCharacteristic.cpp
blob: 4e2002046ab05f2f25960259f7413bd22a0ef6e7 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * 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.
 */

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <vector>
#include <cstdio>

#include  <algorithm>

#include <jau/debug.hpp>

#include "GATTCharacteristic.hpp"
#include "GATTHandler.hpp"
#include "DBTDevice.hpp"

using namespace direct_bt;
using namespace jau;

#define CHAR_DECL_PROPS_ENUM(X) \
        X(Broadcast,broadcast) \
        X(Read,read) \
        X(WriteNoAck,write-without-response) \
        X(WriteWithAck,write) \
        X(Notify,notify) \
        X(Indicate,indicate) \
        X(AuthSignedWrite,authenticated-signed-writes) \
        X(ExtProps,extended-properties)

/**
        "reliable-write"
        "writable-auxiliaries"
        "encrypt-read"
        "encrypt-write"
        "encrypt-authenticated-read"
        "encrypt-authenticated-write"
        "secure-read" (Server only)
        "secure-write" (Server only)
        "authorize"
 */

#define CASE_TO_STRING2(V,S) case V: return #S;

std::string GATTCharacteristic::getPropertyString(const PropertyBitVal prop) noexcept {
    switch(prop) {
        CHAR_DECL_PROPS_ENUM(CASE_TO_STRING2)
        default: ; // fall through intended
    }
    return "Unknown property";
}

std::string GATTCharacteristic::getPropertiesString(const PropertyBitVal properties) noexcept {
    const PropertyBitVal none = static_cast<PropertyBitVal>(0);
    const uint8_t one = 1;
    bool has_pre = false;
    std::string out("[");
    for(int i=0; i<8; i++) {
        const PropertyBitVal propertyBit = static_cast<PropertyBitVal>( one << i );
        if( none != ( properties & propertyBit ) ) {
            if( has_pre ) { out.append(", "); }
            out.append(getPropertyString(propertyBit));
            has_pre = true;
        }
    }
    out.append("]");
    return out;
}

jau::darray<std::unique_ptr<std::string>> GATTCharacteristic::getPropertiesStringList(const PropertyBitVal properties) noexcept {
    jau::darray<std::unique_ptr<std::string>> out;
    const PropertyBitVal none = static_cast<PropertyBitVal>(0);
    const uint8_t one = 1;
    for(int i=0; i<8; i++) {
        const PropertyBitVal propertyBit = static_cast<PropertyBitVal>( one << i );
        if( none != ( properties & propertyBit ) ) {
            out.push_back( std::unique_ptr<std::string>( new std::string( getPropertyString(propertyBit) ) ) );
        }
    }
    return out;
}

std::string GATTCharacteristic::toString() const noexcept {
    uint16_t service_handle_end = 0xffff;
    GATTServiceRef serviceRef = getServiceUnchecked();
    std::string service_uuid_str = "";
    std::string service_name = "";
    std::string char_name = "";
    std::string desc_str = ", descr[ ";

    if( nullptr != serviceRef ) {
        std::unique_ptr<const uuid_t> & service_uuid = serviceRef->type;
        service_uuid_str = service_uuid->toString();
        service_handle_end = serviceRef->endHandle;

        if( uuid_t::TypeSize::UUID16_SZ == service_uuid->getTypeSize() ) {
            const uint16_t uuid16 = (static_cast<const uuid16_t*>(service_uuid.get()))->value;
            service_name = ", "+GattServiceTypeToString(static_cast<GattServiceType>(uuid16));
        }
    }
    if( uuid_t::TypeSize::UUID16_SZ == value_type->getTypeSize() ) {
        const uint16_t uuid16 = (static_cast<const uuid16_t*>(value_type.get()))->value;
        char_name = ", "+GattCharacteristicTypeToString(static_cast<GattCharacteristicType>(uuid16));
    }
    for(size_t i=0; i<descriptorList.size(); i++) {
        const GATTDescriptorRef cd = descriptorList[i];
        desc_str += cd->toString() + ", ";
    }
    desc_str += " ]";
    return "[handle "+uint16HexString(handle)+", props "+uint8HexString(properties)+" "+getPropertiesString()+
           ", value[type 0x"+value_type->toString()+", handle "+uint16HexString(value_handle)+char_name+desc_str+
           "], service[type 0x"+service_uuid_str+
           ", handle[ "+uint16HexString(service_handle)+".."+uint16HexString(service_handle_end)+" ]"+
           service_name+", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"] ] ]";
}

std::string GATTCharacteristic::toShortString() const noexcept {
    std::string char_name = "";

    if( uuid_t::TypeSize::UUID16_SZ == value_type->getTypeSize() ) {
        const uint16_t uuid16 = (static_cast<const uuid16_t*>(value_type.get()))->value;
        char_name = ", "+GattCharacteristicTypeToString(static_cast<GattCharacteristicType>(uuid16));
    }
    return "[handle "+uint16HexString(handle)+", props "+uint8HexString(properties)+" "+getPropertiesString()+
           ", value[handle "+uint16HexString(value_handle)+char_name+
           "], service["+
           ", handle[ "+uint16HexString(service_handle)+".. ]"+
           ", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"] ] ]";
}

std::shared_ptr<GATTService> GATTCharacteristic::getServiceChecked() const {
    std::shared_ptr<GATTService> ref = wbr_service.lock();
    if( nullptr == ref ) {
        throw IllegalStateException("GATTCharacteristic's service already destructed: "+toShortString(), E_FILE_LINE);
    }
    return ref;
}

std::shared_ptr<GATTHandler> GATTCharacteristic::getGATTHandlerUnchecked() const noexcept {
    std::shared_ptr<GATTService> s = getServiceUnchecked();
    if( nullptr != s ) {
        return s->getGATTHandlerUnchecked();
    }
    return nullptr;
}

std::shared_ptr<GATTHandler> GATTCharacteristic::getGATTHandlerChecked() const {
    return getServiceChecked()->getGATTHandlerChecked();
}

std::shared_ptr<DBTDevice> GATTCharacteristic::getDeviceUnchecked() const noexcept {
    std::shared_ptr<GATTService> s = getServiceUnchecked();
    if( nullptr != s ) {
        return s->getDeviceUnchecked();
    }
    return nullptr;
}

std::shared_ptr<DBTDevice> GATTCharacteristic::getDeviceChecked() const {
    return getServiceChecked()->getDeviceChecked();
}

bool GATTCharacteristic::configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]) {
    enabledState[0] = false;
    enabledState[1] = false;

    const bool hasEnableNotification = hasProperties(GATTCharacteristic::PropertyBitVal::Notify);
    const bool hasEnableIndication = hasProperties(GATTCharacteristic::PropertyBitVal::Indicate);
    if( !hasEnableNotification && !hasEnableIndication ) {
        DBG_PRINT("Characteristic has neither Notify nor Indicate property present: %s", toString().c_str());
        return false;
    }

    std::shared_ptr<DBTDevice> device = getDeviceUnchecked();
    std::shared_ptr<GATTHandler> gatt = nullptr != device ? device->getGATTHandler() : nullptr;
    if( nullptr == gatt ) {
        if( !enableNotification && !enableIndication ) {
            // OK to have GATTHandler being shutdown @ disable
            DBG_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str());
            return false;
        }
        throw IllegalStateException("Characteristic's device GATTHandle not connected: "+
                toString(), E_FILE_LINE);
    }
    const bool resEnableNotification = hasEnableNotification && enableNotification;
    const bool resEnableIndication = hasEnableIndication && enableIndication;

    if( resEnableNotification == enabledNotifyState &&
        resEnableIndication == enabledIndicateState )
    {
        enabledState[0] = resEnableNotification;
        enabledState[1] = resEnableIndication;
        DBG_PRINT("GATTCharacteristic::configNotificationIndication: Unchanged: notification[shall %d, has %d: %d == %d], indication[shall %d, has %d: %d == %d]",
                enableNotification, hasEnableNotification, enabledNotifyState, resEnableNotification,
                enableIndication, hasEnableIndication, enabledIndicateState, resEnableIndication);
        return true;
    }

    GATTDescriptorRef cccd = this->getClientCharacteristicConfig();
    if( nullptr == cccd ) {
        DBG_PRINT("Characteristic has no ClientCharacteristicConfig descriptor: %s", toString().c_str());
        return false;
    }
    bool res = gatt->configNotificationIndication(*cccd, resEnableNotification, resEnableIndication);
    DBG_PRINT("GATTCharacteristic::configNotificationIndication: res %d, notification[shall %d, has %d: %d -> %d], indication[shall %d, has %d: %d -> %d]",
            res,
            enableNotification, hasEnableNotification, enabledNotifyState, resEnableNotification,
            enableIndication, hasEnableIndication, enabledIndicateState, resEnableIndication);
    if( res ) {
        enabledNotifyState = resEnableNotification;
        enabledIndicateState = resEnableIndication;
        enabledState[0] = resEnableNotification;
        enabledState[1] = resEnableIndication;
    }
    return res;
}

bool GATTCharacteristic::enableNotificationOrIndication(bool enabledState[2]) {
    const bool hasEnableNotification = hasProperties(GATTCharacteristic::PropertyBitVal::Notify);
    const bool hasEnableIndication = hasProperties(GATTCharacteristic::PropertyBitVal::Indicate);

    const bool enableNotification = hasEnableNotification;
    const bool enableIndication = !enableNotification && hasEnableIndication;

    return configNotificationIndication(enableNotification, enableIndication, enabledState);
}

bool GATTCharacteristic::addCharacteristicListener(std::shared_ptr<GATTCharacteristicListener> l) {
    return getDeviceChecked()->addCharacteristicListener(l);
}

bool GATTCharacteristic::addCharacteristicListener(std::shared_ptr<GATTCharacteristicListener> l, bool enabledState[2]) {
    if( !enableNotificationOrIndication(enabledState) ) {
        return false;
    }
    return addCharacteristicListener(l);
}

bool GATTCharacteristic::removeCharacteristicListener(std::shared_ptr<GATTCharacteristicListener> l, bool disableIndicationNotification) {
    if( disableIndicationNotification ) {
        bool enabledState[2];
        configNotificationIndication(false, false, enabledState);
    }
    return getDeviceChecked()->removeCharacteristicListener(l);
}

int GATTCharacteristic::removeAllCharacteristicListener(bool disableIndicationNotification) {
    if( disableIndicationNotification ) {
        bool enabledState[2];
        configNotificationIndication(false, false, enabledState);
    }
    return getDeviceChecked()->removeAllCharacteristicListener();
}

bool GATTCharacteristic::readValue(POctets & res, int expectedLength) {
    std::shared_ptr<DBTDevice> device = getDeviceChecked();
    std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
    if( nullptr == gatt ) {
        throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE);
    }
    return gatt->readCharacteristicValue(*this, res, expectedLength);
}
/**
 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
 */
bool GATTCharacteristic::writeValue(const TROOctets & value) {
    std::shared_ptr<DBTDevice> device = getDeviceChecked();
    std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
    if( nullptr == gatt ) {
        throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE);
    }
    return gatt->writeCharacteristicValue(*this, value);
}

/**
 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
 */
bool GATTCharacteristic::writeValueNoResp(const TROOctets & value) {
    std::shared_ptr<DBTDevice> device = getDeviceChecked();
    std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
    if( nullptr == gatt ) {
        throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE);
    }
    return gatt->writeCharacteristicValueNoResp(*this, value);
}