• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,const int32_t & qosMinBw)26 void ClientSession::Connect(const std::string &peerDevId, const std::string &localName, const std::string &peerName,
27     const int32_t &qosMinBw)
28 {
29     {
30         std::lock_guard<ffrt::mutex> lock(mutex_);
31         if (clientSocket_ > INVALID_SOCKET_ID) {
32             TELEPHONY_LOGI("client socket %{public}d already connect", clientSocket_);
33             return;
34         }
35     }
36     int32_t socket = CreateSocket(peerDevId, localName, peerName);
37     if (socket <= INVALID_SOCKET_ID) {
38         return;
39     }
40     QosTV qos[] = {
41         { .qos = QOS_TYPE_MIN_BW, .value = qosMinBw },
42         { .qos = QOS_TYPE_MAX_LATENCY, .value = QOS_MAX_LATENCY }
43     };
44     int32_t ret = BindAsync(socket, qos, sizeof(qos) / sizeof(qos[0]), &listener_);
45     TELEPHONY_LOGI("async bind socket %{public}d result %{public}d", socket, ret);
46 
47     std::lock_guard<ffrt::mutex> lock(mutex_);
48     clientSocket_ = socket;
49 }
50 
Disconnect()51 void ClientSession::Disconnect()
52 {
53     int32_t socket = INVALID_SOCKET_ID;
54     {
55         std::lock_guard<ffrt::mutex> lock(mutex_);
56         if (clientSocket_ > INVALID_SOCKET_ID) {
57             socket = clientSocket_;
58         }
59         clientSocket_ = INVALID_SOCKET_ID;
60         socket_ = INVALID_SOCKET_ID;
61         TELEPHONY_LOGI("disconnect client session");
62     }
63     if (socket > INVALID_SOCKET_ID) {
64         Shutdown(socket);
65         TELEPHONY_LOGI("close client socket %{public}d success", socket);
66     }
67 }
68 
OnSessionBind(int32_t socket)69 void ClientSession::OnSessionBind(int32_t socket)
70 {
71     {
72         std::lock_guard<ffrt::mutex> lock(mutex_);
73         socket_ = socket;
74     }
75     TELEPHONY_LOGI("session %{public}d bind success", socket);
76     if (callback_ != nullptr) {
77         callback_->OnConnected();
78     }
79 }
80 
OnSessionShutdown(int32_t socket)81 void ClientSession::OnSessionShutdown(int32_t socket)
82 {
83     std::lock_guard<ffrt::mutex> lock(mutex_);
84     if (socket == socket_) {
85         socket_ = INVALID_SOCKET_ID;
86     }
87     if (socket == clientSocket_) {
88         clientSocket_ = INVALID_SOCKET_ID;
89         socket_ = INVALID_SOCKET_ID;
90     }
91 }
92 
CreateSocket(const std::string & peerDevId,const std::string & localName,const std::string & peerName)93 int32_t ClientSession::CreateSocket(const std::string &peerDevId, const std::string &localName,
94                                     const std::string &peerName)
95 {
96     if (peerDevId.empty()) {
97         TELEPHONY_LOGE("create socket fail, empty peer dev");
98         return INVALID_SOCKET_ID;
99     }
100     SocketInfo socketInfo = {
101         .name = const_cast<char*>((localName.c_str())),
102         .peerName = const_cast<char*>(peerName.c_str()),
103         .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
104         .pkgName = const_cast<char*>(PACKET_NAME),
105         .dataType = DATA_TYPE_BYTES
106     };
107     int32_t socket = Socket(socketInfo);
108     if (socket <= INVALID_SOCKET_ID) {
109         TELEPHONY_LOGE("create client socket fail %{public}d", socket);
110         return socket;
111     }
112     return socket;
113 }
114 
115 } // namespace Telephony
116 } // namespace OHOS
117