Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTGattService.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 "GattNumbers.hpp"
38
39#include "BTDevice.hpp"
40#include "BTGattHandler.hpp"
41#include "BTGattService.hpp"
42
43using namespace direct_bt;
44using namespace jau;
45
46BTGattHandlerRef BTGattService::getGattHandlerChecked() const {
47 std::shared_ptr<BTGattHandler> ref = wbr_handler.lock();
48 if( nullptr == ref ) {
49 throw IllegalStateException("GATTService's GATTHandler already destructed: "+toShortString(), E_FILE_LINE);
50 }
51 return ref;
52}
53
55 std::shared_ptr<BTGattHandler> h = getGattHandlerUnchecked();
56 if( nullptr != h ) {
57 return h->getDeviceUnchecked();
58 }
59 return nullptr;
60}
61
63 return getGattHandlerChecked()->getDeviceChecked();
64}
65
67 for(BTGattCharRef& c : characteristicList) {
68 if( nullptr != c && char_uuid.equivalent( *(c->value_type) ) ) {
69 return c;
70 }
71 }
72 return nullptr;
73}
74
75BTGattCharRef BTGattService::findGattChar(const BTGattChar& characteristic) noexcept {
76 for(BTGattCharRef& c : characteristicList) {
77 if( nullptr != c && characteristic == *c ) {
78 return c;
79 }
80 }
81 return nullptr;
82}
83
84std::string BTGattService::toString() const noexcept {
85 std::string name = "";
86 if( uuid_t::TypeSize::UUID16_SZ == type->getTypeSize() ) {
87 const uint16_t uuid16 = (static_cast<const uuid16_t*>(type.get()))->value;
88 name = " - "+GattServiceTypeToString(static_cast<GattServiceType>(uuid16));
89 }
90 return "Srvc[type 0x"+type->toString()+", handle ["+to_hexstring(handle)+".."+to_hexstring(end_handle)+"]"+
91 name+", "+std::to_string(characteristicList.size())+" chars]";
92}
93
94std::string BTGattService::toShortString() const noexcept {
95 std::string name = "";
96 if( uuid_t::TypeSize::UUID16_SZ == type->getTypeSize() ) {
97 const uint16_t uuid16 = (static_cast<const uuid16_t*>(type.get()))->value;
98 name = " - "+GattServiceTypeToString(static_cast<GattServiceType>(uuid16));
99 }
100 return "Srvc[handle ["+to_hexstring(handle)+".."+to_hexstring(end_handle)+"]"+
101 name+", "+std::to_string(characteristicList.size())+" characteristics]";
102}
#define E_FILE_LINE
Representing a Gatt Characteristic object from the GATTRole::Client perspective.
Definition: BTGattChar.hpp:94
BTGattCharRef findGattChar(const jau::uuid_t &char_uuid) noexcept
Find a BTGattChar by its char_uuid.
const uint16_t end_handle
Service end handle, inclusive.
std::unique_ptr< const jau::uuid_t > type
Service type UUID.
BTDeviceRef getDeviceUnchecked() const noexcept
const uint16_t handle
Service start handle.
std::string toString() const noexcept override
BTGattHandlerRef getGattHandlerChecked() const
jau::darray< BTGattCharRef > characteristicList
List of Characteristic Declarations as shared reference.
BTGattHandlerRef getGattHandlerUnchecked() const noexcept
BTDeviceRef getDeviceChecked() const
constexpr size_type size() const noexcept
Like std::vector::size().
Definition: darray.hpp:763
std::string to_string(const alphabet &v) noexcept
Definition: base_codec.hpp:97
std::shared_ptr< BTGattHandler > BTGattHandlerRef
Definition: BTGattChar.hpp:61
std::shared_ptr< BTDevice > BTDeviceRef
Definition: BTDevice.hpp:1347
std::string GattServiceTypeToString(const GattServiceType v) noexcept
GattServiceType
GATT Service Type, each encapsulating a set of Characteristics.
Definition: GattNumbers.hpp:84
std::shared_ptr< BTGattChar > BTGattCharRef
Definition: BTGattChar.hpp:410
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32