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 HFP_HF_DATA_CONNECTION_H
17 #define HFP_HF_DATA_CONNECTION_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "base_def.h"
25 #include "hfp_hf_defines.h"
26 #include "hfp_hf_rfcomm_connection.h"
27 #include "raw_address.h"
28 
29 namespace OHOS {
30 namespace bluetooth {
31 /**
32  * @brief Class for HFP HF data connection.
33  */
34 class HfpHfDataConnection {
35     typedef struct {
36         std::string name {""};
37         uint32_t index {0};
38         uint32_t minVal {0};
39         uint32_t maxVal {0};
40         uint32_t value {0};
41         bool isEnabled {false};
42     } AgIndicator;
43 
44     typedef struct {
45         uint16_t anum {0};
46         uint32_t value {0};
47         uint32_t minVal {0};
48         uint32_t maxVal {0};
49         bool isSupported {false};
50         bool isEnabled {false};
51     } HfIndicator;
52 
53 public:
54     /**
55      * @brief Data connection callback function.
56      *
57      * @param handle Handle.
58      * @param eventId Event id.
59      * @param eventData Event data.
60      * @param context Context.
61      */
62     static void DataConnectionCallback(uint16_t handle, uint32_t eventId, const void *eventData, void *context);
63 
64     /**
65      * @brief Get the AG local features.
66      *
67      * @return Returns the local AG support features.
68      */
69     static uint32_t GetLocalFeatures();
70 
71     /**
72      * @brief Construct a new HfpHfDataConnection object.
73      */
74     HfpHfDataConnection() = default;
75 
76     /**
77      * @brief Destroy the HfpHfDataConnection object.
78      */
79     ~HfpHfDataConnection() = default;
80 
81     /**
82      * @brief Init data connection.
83      */
84     static int Init();
85 
86     /**
87      * @brief Clean up data connection.
88      */
89     static void CleanUp();
90 
91     /**
92      * @brief  This function used to connect remote device rfcomm channel after SDP discovery OK (Initiator).
93      *
94      * @return Returns the error code of the connect result.
95      */
96     int Connect();
97 
98     /**
99      * @brief This function used to disconnect remote device rfcomm channel.
100      *
101      * @return Returns the error code of the disconnect result.
102      */
103     int Disconnect() const;
104 
105     /**
106      * @brief  This function used to read data from remote device.
107      *
108      * @param pkt The address of data packet pointer.
109      * @return Returns the error code of the read data result.
110      */
111     int ReadData(Packet **pkt) const;
112 
113     /**
114      * @brief This function used to write data to remote device.
115      *
116      * @param pkt Data packet pointer.
117      * @return Returns the error code of the write data result.
118      */
119     int WriteData(Packet &pkt) const;
120 
121     /**
122      * @brief Set the remote address.
123      *
124      * @param addr Remote device address.
125      */
126     void SetRemoteAddr(const std::string &addr);
127 
128     /**
129      * @brief Set the SDP info.
130      *
131      * @param sdpInfo Remote device SDP info.
132      */
133     void SetSdpInfo(HfpHfRemoteSdpInfo sdpInfo);
134 
135     /**
136      * @brief Get the connection role.
137      *
138      * @return Returns the connection role.
139      */
140     int GetRole() const;
141 
142     /**
143      * @brief Set the connection role.
144      *
145      * @param role Role in connection.
146      */
147     void SetRole(int role);
148 
149     /**
150      * @brief Get the connection handle.
151      *
152      * @return Returns the connection handle.
153      */
154     uint16_t GetConnectionHandle() const;
155 
156     /**
157      * @brief Set the connection handle.
158      *
159      * @param handle Connection handle.
160      */
161     void SetConnectionHandle(uint16_t handle);
162 
163     /**
164      * @brief Set the SLC connect state.
165      *
166      * @param state SLC connect state.
167      */
168     void SetSlcConnectState(bool state);
169 
170     /**
171      * @brief  This function used to get support of codec negotiation.
172      *
173      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
174      */
175     bool IsCodecNegotiationSupport() const;
176 
177     /**
178      * @brief This function used to get support of ESCO.
179      *
180      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
181      */
182     static bool IsEscoSupport();
183 
184     /**
185      * @brief This function used to get support of ESCO S4.
186      *
187      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
188      */
189     bool IsEscoS4Support() const;
190 
191     /**
192      * @brief This function get remote server channel number.
193      *
194      * @return Remote server channel number
195      */
196     uint8_t GetRemoteScn() const;
197 
198 private:
199     /**
200      * @brief Process Data connection callback function in hfp thread.
201      *
202      * @param handle Handle.
203      * @param eventId Event id.
204      */
205     static void ProcessDataConnectionCallback(uint16_t handle, uint32_t eventId);
206 
207     friend class HfpHfProfile;
208     friend class HfpHfCommandProcessor;
209 
210     static inline const std::string BIND_SETTINGS = "1,2";  // Enhanced Driver Status & Battery Level Status
211     static constexpr uint8_t ANUM_CVSD = 0x02;              // Coding Format(1 Octet) Assign Number CVSD
212     static constexpr uint8_t ANUM_MSBC = 0x05;              // Coding Format(1 Octet) Assign Number MSBC
213     static uint32_t g_localFeatures;
214     static inline int g_localSupportCodecs {HFP_HF_CODEC_NONE};
215     int role_ {HFP_HF_ACCEPTOR};
216     HfpHfConnectState state_ {HFP_HF_STATE_DISCONNECTED};
217     uint32_t remoteFeatures_ {0};
218     int localDemandCodec_ {HFP_HF_CODEC_MSBC};
219     int localSelectedCodec_ {HFP_HF_CODEC_MSBC};
220     int remoteSelectedCodec_ {HFP_HF_CODEC_MSBC};
221     int remoteSupportCodecs_ {HFP_HF_CODEC_NONE};
222     int inUseCodec_ {HFP_HF_CODEC_CVSD};
223     bool slcConnected_ {false};
224     uint32_t remoteChldFeatures_ {0};
225     std::string remoteAddr_ {""};
226     HfpHfRemoteSdpInfo sdpInfo_ {};
227     std::vector<AgIndicator> remoteAgIndicators_ {};
228     std::vector<HfIndicator> remoteHfIndicators_ {};
229     std::vector<HfIndicator> localHfIndicators_ = {
230         {1, 0, 0, 1, true, true},   /* Enhanced Driver Status */
231         {2, 0, 0, 100, true, true}  /* Battery Level Status */
232     };
233     HfpHfRfcommConnection rfcommConnection_ {&HfpHfDataConnection::DataConnectionCallback};
234 
235     BT_DISALLOW_COPY_AND_ASSIGN(HfpHfDataConnection);
236 };
237 }  // namespace bluetooth
238 }  // namespace OHOS
239 #endif // HFP_HF_DATA_CONNECTION_H