1 /*
2 * Copyright (C) 2025 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_cj_socket_spp_server"
17 #endif
18
19 #include "bluetooth_spp_server_impl.h"
20
21 #include "bluetooth_errorcode.h"
22 #include "napi_bluetooth_utils.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
26 const int NUM_20 = 20;
27 int SppServerImpl::count = 0;
28 std::map<int, std::shared_ptr<SppServerImpl>> SppServerImpl::serverMap;
29
SppListen(std::string name,SppOption sppOption,int32_t * errCode)30 int32_t SppServerImpl::SppListen(std::string name, SppOption sppOption, int32_t* errCode)
31 {
32 HILOGD("enter");
33 std::shared_ptr<ServerSocket> server_ =
34 std::make_shared<ServerSocket>(name, UUID::FromString(sppOption.uuid_), sppOption.type_, sppOption.secure_);
35 int errorCode = server_->Listen();
36 if (server_->GetStringTag() != "") {
37 HILOGD("SppListen execute listen success");
38 } else {
39 HILOGE("SppListen execute listen failed");
40 *errCode = static_cast<int32_t>(errorCode);
41 return -1;
42 }
43 std::shared_ptr<SppServerImpl> server = std::make_shared<SppServerImpl>();
44 server->id_ = SppServerImpl::count++;
45 server->server_ = server_;
46 serverMap.insert(std::make_pair(server->id_, server));
47 return static_cast<int32_t>(server->id_);
48 }
49
SppAccept(int32_t serverSocket,int32_t * errCode)50 int32_t SppServerImpl::SppAccept(int32_t serverSocket, int32_t* errCode)
51 {
52 HILOGD("enter");
53 std::shared_ptr<SppServerImpl> server = SppServerImpl::serverMap[serverSocket];
54 if (!server) {
55 HILOGE("server is null");
56 *errCode = BT_ERR_INVALID_PARAM;
57 return -1;
58 }
59 std::shared_ptr<ServerSocket> server_ = server->server_;
60 std::shared_ptr<ClientSocket> client_ = server_->Accept(NUM_20);
61 if (client_ == nullptr) {
62 HILOGE("client_ is null");
63 *errCode = BT_ERR_INVALID_PARAM;
64 return -1;
65 }
66 std::shared_ptr<SppClientImpl> client = std::make_shared<SppClientImpl>();
67 client->id_ = SppClientImpl::count++;
68 client->client_ = client_;
69 SppClientImpl::clientMap.insert(std::make_pair(client->id_, client));
70 return static_cast<int32_t>(client->id_);
71 }
72
RegisterSocketObserver(std::string type,int32_t clientSocket,int64_t cbId)73 int32_t SppServerImpl::RegisterSocketObserver(std::string type, int32_t clientSocket, int64_t cbId)
74 {
75 return SppClientImpl::On(type, clientSocket, cbId);
76 }
77
DeRegisterSocketObserver(std::string type,int32_t clientSocket,int64_t cbId)78 int32_t SppServerImpl::DeRegisterSocketObserver(std::string type, int32_t clientSocket, int64_t cbId)
79 {
80 return SppClientImpl::Off(type, clientSocket, cbId);
81 }
82
SppCloseServerSocket(int32_t socket)83 int32_t SppServerImpl::SppCloseServerSocket(int32_t socket)
84 {
85 HILOGD("enter");
86 bool isOK = false;
87
88 std::shared_ptr<SppServerImpl> server = nullptr;
89 std::shared_ptr<SppClientImpl> client = nullptr;
90 if (SppClientImpl::clientMap[socket] != nullptr) {
91 client = SppClientImpl::clientMap[socket];
92 if (client->client_) {
93 client->client_->Close();
94 SppClientImpl::clientMap.erase(socket);
95 }
96 } else {
97 HILOGE("invalid socket.");
98 }
99 if (serverMap[socket] != nullptr) {
100 server = serverMap[socket];
101 if (server->server_) {
102 server->server_->Close();
103 serverMap.erase(socket);
104 isOK = true;
105 }
106 } else {
107 HILOGE("invalid socket.");
108 }
109 return isOK ? 0 : BT_ERR_INVALID_PARAM;
110 }
111 } // namespace Bluetooth
112 } // namespace OHOS