1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  *
23  * @since 6
24  *
25  */
26 
27 /**
28  * @file bluetooth_gatt_service.h
29  *
30  * @brief gatt service interface.
31  *
32  * @since 6
33  *
34  */
35 
36 #ifndef BLUETOOTH_GATT_SERVICE_H
37 #define BLUETOOTH_GATT_SERVICE_H
38 
39 #include "bluetooth_gatt_characteristic.h"
40 
41 namespace OHOS {
42 namespace Bluetooth {
43 /** A GATT-based Service type.
44  *  Define GATT-based Service types.
45  */
46 enum class GattServiceType : int {
47     PRIMARY,   /**< primary service */
48     SECONDARY, /**< secondary service */
49 };
50 
51 /**
52  * @brief Class for Gatt Service API.
53  *
54  * @since 6
55  *
56  */
57 class BLUETOOTH_API GattService {
58 public:
59     /**
60      * @brief The function to delete constructor of GattService.
61      *
62      * @since 6
63      *
64      */
65     GattService() = delete;
66     /**
67      * @brief A constructor of GattService.
68      *
69      * @param uuid UUID of service.
70      * @param type Type of service.
71      * @since 6
72      *
73      */
74     GattService(const UUID &uuid, const GattServiceType type);
75     /**
76      * @brief A constructor of GattService.
77      *
78      * @param uuid UUID of service.
79      * @param handle Handle of service.
80      * @param endHandle EndHandle of service.
81      * @param type Type of service.
82      * @since 6
83      *
84      */
85     GattService(const UUID &uuid, uint16_t handle, uint16_t endHandle, const GattServiceType type);
86 
87     /**
88      * @brief The function to add characteristic.
89      *
90      * @param characteristic Characteristic object to add.
91      * @since 6
92      *
93      */
94     void AddCharacteristic(const GattCharacteristic &characteristic);
95     /**
96      * @brief The function to add include service.
97      *
98      * @param characteristic Service object to add.
99      * @since 6
100      *
101      */
102     void AddService(GattService &service);
103     /**
104      * @brief The function to get characteristic by UUID.
105      *
106      * @param uuid UUID of characteristic.
107      * @return characteristic.
108      * @since 6
109      *
110      */
111     GattCharacteristic *GetCharacteristic(const UUID &uuid);
112     /**
113      * @brief The function to get all characteristics.
114      *
115      * @return list of characteristics.
116      * @since 6
117      *
118      */
119     std::vector<GattCharacteristic> &GetCharacteristics();
120     /**
121      * @brief The function to get include services.
122      *
123      * @return list of include services.
124      * @since 6
125      *
126      */
127     const std::vector<std::reference_wrapper<GattService>> &GetIncludedServices();
128     /**
129      * @brief The function to get service's handle.
130      *
131      * @return handle.
132      * @since 6
133      *
134      */
135     uint16_t GetHandle() const;
136     /**
137      * @brief The function to get service's type.
138      *
139      * @return bool   primary or not.
140      * @since 6
141      *
142      */
143     bool IsPrimary() const;
144     /**
145      * @brief The function to get service's UUID.
146      *
147      * @return UUID.
148      * @since 6
149      *
150      */
151     const UUID &GetUuid() const;
152 
153     GattService(const GattService &);
154     GattService &operator=(const GattService &) = default;
155 
156     GattService(GattService &&);
157     GattService &operator=(GattService &&) = default;
158 
159 private:
160     /**
161      * @brief The handle of service.
162      *
163      * @since 6
164      *
165      */
166     uint16_t handle_;
167     /**
168      * @brief The endHandle of service.
169      *
170      * @since 6
171      *
172      */
173     uint16_t endHandle_;
174     /**
175      * @brief The type of service.
176      *
177      * @since 6
178      *
179      */
180     GattServiceType serviceType_;
181     /**
182      * @brief The list of current service's include services.
183      *
184      * @since 6
185      *
186      */
187     std::vector<std::reference_wrapper<GattService>> includeServices_;
188     /**
189      * @brief The characteristics of service.
190      *
191      * @since 6
192      *
193      */
194     std::vector<GattCharacteristic> characteristics_;
195     /**
196      * @brief The UUID of service.
197      *
198      * @since 6
199      *
200      */
201     UUID uuid_;
202 };
203 } // namespace Bluetooth
204 } // namespace OHOS
205 #endif  // BLUETOOTH_GATT_SERVICE_H
206