Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
BTGattService.hpp
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 #ifndef BT_GATT_SERVICE_HPP_
27 #define BT_GATT_SERVICE_HPP_
28 
29 #include <cstring>
30 #include <string>
31 #include <memory>
32 #include <cstdint>
33 
34 #include <mutex>
35 #include <atomic>
36 
37 #include <jau/java_uplink.hpp>
38 #include <jau/darray.hpp>
39 
40 #include "UUID.hpp"
41 #include "BTTypes0.hpp"
42 #include "OctetTypes.hpp"
43 #include "ATTPDUTypes.hpp"
44 
45 #include "BTTypes1.hpp"
46 
47 #include "BTGattChar.hpp"
48 
49 /**
50  * - - - - - - - - - - - - - - -
51  *
52  * Module GATTService:
53  *
54  * - BT Core Spec v5.2: Vol 3, Part G Generic Attribute Protocol (GATT)
55  * - BT Core Spec v5.2: Vol 3, Part G GATT: 2.6 GATT Profile Hierarchy
56  */
57 namespace direct_bt {
58 
59  class BTGattHandler; // forward
60  class BTDevice; // forward
61 
62  /**
63  * Representing a complete [Primary] Service Declaration
64  * including its list of Characteristic Declarations,
65  * which also may include its client config if available.
66  */
67  class BTGattService : public BTObject {
68  private:
69  /** Service's GATTHandler weak back-reference */
70  std::weak_ptr<BTGattHandler> wbr_handler;
71 
72  std::string toShortString() const noexcept;
73 
74  public:
75  const bool isPrimary;
76 
77  /**
78  * Service start handle
79  * <p>
80  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
81  * </p>
82  */
83  const uint16_t startHandle;
84 
85  /**
86  * Service end handle
87  * <p>
88  * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
89  * </p>
90  */
91  const uint16_t endHandle;
92 
93  /** Service type UUID */
94  std::unique_ptr<const uuid_t> type;
95 
96  /** List of Characteristic Declarations as shared reference */
98 
99  BTGattService(const std::shared_ptr<BTGattHandler> &handler_, const bool isPrimary_,
100  const uint16_t startHandle_, const uint16_t endHandle_, std::unique_ptr<const uuid_t> && type_) noexcept
101  : wbr_handler(handler_), isPrimary(isPrimary_), startHandle(startHandle_), endHandle(endHandle_), type(std::move(type_)), characteristicList() {
103  }
104 
105  std::string get_java_class() const noexcept override {
106  return java_class();
107  }
108  static std::string java_class() noexcept {
109  return std::string(JAVA_DBT_PACKAGE "DBTGattService");
110  }
111 
112  std::shared_ptr<BTGattHandler> getGattHandlerUnchecked() const noexcept { return wbr_handler.lock(); }
113  std::shared_ptr<BTGattHandler> getGattHandlerChecked() const;
114 
115  std::shared_ptr<BTDevice> getDeviceUnchecked() const noexcept;
116  std::shared_ptr<BTDevice> getDeviceChecked() const;
117 
118  std::string toString() const noexcept override;
119  };
120 
121  inline bool operator==(const BTGattService& lhs, const BTGattService& rhs) noexcept
122  { return lhs.startHandle == rhs.startHandle && lhs.endHandle == rhs.endHandle; /** unique attribute handles */ }
123 
124  inline bool operator!=(const BTGattService& lhs, const BTGattService& rhs) noexcept
125  { return !(lhs == rhs); }
126 
127 } // namespace direct_bt
128 
129 #endif /* BT_GATT_SERVICE_HPP_ */
direct_bt::BTGattService::characteristicList
jau::darray< BTGattCharRef > characteristicList
List of Characteristic Declarations as shared reference.
Definition: BTGattService.hpp:97
darray.hpp
direct_bt::BTGattService::startHandle
const uint16_t startHandle
Service start handle.
Definition: BTGattService.hpp:83
direct_bt
Definition: ATTPDUTypes.hpp:171
BTTypes1.hpp
OctetTypes.hpp
direct_bt::BTGattService
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
Definition: BTGattService.hpp:67
jau::darray< BTGattCharRef >
direct_bt::BTGattService::getGattHandlerChecked
std::shared_ptr< BTGattHandler > getGattHandlerChecked() const
Definition: BTGattService.cpp:46
JAVA_DBT_PACKAGE
#define JAVA_DBT_PACKAGE
Definition: BTTypes1.hpp:39
direct_bt::BTObject
Definition: BTTypes1.hpp:48
direct_bt::BTGattService::get_java_class
std::string get_java_class() const noexcept override
Definition: BTGattService.hpp:105
BTTypes0.hpp
UUID.hpp
direct_bt::BTGattService::type
std::unique_ptr< const uuid_t > type
Service type UUID.
Definition: BTGattService.hpp:94
direct_bt::BTGattService::getDeviceChecked
std::shared_ptr< BTDevice > getDeviceChecked() const
Definition: BTGattService.cpp:62
ATTPDUTypes.hpp
direct_bt::BTGattService::isPrimary
const bool isPrimary
Definition: BTGattService.hpp:75
direct_bt::BTGattService::BTGattService
BTGattService(const std::shared_ptr< BTGattHandler > &handler_, const bool isPrimary_, const uint16_t startHandle_, const uint16_t endHandle_, std::unique_ptr< const uuid_t > &&type_) noexcept
Definition: BTGattService.hpp:99
BTGattChar.hpp
direct_bt::BTGattService::endHandle
const uint16_t endHandle
Service end handle.
Definition: BTGattService.hpp:91
direct_bt::BTGattService::toString
std::string toString() const noexcept override
Definition: BTGattService.cpp:66
direct_bt::BTGattService::getDeviceUnchecked
std::shared_ptr< BTDevice > getDeviceUnchecked() const noexcept
Definition: BTGattService.cpp:54
jau::darray::reserve
void reserve(size_type new_capacity)
Like std::vector::reserve(), increases this instance's capacity to new_capacity.
Definition: darray.hpp:745
direct_bt::operator!=
bool operator!=(const EUI48Sub &lhs, const EUI48Sub &rhs) noexcept
Definition: BTAddress.hpp:275
direct_bt::BTGattService::java_class
static std::string java_class() noexcept
Definition: BTGattService.hpp:108
direct_bt::BTGattService::getGattHandlerUnchecked
std::shared_ptr< BTGattHandler > getGattHandlerUnchecked() const noexcept
Definition: BTGattService.hpp:112
direct_bt::BTDevice
Definition: BTDevice.hpp:57