• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dbinder_remote_listener.h"
17 #include "securec.h"
18 #include "ipc_types.h"
19 #include "dbinder_log.h"
20 #include "dbinder_error_code.h"
21 
22 namespace OHOS {
DBinderRemoteListener(const sptr<DBinderService> & dBinderService)23 DBinderRemoteListener::DBinderRemoteListener(const sptr<DBinderService> &dBinderService)
24     : dBinderService_(dBinderService)
25 {
26     DBINDER_LOGI("create dbinder remote listener");
27 }
28 
~DBinderRemoteListener()29 DBinderRemoteListener::~DBinderRemoteListener()
30 {
31     DBINDER_LOGI("delete dbinder remote listener");
32 }
33 
StartListener(std::shared_ptr<DBinderRemoteListener> & listener)34 bool DBinderRemoteListener::StartListener(std::shared_ptr<DBinderRemoteListener> &listener)
35 {
36     std::lock_guard<std::mutex> lockGuard(busManagerMutex_);
37     std::shared_ptr<ISessionService> softbusManager_ = ISessionService::GetInstance();
38     if (softbusManager_ == nullptr) {
39         DBINDER_LOGE("fail to get softbus service");
40         return false;
41     }
42     int ret = softbusManager_->CreateSessionServer(OWN_SESSION_NAME, PEER_SESSION_NAME, listener);
43     if (ret != 0) {
44         DBINDER_LOGE("fail to create softbus server with ret = %{public}d", ret);
45         return false;
46     }
47     return true;
48 }
49 
StopListener()50 bool DBinderRemoteListener::StopListener()
51 {
52     std::lock_guard<std::mutex> lockGuard(busManagerMutex_);
53     std::shared_ptr<ISessionService> softbusManager_ = ISessionService::GetInstance();
54     if (softbusManager_ == nullptr) {
55         DBINDER_LOGE("softbus manager is null");
56         return false;
57     }
58 
59     int ret = softbusManager_->RemoveSessionServer(OWN_SESSION_NAME, PEER_SESSION_NAME);
60     if (ret != 0) {
61         DBINDER_LOGE("fail to remove softbus server");
62         return false;
63     }
64     softbusManager_ = nullptr;
65     return true;
66 }
67 
SendDataToRemote(const std::string & deviceId,const struct DHandleEntryTxRx * msg)68 bool DBinderRemoteListener::SendDataToRemote(const std::string &deviceId, const struct DHandleEntryTxRx *msg)
69 {
70     if (msg == nullptr) {
71         DBINDER_LOGE("msg is null");
72         return false;
73     }
74 
75     std::shared_ptr<Session> session = OpenSoftbusSession(deviceId);
76     if (session == nullptr) {
77         DBINDER_LOGE("fail to open session");
78         return false;
79     }
80 
81     int ret = session->SendBytes(msg, msg->head.len);
82     if (ret != 0) {
83         DBINDER_LOGE("fail to send bytes, ret = %{public}d", ret);
84         return false;
85     }
86     return true;
87 }
88 
CloseDatabusSession(const std::string & deviceId)89 bool DBinderRemoteListener::CloseDatabusSession(const std::string &deviceId)
90 {
91     std::lock_guard<std::mutex> lockGuard(busManagerMutex_);
92     std::shared_ptr<ISessionService> softbusManager_ = ISessionService::GetInstance();
93     if (softbusManager_ == nullptr) {
94         DBINDER_LOGE("softbus manager is null");
95         return false;
96     }
97 
98     std::shared_ptr<Session> session = softbusManager_->OpenSession(OWN_SESSION_NAME, PEER_SESSION_NAME, deviceId,
99         std::string(""), Session::TYPE_BYTES);
100     if (session == nullptr) {
101         DBINDER_LOGE("fail to open session before closing it");
102         return false;
103     }
104 
105     return softbusManager_->CloseSession(session) == 0;
106 }
107 
OpenSoftbusSession(const std::string & peerDeviceId)108 std::shared_ptr<Session> DBinderRemoteListener::OpenSoftbusSession(const std::string &peerDeviceId)
109 {
110     std::lock_guard<std::mutex> lockGuard(busManagerMutex_);
111     std::shared_ptr<ISessionService> softbusManager_ = ISessionService::GetInstance();
112     if (softbusManager_ == nullptr) {
113         DBINDER_LOGE("softbus manager is null");
114         return nullptr;
115     }
116 
117     return softbusManager_->OpenSession(OWN_SESSION_NAME, PEER_SESSION_NAME, peerDeviceId, std::string(""),
118         Session::TYPE_BYTES);
119 }
120 
OnSessionOpened(std::shared_ptr<Session> session)121 int DBinderRemoteListener::OnSessionOpened(std::shared_ptr<Session> session)
122 {
123     DBINDER_LOGI("peer session is open");
124     if (session->GetPeerSessionName() != PEER_SESSION_NAME) {
125         DBINDER_LOGE("invalid session name, peer session name = %{public}s", session->GetPeerSessionName().c_str());
126         return -DBINDER_SERVICE_WRONG_SESSION;
127     }
128     return 0;
129 }
130 
OnSessionClosed(std::shared_ptr<Session> session)131 void DBinderRemoteListener::OnSessionClosed(std::shared_ptr<Session> session)
132 {
133     DBINDER_LOGI("peer session name = %{public}s is closed", session->GetPeerSessionName().c_str());
134 }
135 
OnBytesReceived(std::shared_ptr<Session> session,const char * data,ssize_t len)136 void DBinderRemoteListener::OnBytesReceived(std::shared_ptr<Session> session, const char *data, ssize_t len)
137 {
138     if (data == nullptr || len != static_cast<ssize_t>(sizeof(struct DHandleEntryTxRx))) {
139         DBINDER_LOGE("session has wrong input, peer session name = %s, data length = %zd",
140             session->GetPeerSessionName().c_str(), len);
141         // ignore the package
142         return;
143     }
144 
145     if (dBinderService_ == nullptr) {
146         DBINDER_LOGE("dbinder service is not started");
147         return;
148     }
149 
150     struct DHandleEntryTxRx *handleEntry = (struct DHandleEntryTxRx *)data;
151     if (handleEntry == nullptr) {
152         DBINDER_LOGE("msg is null");
153         return;
154     }
155 
156     if (!dBinderService_->OnRemoteMessageTask(handleEntry)) {
157         DBINDER_LOGE("process remote message fail");
158     }
159 }
160 } // namespace OHOS
161