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 GATT_SERVER_SERVICE_H
17 #define GATT_SERVER_SERVICE_H
18 
19 #include <list>
20 #include <string>
21 #include "base_def.h"
22 #include "context.h"
23 #include "gatt_data.h"
24 #include "interface_profile_gatt_server.h"
25 #include "raw_address.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 class GattServerService : public IProfileGattServer, public utility::Context {
30 public:
31     int RegisterApplication(std::shared_ptr<IGattServerCallback> callback) override;
32     int DeregisterApplication(int appId) override;
33     int AddService(int appId, Service &service) override;
34     int RemoveService(int appId, const Service &service) override;
35     int ClearServices(int appId) override;
36     int NotifyClient(const GattDevice &device, Characteristic &characteristic, bool needConfirm = false) override;
37     int RespondCharacteristicRead(const GattDevice &device, Characteristic &characteristic, int ret) override;
38     int RespondCharacteristicReadByUuid(const GattDevice &device, Characteristic &characteristic, int ret) override;
39     int RespondCharacteristicWrite(const GattDevice &device, const Characteristic &characteristic, int ret) override;
40     int RespondDescriptorRead(const GattDevice &device, Descriptor &descriptor, int ret) override;
41     int RespondDescriptorWrite(const GattDevice &device, const Descriptor &descriptor, int ret) override;
42     int CancelConnection(const GattDevice &device) override;
43     int SetCharacteristicValue(const Characteristic &characteristic);
44     int SetCharacteristicPermission(const Characteristic &characteristic, uint8_t properties, uint8_t permission);
45 
46     int RegisterApplicationSync(std::shared_ptr<IGattServerCallback> callback) const;
47     int DeregisterApplicationSync(int appId) const;
48     int AddServiceSync(int appId, Service &service) const;
49     const std::string GetDatabaseHash() const;
50 
51     GattServerService();
52     ~GattServerService();
53 
54     /// Inherited from the parent class
55     void Enable() override;
56     void Disable() override;
57     utility::Context *GetContext() override;
58 
59     std::list<RawAddress> GetConnectDevices() override;
60     int GetConnectState() override;
61     int GetMaxConnectNum() override;
62 
63     BT_DISALLOW_COPY_AND_ASSIGN(GattServerService);
64 
65 private:
66     int Connect(const RawAddress &device) override;
67     int Disconnect(const RawAddress &device) override;
68 
69     DECLARE_IMPL();
70 };
71 }  // namespace bluetooth
72 }  // namespace OHOS
73 
74 #endif  // !GATT_SERVER_SERVICE_H
75