• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "soft_bus_session_listener.h"
17 #include "remote_command_manager.h"
18 #include "soft_bus_manager.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusSessionListener"};
25 }
26 namespace {
27 //  Indicates the pointer to the session name, which is the unique ID of the session server. The value cannot be empty
28 //  and can contain a maximum of 64 characters.
29 static const int32_t SESSION_NAME_MAXLENGTH = 64;
30 static const int32_t SESSION_ACCEPTED = 0;
31 static const int32_t SESSION_REFUSED = -1;
32 } // namespace
33 
34 std::mutex SoftBusSessionListener::g_SessionMutex_;
35 std::map<int32_t, int64_t> SoftBusSessionListener::g_SessionOpenedMap_;
36 
OnSessionOpened(int32_t sessionId,int32_t result)37 int32_t SoftBusSessionListener::OnSessionOpened(int32_t sessionId, int32_t result)
38 {
39     if (result != Constant::SUCCESS) {
40         ACCESSTOKEN_LOG_ERROR(LABEL, "OnSessionOpened, result: %{public}d", result);
41         return SESSION_REFUSED;
42     }
43 
44     int32_t len = SESSION_NAME_MAXLENGTH + 1;
45     char contents[len];
46     int32_t resultCode = ::GetPeerSessionName(sessionId, contents, len);
47     if (resultCode != Constant::SUCCESS) {
48         ACCESSTOKEN_LOG_ERROR(LABEL, "OnSessionOpened, GetPeerSessionName failed, result: %{public}d", resultCode);
49         return SESSION_REFUSED;
50     }
51     std::string peerSessionName(contents);
52     if (SoftBusManager::SESSION_NAME != peerSessionName) {
53         ACCESSTOKEN_LOG_ERROR(LABEL, "OnSessionOpened, unknown sessionId name.");
54         return SESSION_REFUSED;
55     }
56 
57     ACCESSTOKEN_LOG_INFO(LABEL, "OnSessionOpened, id = %{public}d", sessionId);
58 
59     // store sessionId state: opening
60     std::lock_guard<std::mutex> guard(g_SessionMutex_);
61     auto iter = g_SessionOpenedMap_.find(sessionId);
62     if (iter == g_SessionOpenedMap_.end()) {
63         g_SessionOpenedMap_.insert(std::pair<int32_t, int64_t>(sessionId, (int64_t) 1));
64     } else {
65         iter->second = iter->second + 1;
66     }
67 
68     return SESSION_ACCEPTED;
69 }
70 
OnSessionClosed(int32_t sessionId)71 void SoftBusSessionListener::OnSessionClosed(int32_t sessionId)
72 {
73     ACCESSTOKEN_LOG_DEBUG(LABEL, "OnSessionClosed");
74 
75     // clear sessionId state
76     std::lock_guard<std::mutex> guard(g_SessionMutex_);
77     auto iter = g_SessionOpenedMap_.find(sessionId);
78     if (iter != g_SessionOpenedMap_.end()) {
79         g_SessionOpenedMap_.erase(iter);
80     }
81 }
82 
OnMessageReceived(int32_t sessionId,const void * data,uint32_t dataLen)83 void SoftBusSessionListener::OnMessageReceived(int32_t sessionId, const void *data, uint32_t dataLen)
84 {
85     (void)data;
86     ACCESSTOKEN_LOG_DEBUG(LABEL, "OnMessageReceived: data length = %{public}u", dataLen);
87 }
88 
OnBytesReceived(int32_t sessionId,const void * data,uint32_t dataLen)89 void SoftBusSessionListener::OnBytesReceived(int32_t sessionId, const void *data, uint32_t dataLen)
90 {
91     if ((sessionId == Constant::INVALID_SESSION) || (dataLen == 0) ||
92         (dataLen > MAX_ONBYTES_RECEIVED_DATA_LEN) || (data == nullptr)) {
93         ACCESSTOKEN_LOG_ERROR(LABEL, "params invalid, data length: %{public}u", dataLen);
94         return;
95     }
96 
97     int32_t len = SESSION_NAME_MAXLENGTH + 1;
98     char contents[len];
99     int32_t resultCode = ::GetPeerDeviceId(sessionId, contents, len);
100     if (resultCode != Constant::SUCCESS) {
101         ACCESSTOKEN_LOG_ERROR(LABEL, "GetPeerDeviceId, failed, result: %{public}d", resultCode);
102         return;
103     }
104     std::string networkId(contents);
105     ACCESSTOKEN_LOG_INFO(LABEL, "data length = %{public}u", dataLen);
106     auto channel = RemoteCommandManager::GetInstance().GetExecutorChannel(networkId);
107     if (channel == nullptr) {
108         ACCESSTOKEN_LOG_ERROR(LABEL, "GetExecutorChannel failed");
109         return;
110     }
111     channel->HandleDataReceived(sessionId, static_cast<unsigned char *>(const_cast<void *>(data)), dataLen);
112 }
113 
GetSessionState(int32_t sessionId)114 int64_t SoftBusSessionListener::GetSessionState(int32_t sessionId)
115 {
116     // get session state
117     std::lock_guard<std::mutex> guard(g_SessionMutex_);
118     auto iter = g_SessionOpenedMap_.find(sessionId);
119     if (iter == g_SessionOpenedMap_.end()) {
120         return STATE_NOTFOUND;
121     }
122     return iter->second;
123 }
124 
DeleteSessionIdFromMap(int32_t sessionId)125 void SoftBusSessionListener::DeleteSessionIdFromMap(int32_t sessionId)
126 {
127     ACCESSTOKEN_LOG_DEBUG(LABEL, "DeleteSessionIdFromMap");
128     // delete sessionId in map
129     std::lock_guard<std::mutex> guard(g_SessionMutex_);
130     auto iter = g_SessionOpenedMap_.find(sessionId);
131     if (iter != g_SessionOpenedMap_.end()) {
132         g_SessionOpenedMap_.erase(iter);
133     }
134 }
135 } // namespace AccessToken
136 } // namespace Security
137 } // namespace OHOS
138