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_RFCOMM_CONNECTION_SERVER_H
17 #define HFP_HF_RFCOMM_CONNECTION_SERVER_H
18 
19 #include <cstdint>
20 
21 #include "base_def.h"
22 #include "rfcomm.h"
23 
24 namespace OHOS {
25 namespace bluetooth {
26 /**
27  * @brief Class for rfcomm connection server.
28  */
29 class HfpHfRfcommConnectionServer {
30 public:
31     /**
32      * @brief Construct a new HfpHfRfcommConnectionServer object.
33      */
34     HfpHfRfcommConnectionServer() = default;
35 
36     /**
37      * @brief Destroy the HfpHfRfcommConnectionServer object.
38      */
39     ~HfpHfRfcommConnectionServer() = default;
40 
41     /**
42      * @brief This function used to register rfcomm server.
43      *
44      * @param fn The pointer to rfcomm callback function.
45      * @return Returns the result of register rfcomm server.
46      */
47     int RegisterServer(RFCOMM_EventCallback fn) const;
48 
49     /**
50      * @brief This function used to deregister rfcomm server.
51      *
52      * @return Returns the result of remove rfcomm server.
53      */
54     int RemoveServer();
55 
56     /**
57      * @brief This function called while rfcomm connection incoming.
58      *
59      * @param handle Rfcomm handle.
60      * @return Returns the result of accept rfcomm connection.
61      */
62     static int AcceptConnection(uint16_t handle);
63 
64     /**
65      * @brief This function called while rfcomm connection incoming.
66      *
67      * @param handle Rfcomm handle.
68      * @return Returns the result of reject rfcomm connection.
69      */
70     static int RejectConnection(uint16_t handle);
71 
72     /**
73      * @brief This function apply for server channel number.
74      *
75      * @return Returns assigned server channel number.
76      */
77     uint8_t AssignLocalScn();
78 
79     /**
80      * @brief This function for get server channel number.
81      *
82      * @return Returns server channel number.
83      */
84     uint8_t GetLocalScn() const;
85 
86 private:
87     uint8_t localScn_ {0};
88     inline static constexpr uint32_t HFP_RFCOMM_CONNECTION_SERVER_EVENTS =
89         RFCOMM_CHANNEL_EV_CONNECT_INCOMING | RFCOMM_CHANNEL_EV_CONNECT_SUCCESS | RFCOMM_CHANNEL_EV_CONNECT_FAIL |
90         RFCOMM_CHANNEL_EV_DISCONNECTED | RFCOMM_CHANNEL_EV_DISCONNECT_SUCCESS | RFCOMM_CHANNEL_EV_DISCONNECT_FAIL |
91         RFCOMM_CHANNEL_EV_REV_DATA;
92 
93     BT_DISALLOW_COPY_AND_ASSIGN(HfpHfRfcommConnectionServer);
94 };
95 }  // namespace bluetooth
96 }  // namespace OHOS
97 #endif // HFP_HF_RFCOMM_CONNECTION_SERVER_H