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