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 #include "bluetooth_errorcode.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_utils_server.h"
19 #include "bt_def.h"
20 #include "interface_profile_manager.h"
21 #include "interface_profile_socket.h"
22 #include "permission_utils.h"
23 #include "bluetooth_socket_server.h"
24
25 using namespace OHOS::bluetooth;
26
27 namespace OHOS {
28 namespace Bluetooth {
Connect(ConnectSocketParam & param,int & fd)29 int BluetoothSocketServer::Connect(ConnectSocketParam ¶m, int &fd)
30 {
31 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
32 HILOGE("false, check permission failed");
33 return BT_ERR_INTERNAL_ERROR;
34 }
35 IProfileSocket *socket = (IProfileSocket *)IProfileManager::GetInstance()->GetProfileService(PROFILE_NAME_SPP);
36 if (socket != nullptr) {
37 fd = socket->Connect(param.addr, param.uuid, (int)param.securityFlag, (int)param.type);
38 }
39
40 return NO_ERROR;
41 }
42
Listen(ListenSocketParam & param,int & fd)43 int BluetoothSocketServer::Listen(ListenSocketParam ¶m, int &fd)
44 {
45 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
46 HILOGE("false, check permission failed");
47 return BT_ERR_PERMISSION_FAILED;
48 }
49
50 IProfileSocket *socket = (IProfileSocket *)IProfileManager::GetInstance()->GetProfileService(PROFILE_NAME_SPP);
51 if (socket != nullptr) {
52 fd = socket->Listen(param.name, param.uuid, (int)param.securityFlag, (int)param.type);
53 }
54
55 if (fd != -1 && param.observer != nullptr) {
56 socketObserverList_->AddObserver(fd, param.observer->AsObject());
57 }
58
59 return NO_ERROR;
60 }
61
DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> & observer)62 int BluetoothSocketServer::DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> &observer)
63 {
64 socketObserverList_->RemoveObserver(observer->AsObject());
65 return NO_ERROR;
66 }
67
RegisterClientObserver(const BluetoothRawAddress & addr,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)68 int BluetoothSocketServer::RegisterClientObserver(const BluetoothRawAddress &addr, const bluetooth::Uuid uuid,
69 const sptr<IBluetoothClientSocketObserver> &observer)
70 {
71 return BT_ERR_API_NOT_SUPPORT;
72 }
73
DeregisterClientObserver(const BluetoothRawAddress & addr,const bluetooth::Uuid uuid,const sptr<IBluetoothClientSocketObserver> & observer)74 int BluetoothSocketServer::DeregisterClientObserver(const BluetoothRawAddress &addr, const bluetooth::Uuid uuid,
75 const sptr<IBluetoothClientSocketObserver> &observer)
76 {
77 return BT_ERR_API_NOT_SUPPORT;
78 }
79
UpdateCocConnectionParams(const BluetoothSocketCocInfo & info)80 int BluetoothSocketServer::UpdateCocConnectionParams(const BluetoothSocketCocInfo &info)
81 {
82 return BT_ERR_API_NOT_SUPPORT;
83 }
84 } // namespace Bluetooth
85 } // namespace OHOS