Direct-BT  2.3.1
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 
43 using namespace direct_bt;
44 using namespace jau;
45 
46 std::shared_ptr<BTGattHandler> 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 
54 std::shared_ptr<BTDevice> BTGattService::getDeviceUnchecked() const noexcept {
55  std::shared_ptr<BTGattHandler> h = getGattHandlerUnchecked();
56  if( nullptr != h ) {
57  return h->getDeviceUnchecked();
58  }
59  return nullptr;
60 }
61 
62 std::shared_ptr<BTDevice> BTGattService::getDeviceChecked() const {
63  return getGattHandlerChecked()->getDeviceChecked();
64 }
65 
66 std::string BTGattService::toString() const noexcept {
67  std::string name = "";
68  if( uuid_t::TypeSize::UUID16_SZ == type->getTypeSize() ) {
69  const uint16_t uuid16 = (static_cast<const uuid16_t*>(type.get()))->value;
70  name = " - "+GattServiceTypeToString(static_cast<GattServiceType>(uuid16));
71  }
72  return "[type 0x"+type->toString()+", handle ["+to_hexstring(startHandle)+".."+to_hexstring(endHandle)+"]"+
73  name+", "+std::to_string(characteristicList.size())+" characteristics]";
74 }
75 
76 std::string BTGattService::toShortString() const noexcept {
77  std::string name = "";
78  if( uuid_t::TypeSize::UUID16_SZ == type->getTypeSize() ) {
79  const uint16_t uuid16 = (static_cast<const uuid16_t*>(type.get()))->value;
80  name = " - "+GattServiceTypeToString(static_cast<GattServiceType>(uuid16));
81  }
82  return "[handle ["+to_hexstring(startHandle)+".."+to_hexstring(endHandle)+"]"+
83  name+", "+std::to_string(characteristicList.size())+" characteristics]";
84 }
direct_bt::BTGattHandler::getDeviceUnchecked
std::shared_ptr< BTDevice > getDeviceUnchecked() const noexcept
Definition: BTGattHandler.hpp:250
BTGattHandler.hpp
direct_bt::GattServiceType
GattServiceType
GATT Service Type, each encapsulating a set of Characteristics.
Definition: GattNumbers.hpp:64
direct_bt
Definition: ATTPDUTypes.hpp:171
BTGattService.hpp
direct_bt::uuid16_t
Definition: UUID.hpp:98
jau
Definition: basic_algos.hpp:34
jau::to_string
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
Definition: byte_util.hpp:198
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
direct_bt::BTGattService::getGattHandlerChecked
std::shared_ptr< BTGattHandler > getGattHandlerChecked() const
Definition: BTGattService.cpp:46
direct_bt::BTGattService::getDeviceChecked
std::shared_ptr< BTDevice > getDeviceChecked() const
Definition: BTGattService.cpp:62
debug.hpp
jau::IllegalStateException
Definition: basic_types.hpp:117
jau::to_hexstring
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
Definition: string_util.hpp:104
direct_bt::BTGattService::toString
std::string toString() const noexcept override
Definition: BTGattService.cpp:66
GattNumbers.hpp
direct_bt::uuid_t::TypeSize::UUID16_SZ
@ UUID16_SZ
direct_bt::BTGattService::getDeviceUnchecked
std::shared_ptr< BTDevice > getDeviceUnchecked() const noexcept
Definition: BTGattService.cpp:54
direct_bt::GattServiceTypeToString
std::string GattServiceTypeToString(const GattServiceType v) noexcept
Definition: GATTNumbers.cpp:207
BTDevice.hpp