1 /*
2 * Copyright (C) 2024 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 "client_session.h"
17 #include "telephony_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Telephony {
~ClientSession()21 ClientSession::~ClientSession()
22 {
23 Disconnect();
24 }
25
Connect(const std::string &peerDevId, const std::string &localName, const std::string &peerName)26 void ClientSession::Connect(const std::string &peerDevId, const std::string &localName, const std::string &peerName)
27 {
28 {
29 std::lock_guard<ffrt::mutex> lock(mutex_);
30 if (socket_ > INVALID_SOCKET_ID) {
31 TELEPHONY_LOGI("client socket %{public}d already connect", socket_);
32 return;
33 }
34 }
35 int32_t socket = CreateSocket(peerDevId, localName, peerName);
36 if (socket <= INVALID_SOCKET_ID) {
37 return;
38 }
39 QosTV qos[] = {
40 { .qos = QOS_TYPE_MIN_BW, .value = QOS_MIN_BW },
41 { .qos = QOS_TYPE_MAX_LATENCY, .value = QOS_MAX_LATENCY }
42 };
43 int32_t ret = BindAsync(socket, qos, sizeof(qos) / sizeof(qos[0]), &listener_);
44 TELEPHONY_LOGI("async bind socket %{public}d result %{public}d", socket, ret);
45 }
46
Disconnect()47 void ClientSession::Disconnect()
48 {
49 std::lock_guard<ffrt::mutex> lock(mutex_);
50 if (socket_ > INVALID_SOCKET_ID) {
51 Shutdown(socket_);
52 TELEPHONY_LOGI("close client socket %{public}d success", socket_);
53 }
54 socket_ = INVALID_SOCKET_ID;
55 }
56
OnSessionBind(int32_t socket)57 void ClientSession::OnSessionBind(int32_t socket)
58 {
59 {
60 std::lock_guard<ffrt::mutex> lock(mutex_);
61 socket_ = socket;
62 }
63 TELEPHONY_LOGI("session %{public}d bind success", socket);
64 if (callback_ != nullptr) {
65 callback_->OnConnected();
66 }
67 }
68
OnSessionShutdown(int32_t socket)69 void ClientSession::OnSessionShutdown(int32_t socket)
70 {
71 std::lock_guard<ffrt::mutex> lock(mutex_);
72 if (socket == socket_) {
73 socket_ = INVALID_SOCKET_ID;
74 }
75 }
76
CreateSocket(const std::string &peerDevId, const std::string &localName, const std::string &peerName)77 int32_t ClientSession::CreateSocket(const std::string &peerDevId, const std::string &localName,
78 const std::string &peerName)
79 {
80 if (peerDevId.empty()) {
81 TELEPHONY_LOGE("create socket fail, empty peer dev");
82 return INVALID_SOCKET_ID;
83 }
84 SocketInfo socketInfo = {
85 .name = const_cast<char*>((localName.c_str())),
86 .peerName = const_cast<char*>(peerName.c_str()),
87 .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
88 .pkgName = const_cast<char*>(PACKET_NAME),
89 .dataType = DATA_TYPE_BYTES
90 };
91 int32_t socket = Socket(socketInfo);
92 if (socket <= INVALID_SOCKET_ID) {
93 TELEPHONY_LOGE("create client socket fail %{public}d", socket);
94 return socket;
95 }
96 return socket;
97 }
98
99 } // namespace Telephony
100 } // namespace OHOS
101