aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt/BTGattChar.cpp
blob: f33c4f80aed17aee28a1840fe61bf5f508b6c23c (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * 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/dfa_utf8_decode.hpp>
#include <jau/debug.hpp>

#include "BTDevice.hpp"
#include "BTGattService.hpp"
#include "BTGattChar.hpp"
#include "BTGattHandler.hpp"

using namespace direct_bt;
using namespace jau;

/**
 * Simple access and provision of a typename string representation
 * at compile time like RTTI via jau::type_name_cue.
 */
JAU_TYPENAME_CUE(direct_bt::BTGattCharListener)

/**
 * Simple access and provision of a typename string representation
 * at compile time like RTTI via jau::type_name_cue.
 */
JAU_TYPENAME_CUE(direct_bt::AssociatedBTGattCharListener)

const char * BTGattCharListener::type_name() const noexcept { return jau::type_name_cue<BTGattCharListener>::name(); }

const char * AssociatedBTGattCharListener::type_name() const noexcept { return jau::type_name_cue<AssociatedBTGattCharListener>::name(); }


#define CHAR_DECL_PROPS_ENUM(X) \
        X(BTGattChar,Broadcast,broadcast) \
        X(BTGattChar,Read,read) \
        X(BTGattChar,WriteNoAck,write-noack) \
        X(BTGattChar,WriteWithAck,write-ack) \
        X(BTGattChar,Notify,notify) \
        X(BTGattChar,Indicate,indicate) \
        X(BTGattChar,AuthSignedWrite,authenticated-signed-writes) \
        X(BTGattChar,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 CASE2_TO_STRING2(U,V,W) case U::V: return #W;

static std::string _getPropertyBitValStr(const BTGattChar::PropertyBitVal prop) noexcept {
    switch(prop) {
        CHAR_DECL_PROPS_ENUM(CASE2_TO_STRING2)
        default: ; // fall through intended
    }
    return "Unknown property";
}

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

std::shared_ptr<BTGattDesc> BTGattChar::findGattDesc(const jau::uuid_t& desc_uuid) noexcept {
    const size_t descriptors_size = descriptorList.size();
    for(size_t j = 0; j < descriptors_size; j++) {
        direct_bt::BTGattDescRef descriptor = descriptorList[j];
        if( nullptr != descriptor && desc_uuid == *(descriptor->type) ) {
            return descriptor;
        }
    }
    return nullptr;
}

std::string BTGattChar::toString() const noexcept {
    std::string char_name = "";
    std::string desc_str;

    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));
    }
    {
        BTGattDescRef ud = getUserDescription();
        if( nullptr != ud ) {
            char_name.append( ", '" + dfa_utf8_decode( ud->value.get_ptr(), ud->value.size() ) + "'");
        }
    }
    if( 0 < descriptorList.size() ) {
        bool comma = false;
        desc_str = ", descr[";
        for(size_t i=0; i<descriptorList.size(); i++) {
            const BTGattDescRef cd = descriptorList[i];
            if( comma ) {
                desc_str += ", ";
            }
            desc_str += "handle "+to_hexstring(cd->handle);
            comma = true;
        }
        desc_str += "]";
    }

    std::string notify_str;
    if( hasProperties(BTGattChar::PropertyBitVal::Notify) || hasProperties(BTGattChar::PropertyBitVal::Indicate) ) {
        notify_str = ", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"]";
    }
    return "Char[handle "+to_hexstring(handle)+", props "+to_hexstring(properties)+" "+to_string(properties)+
            char_name+desc_str+", ccd-idx "+std::to_string(clientCharConfigIndex)+notify_str+
           ", value[type 0x"+value_type->toString()+", handle "+to_hexstring(value_handle)+
           "]]";
}

std::string BTGattChar::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));
    }
    {
        BTGattDescRef ud = getUserDescription();
        if( nullptr != ud ) {
            char_name.append( ", '" + dfa_utf8_decode( ud->value.get_ptr(), ud->value.size() ) + "'");
        }
    }
    std::string notify_str;
    if( hasProperties(BTGattChar::PropertyBitVal::Notify) || hasProperties(BTGattChar::PropertyBitVal::Indicate) ) {
        notify_str = ", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"]";
    }
    return "Char[handle "+to_hexstring(handle)+", props "+to_hexstring(properties)+" "+to_string(properties)+
            char_name+", value[handle "+to_hexstring(value_handle)+
           "], ccd-idx "+std::to_string(clientCharConfigIndex)+notify_str+"]";
}

std::shared_ptr<BTGattHandler> BTGattChar::getGattHandlerUnchecked() const noexcept {
    std::shared_ptr<BTGattService> s = getServiceUnchecked();
    if( nullptr != s ) {
        return s->getGattHandlerUnchecked();
    }
    return nullptr;
}

std::shared_ptr<BTDevice> BTGattChar::getDeviceUnchecked() const noexcept {
    std::shared_ptr<BTGattService> s = getServiceUnchecked();
    if( nullptr != s ) {
        return s->getDeviceUnchecked();
    }
    return nullptr;
}

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

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

    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    std::shared_ptr<BTGattHandler> 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());
        } else {
            ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str());
        }
        return false;
    }
    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;
    }

    BTGattDescRef cccd = this->getClientCharConfig();
    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 BTGattChar::enableNotificationOrIndication(bool enabledState[2]) noexcept {
    const bool hasEnableNotification = hasProperties(BTGattChar::PropertyBitVal::Notify);
    const bool hasEnableIndication = hasProperties(BTGattChar::PropertyBitVal::Indicate);

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

    return configNotificationIndication(enableNotification, enableIndication, enabledState);
}

bool BTGattChar::disableIndicationNotification() noexcept {
    bool enabledState[2];
    return configNotificationIndication(false, false, enabledState);
}

class DelegatedBTGattCharListener : public BTGattCharListener {
    private:
        const BTGattChar * associatedChar;
        std::shared_ptr<BTGattChar::Listener> delegate;

    public:
        DelegatedBTGattCharListener(const BTGattChar * characteristicMatch, std::shared_ptr<BTGattChar::Listener> l) noexcept
        : associatedChar(characteristicMatch), delegate(l) { }

        const char * type_name() const noexcept override;

        bool match(const BTGattChar & characteristic) noexcept override {
            if( nullptr == associatedChar ) {
                return true;
            }
            return *associatedChar == characteristic;
        }

        void notificationReceived(BTGattCharRef charDecl,
                                  const TROOctets& charValue, const uint64_t timestamp) override {
            delegate->notificationReceived(charDecl, charValue, timestamp);
        }

        void indicationReceived(BTGattCharRef charDecl,
                                const TROOctets& charValue, const uint64_t timestamp,
                                const bool confirmationSent) override {
            delegate->indicationReceived(charDecl, charValue, timestamp, confirmationSent);
        }

        std::string toString() override {
            return std::string(type_name())+"["+jau::to_string(this)+", delegate "+jau::to_string(delegate.get())+"]";
        }

        /**
         * Comparison operator merely testing for same memory reference of the delegate.
         */
        bool operator==(const BTGattCharListener& rhs) const noexcept override;

        bool operator!=(const DelegatedBTGattCharListener& rhs) const noexcept
        { return !(*this == rhs); }
};

/**
 * Simple access and provision of a typename string representation
 * at compile time like RTTI via jau::type_name_cue.
 */
JAU_TYPENAME_CUE(DelegatedBTGattCharListener)

const char * DelegatedBTGattCharListener::type_name() const noexcept { return jau::type_name_cue<DelegatedBTGattCharListener>::name(); }

bool DelegatedBTGattCharListener::operator==(const BTGattCharListener& rhs) const noexcept
{
    if( 0 != strcmp(rhs.type_name(), jau::type_name_cue<DelegatedBTGattCharListener>::name()) ) {
        return false;
    }
    const DelegatedBTGattCharListener& rhs2 = static_cast<const DelegatedBTGattCharListener&>(rhs);
    return delegate.get() == rhs2.delegate.get();
}

bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l) noexcept {
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return false;
    }
    return device->addCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) );
}

bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l, bool enabledState[2]) noexcept {
    if( !enableNotificationOrIndication(enabledState) ) {
        return false;
    }
    return addCharListener(l);
}

bool BTGattChar::removeCharListener(std::shared_ptr<Listener> l) noexcept {
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return false;
    }
    return device->removeCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) );
}

int BTGattChar::removeAllAssociatedCharListener(bool shallDisableIndicationNotification) noexcept {
    if( shallDisableIndicationNotification ) {
        disableIndicationNotification();
    }
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return 0;
    }
    return device->removeAllAssociatedCharListener(this);
}

bool BTGattChar::readValue(POctets & res, int expectedLength) noexcept {
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return false;
    }
    std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
    if( nullptr == gatt ) {
        ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str());
        return false;
    }
    return gatt->readCharacteristicValue(*this, res, expectedLength);
}
/**
 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
 */
bool BTGattChar::writeValue(const TROOctets & value) noexcept {
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return false;
    }
    std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
    if( nullptr == gatt ) {
        ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str());
        return false;
    }
    return gatt->writeCharacteristicValue(*this, value);
}

/**
 * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
 */
bool BTGattChar::writeValueNoResp(const TROOctets & value) noexcept {
    std::shared_ptr<BTDevice> device = getDeviceUnchecked();
    if( nullptr == device ) {
        ERR_PRINT("Characteristic's device null: %s", toShortString().c_str());
        return false;
    }
    std::shared_ptr<BTGattHandler> gatt = device->getGattHandler();
    if( nullptr == gatt ) {
        ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str());
        return false;
    }
    return gatt->writeCharacteristicValueNoResp(*this, value);
}