• 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 "pin_holder_session.h"
17 
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_crypto.h"
21 #include "dm_log.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 std::shared_ptr<IPinholderSessionCallback> PinHolderSession::pinholderSessionCallback_ = nullptr;
26 std::mutex PinHolderSession::pinHolderSessionLock_;
PinHolderSession()27 PinHolderSession::PinHolderSession()
28 {
29     LOGD("PinHolderSession constructor.");
30 }
31 
~PinHolderSession()32 PinHolderSession::~PinHolderSession()
33 {
34     LOGD("PinHolderSession destructor.");
35 }
36 
RegisterSessionCallback(std::shared_ptr<IPinholderSessionCallback> callback)37 int32_t PinHolderSession::RegisterSessionCallback(std::shared_ptr<IPinholderSessionCallback> callback)
38 {
39     std::lock_guard<std::mutex> autoLock(pinHolderSessionLock_);
40     pinholderSessionCallback_ = callback;
41     return DM_OK;
42 }
43 
UnRegisterSessionCallback()44 int32_t PinHolderSession::UnRegisterSessionCallback()
45 {
46     std::lock_guard<std::mutex> autoLock(pinHolderSessionLock_);
47     pinholderSessionCallback_ = nullptr;
48     return DM_OK;
49 }
50 
OpenSessionServer(const PeerTargetId & targetId)51 int32_t PinHolderSession::OpenSessionServer(const PeerTargetId &targetId)
52 {
53     int32_t sessionId = -1;
54     ConnectionAddr addrInfo;
55     int32_t ret = GetAddrByTargetId(targetId, addrInfo);
56     if (ret != DM_OK) {
57         LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId);
58         return ret;
59     }
60     sessionId = ::OpenAuthSession(DM_PIN_HOLDER_SESSION_NAME, &addrInfo, 1, nullptr);
61     if (sessionId < 0) {
62         LOGE("[SOFTBUS]open session error, sessionId: %{public}d.", sessionId);
63         return sessionId;
64     }
65     LOGI("success. sessionId: %{public}d.", sessionId);
66     return sessionId;
67 }
68 
CloseSessionServer(int32_t sessionId)69 int32_t PinHolderSession::CloseSessionServer(int32_t sessionId)
70 {
71     LOGD("CloseSessionServer.");
72     ::CloseSession(sessionId);
73     return DM_OK;
74 }
75 
OnSessionOpened(int sessionId,int result)76 int PinHolderSession::OnSessionOpened(int sessionId, int result)
77 {
78     std::shared_ptr<IPinholderSessionCallback> tempCbk;
79     {
80         std::lock_guard<std::mutex> autoLock(pinHolderSessionLock_);
81         tempCbk = pinholderSessionCallback_;
82     }
83     LOGI("[SOFTBUS]OnSessionOpened sessionId: %{public}d", sessionId);
84     if (tempCbk == nullptr) {
85         LOGE("OnSessionOpened error, pinholderSessionCallback_ is nullptr.");
86         return ERR_DM_FAILED;
87     }
88     int32_t sessionSide = GetSessionSide(sessionId);
89     tempCbk->OnSessionOpened(sessionId, sessionSide, result);
90     LOGI("OnSessionOpened, success, sessionId: %{public}d.", sessionId);
91     return DM_OK;
92 }
93 
OnSessionClosed(int sessionId)94 void PinHolderSession::OnSessionClosed(int sessionId)
95 {
96     std::shared_ptr<IPinholderSessionCallback> tempCbk;
97     {
98         std::lock_guard<std::mutex> autoLock(pinHolderSessionLock_);
99         tempCbk = pinholderSessionCallback_;
100     }
101     LOGI("[SOFTBUS]OnSessionClosed sessionId: %{public}d", sessionId);
102     if (tempCbk == nullptr) {
103         LOGE("OnSessionClosed error, pinholderSessionCallback_ is nullptr.");
104         return;
105     }
106     tempCbk->OnSessionClosed(sessionId);
107     LOGI("OnSessionClosed, success, sessionId: %{public}d.", sessionId);
108     return;
109 }
110 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)111 void PinHolderSession::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
112 {
113     if (sessionId < 0 || data == nullptr || dataLen <= 0) {
114         LOGE("[SOFTBUS]fail to receive data from softbus with sessionId: %{public}d, dataLen: %{public}d.", sessionId,
115             dataLen);
116         return;
117     }
118     std::shared_ptr<IPinholderSessionCallback> tempCbk;
119     {
120         std::lock_guard<std::mutex> autoLock(pinHolderSessionLock_);
121         tempCbk = pinholderSessionCallback_;
122     }
123     if (tempCbk == nullptr) {
124         LOGE("OnBytesReceived error, pinholderSessionCallback_ is nullptr.");
125         return;
126     }
127     LOGI("start, sessionId: %{public}d, dataLen: %{public}d.", sessionId, dataLen);
128     std::string message = std::string(reinterpret_cast<const char *>(data), dataLen);
129     tempCbk->OnDataReceived(sessionId, message);
130     LOGI("OnBytesReceived, success, sessionId: %{public}d.", sessionId);
131     return;
132 }
133 
SendData(int32_t sessionId,const std::string & message)134 int32_t PinHolderSession::SendData(int32_t sessionId, const std::string &message)
135 {
136     JsonObject jsonObject(message);
137     if (jsonObject.IsDiscarded()) {
138         LOGE("extrasJson error, message: %{public}s.", GetAnonyString(message).c_str());
139         return ERR_DM_FAILED;
140     }
141     if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
142         LOGE("SoftbusSession::SendData err json string.");
143         return ERR_DM_FAILED;
144     }
145     int32_t msgType = jsonObject[TAG_MSG_TYPE].Get<int32_t>();
146     LOGI("start, msgType: %{public}d.", msgType);
147     int32_t ret = SendBytes(sessionId, message.c_str(), strlen(message.c_str()));
148     if (ret != DM_OK) {
149         LOGE("[SOFTBUS]SendBytes failed, ret: %{public}d.", ret);
150         return ret;
151     }
152     return ret;
153 }
154 
GetAddrByTargetId(const PeerTargetId & targetId,ConnectionAddr & addr)155 int32_t PinHolderSession::GetAddrByTargetId(const PeerTargetId &targetId, ConnectionAddr &addr)
156 {
157     if (!targetId.wifiIp.empty() && targetId.wifiIp.length() <= IP_STR_MAX_LEN) {
158         addr.type = ConnectionAddrType::CONNECTION_ADDR_WLAN;
159         if (memcpy_s(addr.info.ip.ip, IP_STR_MAX_LEN, targetId.wifiIp.c_str(), targetId.wifiIp.length()) != DM_OK) {
160             LOGE("copy wifi data failed.");
161             return ERR_DM_FAILED;
162         }
163         addr.info.ip.port = targetId.wifiPort;
164     } else if (!targetId.brMac.empty() && targetId.brMac.length() <= BT_MAC_LEN) {
165         addr.type = ConnectionAddrType::CONNECTION_ADDR_BR;
166         if (memcpy_s(addr.info.br.brMac, BT_MAC_LEN, targetId.brMac.c_str(), targetId.brMac.length()) != DM_OK) {
167             LOGE("copy br data failed.");
168             return ERR_DM_FAILED;
169         }
170     } else if (!targetId.bleMac.empty() && targetId.bleMac.length() <= BT_MAC_LEN) {
171         addr.type = ConnectionAddrType::CONNECTION_ADDR_BLE;
172         if (memcpy_s(addr.info.ble.bleMac, BT_MAC_LEN, targetId.bleMac.c_str(), targetId.bleMac.length()) != DM_OK) {
173             LOGE("copy ble data failed.");
174             return ERR_DM_FAILED;
175         }
176         if (!targetId.deviceId.empty()) {
177             Crypto::ConvertHexStringToBytes(addr.info.ble.udidHash, UDID_HASH_LEN,
178                 targetId.deviceId.c_str(), targetId.deviceId.length());
179         }
180     }
181     return DM_OK;
182 }
183 } // namespace DistributedHardware
184 } // namespace OHOS