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_AG_DATA_CONNECTION_SERVER_H
17 #define HFP_AG_DATA_CONNECTION_SERVER_H
18 
19 #include <cstdint>
20 
21 #include "base_def.h"
22 #include "hfp_ag_rfcomm_connection_server.h"
23 
24 namespace OHOS {
25 namespace bluetooth {
26 /**
27  * @brief Class for managining HFP AG data connection.
28  */
29 class HfpAgDataConnectionServer {
30 public:
31     /**
32      * @brief Get the HfpAgDataConnectionServer instance.
33      *
34      * @return Returns the HfpAgDataConnectionServer instance.
35      */
36     static HfpAgDataConnectionServer &GetInstance();
37 
38     /**
39      * @brief This function used to register rfcomm server.
40      *
41      * @return Returns the result of register rfcomm server.
42      */
43     int RegisterServer() const;
44 
45     /**
46      * @brief This function used to deregister rfcomm server.
47      *
48      * @return Returns the result of remove rfcomm server.
49      */
50     int RemoveServer();
51 
52     /**
53      * @brief This function called while rfcomm connection incoming.
54      *
55      * @param handle Rfcomm connection handle.
56      * @return Returns the result of accept rfcomm connection.
57      */
58     int AcceptConnection(uint16_t handle) const;
59 
60     /**
61      * @brief This function called while rfcomm connection incoming.
62      *
63      * @param handle Rfcomm connection handle.
64      * @return Returns the result of reject rfcomm connection.
65      */
66     int RejectConnection(uint16_t handle) const;
67 
68     /**
69      * @brief This function apply for server channel number.
70      *
71      * @return Returns the assigned server channel number.
72      */
73     uint8_t AssignLocalScn();
74 
75     /**
76      * @brief This function for get server channel number.
77      *
78      * @return Returns server channel number.
79      */
80     uint8_t GetLocalScn() const;
81 
82 private:
83     /**
84      * @brief Data connection server callback function.
85      *
86      * @param handle Handle.
87      * @param eventId Event id.
88      * @param eventData Event data.
89      * @param context Context.
90      */
91     static void DataConnectionServerCallback(uint16_t handle, uint32_t eventId,
92                                              const void *eventData, void *context);
93 
94     /**
95      * @brief Process Data connection server callback function in hfp thread.
96      *
97      * @param handle Handle.
98      * @param eventId Event id.
99      * @param inComingAddr Incoming connect request bluetooth device address.
100      */
101     static void ProcessDataConnectionServerCallback(uint16_t handle, uint32_t eventId, const std::string &inComingAddr);
102 
103     /**
104      * @brief Construct a new HfpHfDataConnectionServer object.
105      */
106     HfpAgDataConnectionServer() = default;
107 
108     /**
109      * @brief Destroy the HfpHfDataConnectionServer object.
110      */
111     ~HfpAgDataConnectionServer() = default;
112 
113     HfpAgRfcommConnectionServer rfcommServer_ {};
114 
115     BT_DISALLOW_COPY_AND_ASSIGN(HfpAgDataConnectionServer);
116 };
117 }  // namespace bluetooth
118 }  // namespace OHOS
119 #endif // HFP_AG_DATA_CONNECTION_SERVER_H