1 /*
2 * Copyright (C) 2021 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 #include "hfp_hf_rfcomm_connection_server.h"
17
18 #include "hfp_hf_defines.h"
19 #include "hfp_hf_gap_server.h"
20
21 namespace OHOS {
22 namespace bluetooth {
RegisterServer(RFCOMM_EventCallback fn) const23 int HfpHfRfcommConnectionServer::RegisterServer(RFCOMM_EventCallback fn) const
24 {
25 int ret = HfpHfGapServer::RegisterServiceSecurity(localScn_);
26 HFP_HF_RETURN_IF_FAIL(ret);
27
28 ret = RFCOMM_RegisterServer(localScn_, HFP_HF_COMMAND_MTU, HFP_RFCOMM_CONNECTION_SERVER_EVENTS, fn, nullptr);
29 HFP_HF_RETURN_IF_FAIL(ret);
30 return BT_SUCCESS;
31 }
32
RemoveServer()33 int HfpHfRfcommConnectionServer::RemoveServer()
34 {
35 int ret = HfpHfGapServer::DeregisterServiceSecurity();
36 HFP_HF_RETURN_IF_FAIL(ret);
37
38 ret = RFCOMM_DeregisterServer(localScn_, true);
39 HFP_HF_RETURN_IF_FAIL(ret);
40
41 ret = RFCOMM_FreeServerNum(localScn_);
42 HFP_HF_RETURN_IF_FAIL(ret);
43
44 localScn_ = 0;
45 return BT_SUCCESS;
46 }
47
AcceptConnection(uint16_t handle)48 int HfpHfRfcommConnectionServer::AcceptConnection(uint16_t handle)
49 {
50 int ret = RFCOMM_AcceptConnection(handle);
51 HFP_HF_RETURN_IF_FAIL(ret);
52 return BT_SUCCESS;
53 }
54
RejectConnection(uint16_t handle)55 int HfpHfRfcommConnectionServer::RejectConnection(uint16_t handle)
56 {
57 int ret = RFCOMM_RejectConnection(handle);
58 HFP_HF_RETURN_IF_FAIL(ret);
59 return BT_SUCCESS;
60 }
61
AssignLocalScn()62 uint8_t HfpHfRfcommConnectionServer::AssignLocalScn()
63 {
64 localScn_ = RFCOMM_AssignServerNum();
65 return localScn_;
66 }
67
GetLocalScn() const68 uint8_t HfpHfRfcommConnectionServer::GetLocalScn() const
69 {
70 return localScn_;
71 }
72 } // namespace bluetooth
73 } // namespace OHOS