1 /*
2  * Copyright (C) 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 HID_HOST_HOGP_H
17 #define HID_HOST_HOGP_H
18 
19 #include <map>
20 
21 #include "dispatcher.h"
22 #include "gap_le_if.h"
23 #include "gatt_data.h"
24 #include "gatt/gatt_defines.h"
25 #include "hid_host_defines.h"
26 #include "hid_host_message.h"
27 #include "interface_profile_gatt_client.h"
28 #include "interface_profile_manager.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 using utility::Dispatcher;
33 
34 class HidHostHogp {
35 public:
36     /**
37      * @brief Construct a new HidHostHogp object
38      *
39      */
40     explicit HidHostHogp(const std::string &address);
41     /**
42      * @brief Destroy the HidHostHogp object
43      *
44      */
45     ~HidHostHogp();
46 
47     int Connect();
48     int Disconnect();
49     int SendData(const HidHostMessage &msg);
50     PnpInformation& GetRemotePnpInfo();
51     HidInformation& GetRemoteHidInfo();
52     void ProcessEvent(const HidHostMessage &event);
53     static std::string GetEventName(int what);
54 
55 private:
56     class HogpGattClientCallback : public IGattClientCallback {
57     public:
58         explicit HogpGattClientCallback(HidHostHogp *hogp);
59         ~HogpGattClientCallback() = default;
60         void OnConnectionStateChanged(int state, int newState) override;
61         void OnServicesChanged(const std::vector<Service> &services) override;
62         void OnCharacteristicRead(int ret, const Characteristic &characteristic) override;
63         void OnCharacteristicWrite(int ret, const Characteristic &characteristic) override;
64         void OnCharacteristicChanged(const Characteristic &characteristic) override;
65         void OnDescriptorRead(int ret, const Descriptor &descriptor) override;
66         void OnDescriptorWrite(int ret, const Descriptor &descriptor) override;
67         void OnMtuChanged(int state, int mtu) override;
68         void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) override;
69         void OnServicesDiscovered(int status) override;
70         void OnReadRemoteRssiValue(const RawAddress &addr, int rssi, int status) override;
71 
72     private:
73         HidHostHogp *hogp_;
74     };
75     std::string address_ {""};
76     int appId_ = -1;
77     PnpInformation pnpInf_ {};
78     HidInformation hidInf_ {};
79     std::unique_ptr<HogpGattClientCallback> gattCallback_ = nullptr;
80     uint8_t state_;
81     std::condition_variable cvfull_;
82     std::mutex mutexWaitGattCallback_;
83     std::unique_ptr<Characteristic> characteristicTemp_ = nullptr;
84     std::unique_ptr<Descriptor> descriptorTemp_ = nullptr;
85     std::map<uint16_t, std::unique_ptr<HogpReport>> reports_ {};
86     std::unique_ptr<Dispatcher> dispatcher_ {};
87 
88     IProfileGattClient *GetGattClientService();
89     int DiscoverStart();
90     int RegisterGattClientApplication(const RawAddress &addr);
91     int DeregisterGattClientApplication();
92 
93     static void GapLeRequestSecurityResult(
94         const BtAddr *addr, uint8_t result, GAP_LeSecurityStatus status, void *context);
95 
96     void OnConnectionStateChangedTask(int newState);
97     void OnServicesDiscoveredTask(int status);
98     void OnServicesDiscoveredTask_(int status);
99     void SendStopHogpThread();
100     void SendData_(const HidHostMessage &msg);
101 
102     int GetPnpInformation(Service service);
103     int GetHidInformation(Service service);
104     int GetHidReportMap(Characteristic character);
105     void GetHidReport(Characteristic character);
106     int SavePnpInformation(Characteristic character);
107     int SaveHidInformation(Characteristic character);
108     int SaveReportMap(Characteristic character);
109     void SaveReport(Characteristic character, Descriptor descriptor, Descriptor config);
110     void SetInputReportNotification();
111 
112     int SendGetReport(uint8_t reportId, Characteristic character);
113     int SendSetReport(Characteristic character, int length, uint8_t* pkt);
114     void ReceiveHandShake(uint16_t err);
115     int ReceiveControlData(Characteristic character, uint8_t reportId);
116     HogpReport* FindReportByTypeAndId(uint8_t type, uint8_t reportId);
117 
118     // For PTS
119     void GetBatteryInformation(Service service);
120     void GetExternalRptRefInfo(Characteristic character);
121     void GetExternalCfgInfo(Characteristic character);
122 };
123 }  // namespace bluetooth
124 }  // namespace OHOS
125 #endif  // HID_HOST_HOGP_H