1 /*
2  * Copyright (C) 2021-2022 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 #ifndef GENERIC_ATTRIBUTE_SERVICE_H
17 #define GENERIC_ATTRIBUTE_SERVICE_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <map>
22 #include <memory>
23 #include "../gatt_connection.h"
24 #include "../gatt_server_service.h"
25 #include "base_def.h"
26 #include "dispatcher.h"
27 #include "gatt_data.h"
28 #include "gatt_service_over_bredr.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 class GenericAttributeService {
33 public:
34     GenericAttributeService(GattServerService &service, utility::Dispatcher &dispatcher);
35     ~GenericAttributeService();
36 
37     int RegisterService();
38     void DeregisterService() const;
39 
40     int RegisterSDP();
41     void DeregisterSDP() const;
42 
43     BT_DISALLOW_COPY_AND_ASSIGN(GenericAttributeService);
44 
45 private:
46     static const size_t CLIENT_CHARACTERISTIC_CONFIGURATION_VALUE_LENGTH;
47     static const size_t SERVICE_CHANGED_VALUE_LENGTH;
48     static const uint8_t CLIENT_CHARACTERISTIC_CONFIGURATION_DEFAULT_VALUE[2];
49     static const uint8_t SERVICE_CHANGED_DEFAULT_VALUE[4];
50 
51     class GattServerCallbackImpl;
52     struct NotifyInformation {
53         bool isNotified = true;
54         uint16_t startHandle_ = 0;
55         uint16_t endHandle_ = 0;
56     };
57 
58     GattServerService &serverService_;
59     utility::Dispatcher &dispatcher_;
60     int appId_ = -1;
61     std::unique_ptr<GattServiceOverBredrInterface> sdpRegister_ = {nullptr};
62     std::unique_ptr<Service> instance_ = {nullptr};
63     std::shared_ptr<GattServerCallbackImpl> serviceCallback_;
64     std::map<GattDevice, NotifyInformation> devices_ = {};
65 
66     void StoreNotifyInformation() const;
67     void LoadNotifyInformation();
68     void ComfirmNotify(GattDevice device);
69     std::unique_ptr<Service> BuildService();
70     void NotifyAllDevices(uint16_t startHandle, uint16_t endHandle);
71     void NotifyServiceChanged(const GattDevice& device, Characteristic& characteristic) const;
72     void ConnectIncoming(const GattDevice& device);
73     void Disconnected(const GattDevice& device);
74     std::unique_ptr<uint8_t[]> BuildValue(uint16_t startHandle, uint16_t endHandle) const;
75 };
76 }  // namespace bluetooth
77 }  // namespace OHOS
78 
79 #endif // GENERIC_ATTRIBUTE_SERVICE_H