1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include <vector>
17 
18 #include "pw_bluetooth_sapphire/internal/host/att/attribute.h"
19 #include "pw_bluetooth_sapphire/internal/host/common/uuid.h"
20 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt_defs.h"
21 
22 namespace bt::gatt {
23 
24 // TODO(armansito): Rename this file to "local_types.h" and add the Local*
25 // prefix to the types here.
26 
27 class Characteristic;
28 using CharacteristicPtr = std::unique_ptr<Characteristic>;
29 
30 // Represents a single remote or local GATT service. A Service object simply
31 // represents the composition/structure of a GATT service, such as its type,
32 // characteristics, includes, etc and is not intended to carry service state.
33 class Service final {
34  public:
35   Service(bool primary, const UUID& type);
36   ~Service() = default;
37 
primary()38   bool primary() const { return primary_; }
type()39   const UUID& type() const { return type_; }
40 
41   // The list of characteristics that have been added to this service.
characteristics()42   const std::vector<CharacteristicPtr>& characteristics() const {
43     return characteristics_;
44   }
45 
46   // Passes the ownership of this service's characteristics to the caller.
ReleaseCharacteristics()47   std::vector<CharacteristicPtr> ReleaseCharacteristics() {
48     return std::move(characteristics_);
49   }
50 
51   // Adds the given characteristic to this service.
AddCharacteristic(CharacteristicPtr && chr)52   inline void AddCharacteristic(CharacteristicPtr&& chr) {
53     characteristics_.push_back(std::forward<CharacteristicPtr>(chr));
54   }
55 
56   // TODO(armansito): Support included services.
57 
58  private:
59   bool primary_;
60   UUID type_;
61   std::vector<CharacteristicPtr> characteristics_;
62 
63   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(Service);
64 };
65 
66 using ServicePtr = std::unique_ptr<Service>;
67 
68 class Descriptor;
69 using DescriptorPtr = std::unique_ptr<Descriptor>;
70 
71 // Represents a single remote or local GATT characteristic. This represents the
72 // composition/structure of a characteristic and is not intended to carry state.
73 class Characteristic final {
74  public:
75   Characteristic(IdType id,
76                  const UUID& type,
77                  uint8_t properties,
78                  uint16_t extended_properties,
79                  const att::AccessRequirements& read_permissions,
80                  const att::AccessRequirements& write_permissions,
81                  const att::AccessRequirements& update_permissions);
82   ~Characteristic() = default;
83 
id()84   IdType id() const { return id_; }
type()85   const UUID& type() const { return type_; }
properties()86   uint8_t properties() const { return properties_; }
extended_properties()87   uint16_t extended_properties() const { return extended_properties_; }
88 
read_permissions()89   const att::AccessRequirements& read_permissions() const {
90     return read_permissions_;
91   }
92 
write_permissions()93   const att::AccessRequirements& write_permissions() const {
94     return write_permissions_;
95   }
96 
update_permissions()97   const att::AccessRequirements& update_permissions() const {
98     return update_permissions_;
99   }
100 
descriptors()101   const std::vector<DescriptorPtr>& descriptors() const { return descriptors_; }
102 
103   // Passes the ownership of this characteristic's descriptors to the caller.
ReleaseDescriptors()104   std::vector<DescriptorPtr> ReleaseDescriptors() {
105     return std::move(descriptors_);
106   }
107 
AddDescriptor(DescriptorPtr && desc)108   inline void AddDescriptor(DescriptorPtr&& desc) {
109     descriptors_.push_back(std::forward<DescriptorPtr>(desc));
110   }
111 
112  private:
113   IdType id_;
114   UUID type_;
115   uint8_t properties_;
116   uint16_t extended_properties_;
117   att::AccessRequirements read_permissions_;
118   att::AccessRequirements write_permissions_;
119   att::AccessRequirements update_permissions_;
120   std::vector<DescriptorPtr> descriptors_;
121 
122   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(Characteristic);
123 };
124 
125 // Represents a single remote or local GATT characteristic descriptor. This
126 // represents the composition/structure of a characteristic and is not intended
127 // to carry state.
128 class Descriptor final {
129  public:
130   Descriptor(IdType id,
131              const UUID& type,
132              const att::AccessRequirements& read_permissions,
133              const att::AccessRequirements& write_permissions);
134   ~Descriptor() = default;
135 
id()136   IdType id() const { return id_; }
type()137   const UUID& type() const { return type_; }
138 
read_permissions()139   const att::AccessRequirements& read_permissions() const {
140     return read_permissions_;
141   }
142 
write_permissions()143   const att::AccessRequirements& write_permissions() const {
144     return write_permissions_;
145   }
146 
147  private:
148   IdType id_;
149   UUID type_;
150   att::AccessRequirements read_permissions_;
151   att::AccessRequirements write_permissions_;
152 
153   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(Descriptor);
154 };
155 
156 }  // namespace bt::gatt
157