Direct-BT v3.3.0-1-gc2d430c
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#include <jau/octets.hpp>
40#include <jau/uuid.hpp>
41
42#include "BTTypes0.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 */
57namespace direct_bt {
58
59 class BTGattHandler; // forward
60 typedef std::shared_ptr<BTGattHandler> BTGattHandlerRef;
61
62 class BTDevice; // forward
63 typedef std::shared_ptr<BTDevice> BTDeviceRef;
64
65 /** \addtogroup DBTUserClientAPI
66 *
67 * @{
68 */
69
70 /**
71 * Representing a Gatt Service object from the ::GATTRole::Client perspective.
72 *
73 * A list of shared BTGattService instances can be retrieved from BTDevice
74 * after successful connection and optional pairing via BTDevice::getGattServices().
75 *
76 * See [Direct-BT Overview](namespacedirect__bt.html#details).
77 *
78 * BT Core Spec v5.2: Vol 3, Part G GATT: 3.1 Service Definition
79 *
80 * Includes a complete [Primary] Service Declaration
81 * including its list of Characteristic Declarations,
82 * which also may include its client config if available.
83 */
84 class BTGattService : public BTObject {
85 private:
86 /** Service's GATTHandler weak back-reference */
87 std::weak_ptr<BTGattHandler> wbr_handler;
88
89 std::string toShortString() const noexcept;
90
91 public:
92 const bool primary;
93
94 /**
95 * Service start handle
96 * <p>
97 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
98 * </p>
99 */
100 const uint16_t handle;
101
102 /**
103 * Service end handle, inclusive.
104 * <p>
105 * Attribute handles are unique for each device (server) (BT Core Spec v5.2: Vol 3, Part F Protocol..: 3.2.2 Attribute Handle).
106 * </p>
107 */
108 const uint16_t end_handle;
109
110 /** Service type UUID */
111 std::unique_ptr<const jau::uuid_t> type;
112
113 /** List of Characteristic Declarations as shared reference */
115
116 BTGattService(const std::shared_ptr<BTGattHandler> &handler_, const bool isPrimary_,
117 const uint16_t startHandle_, const uint16_t endHandle_, std::unique_ptr<const jau::uuid_t> && type_) noexcept
118 : wbr_handler(handler_), primary(isPrimary_), handle(startHandle_), end_handle(endHandle_), type(std::move(type_)), characteristicList() {
120 }
121
122 std::string get_java_class() const noexcept override {
123 return java_class();
124 }
125 static std::string java_class() noexcept {
126 return std::string(JAVA_DBT_PACKAGE "DBTGattService");
127 }
128
129 BTGattHandlerRef getGattHandlerUnchecked() const noexcept { return wbr_handler.lock(); }
131
132 BTDeviceRef getDeviceUnchecked() const noexcept;
134
135 /**
136 * Find a BTGattChar by its char_uuid.
137 *
138 * @parameter char_uuid the jau::uuid_t of the desired BTGattChar, within this BTGattService.
139 * @return The matching characteristic or null if not found
140 */
141 BTGattCharRef findGattChar(const jau::uuid_t& char_uuid) noexcept;
142
143 /**
144 * Find a BTGattChar by itself, i.e. mapping BTGattChar instance to BTGattCharRef.
145 *
146 * @parameter characteristic the desired BTGattChar, within this BTGattService.
147 * @return The matching characteristic or null if not found
148 */
149 BTGattCharRef findGattChar(const BTGattChar& characteristic) noexcept;
150
151 std::string toString() const noexcept override;
152 };
153 typedef std::shared_ptr<BTGattService> BTGattServiceRef;
154
155 inline bool operator==(const BTGattService& lhs, const BTGattService& rhs) noexcept
156 { return lhs.handle == rhs.handle && lhs.end_handle == rhs.end_handle; /** unique attribute handles */ }
157
158 inline bool operator!=(const BTGattService& lhs, const BTGattService& rhs) noexcept
159 { return !(lhs == rhs); }
160
161 /**@}*/
162
163} // namespace direct_bt
164
165#endif /* BT_GATT_SERVICE_HPP_ */
#define JAVA_DBT_PACKAGE
Definition: BTTypes0.hpp:43
Representing a Gatt Characteristic object from the GATTRole::Client perspective.
Definition: BTGattChar.hpp:94
Representing a Gatt Service object from the GATTRole::Client perspective.
BTGattCharRef findGattChar(const jau::uuid_t &char_uuid) noexcept
Find a BTGattChar by its char_uuid.
BTGattService(const std::shared_ptr< BTGattHandler > &handler_, const bool isPrimary_, const uint16_t startHandle_, const uint16_t endHandle_, std::unique_ptr< const jau::uuid_t > &&type_) noexcept
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.
static std::string java_class() noexcept
BTGattHandlerRef getGattHandlerUnchecked() const noexcept
std::string get_java_class() const noexcept override
BTDeviceRef getDeviceChecked() const
void reserve(size_type new_capacity)
Like std::vector::reserve(), increases this instance's capacity to new_capacity.
Definition: darray.hpp:840
std::shared_ptr< BTGattHandler > BTGattHandlerRef
Definition: BTGattChar.hpp:61
std::shared_ptr< BTDevice > BTDeviceRef
Definition: BTDevice.hpp:1347
bool operator!=(const BTAdapter &lhs, const BTAdapter &rhs) noexcept
Definition: BTAdapter.hpp:1351
std::shared_ptr< BTGattChar > BTGattCharRef
Definition: BTGattChar.hpp:410
std::shared_ptr< BTGattService > BTGattServiceRef
Definition: BTGattChar.hpp:67
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition: backtrace.hpp:32
STL namespace.