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 #include <random>
16
17 #include "securec.h"
18
19 #include "anonymous_string.h"
20 #include "auth_manager.h"
21 #include "constants.h"
22 #include "device_manager_errno.h"
23 #include "device_manager_log.h"
24 #include "dm_ability_manager.h"
25 #include "encrypt_utils.h"
26 #include "ipc_server_adapter.h"
27 #include "ipc_server_listener.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 IMPLEMENT_SINGLE_INSTANCE(IpcServerAdapter);
32
CheckParamValid(nlohmann::json & extraJson,const DmAppImageInfo & imageInfo)33 int32_t IpcServerAdapter::CheckParamValid(nlohmann::json &extraJson, const DmAppImageInfo &imageInfo)
34 {
35 if (!extraJson.contains(APP_NAME_KEY) ||
36 !extraJson.contains(APP_DESCRIPTION_KEY) ||
37 !extraJson.contains(AUTH_TYPE)) {
38 DMLOG(DM_LOG_ERROR, "Invalid para");
39 return DEVICEMANAGER_INVALID_VALUE;
40 }
41
42 std::string appName = extraJson[APP_NAME_KEY];
43 std::string appDescription = extraJson[APP_DESCRIPTION_KEY];
44
45 if (appName.empty() || appDescription.empty()) {
46 DMLOG(DM_LOG_ERROR, "Invalid app image info");
47 return DEVICEMANAGER_INVALID_VALUE;
48 }
49 if (extraJson[AUTH_TYPE] != AUTH_TYPE_PIN) {
50 DMLOG(DM_LOG_ERROR, "invalid auth type, only support pin auth");
51 return DEVICEMANAGER_INVALID_VALUE;
52 }
53 return DEVICEMANAGER_OK;
54 }
55
GenRandInt(int32_t randMin,int32_t randMax)56 int32_t IpcServerAdapter::GenRandInt(int32_t randMin, int32_t randMax)
57 {
58 std::random_device randDevice;
59 std::mt19937 genRand(randDevice());
60 std::uniform_int_distribution<int> disRand(randMin, randMax);
61 return disRand(genRand);
62 }
63
ModuleInit()64 int32_t IpcServerAdapter::ModuleInit()
65 {
66 if (SoftbusAdapter::Init() != DEVICEMANAGER_OK) {
67 DMLOG(DM_LOG_ERROR, "softbus adapter init failed");
68 return DEVICEMANAGER_INIT_FAILED;
69 }
70 if (HichainConnector::GetInstance().Init() != DEVICEMANAGER_OK) {
71 DMLOG(DM_LOG_ERROR, "hichain connector init failed");
72 return DEVICEMANAGER_INIT_FAILED;
73 }
74 return DEVICEMANAGER_OK;
75 }
76
GetTrustedDeviceList(std::string & pkgName,std::string & extra,DmDeviceInfo ** info,int32_t * infoNum)77 int32_t IpcServerAdapter::GetTrustedDeviceList(std::string &pkgName, std::string &extra,
78 DmDeviceInfo **info, int32_t *infoNum)
79 {
80 if (info == nullptr || infoNum == nullptr) {
81 return DEVICEMANAGER_NULLPTR;
82 }
83 DMLOG(DM_LOG_INFO, "In, pkgName: %s", pkgName.c_str());
84 NodeBasicInfo *nodeInfo = nullptr;
85 *info = nullptr;
86 *infoNum = 0;
87 int32_t ret = SoftbusAdapter::GetTrustDevices(pkgName, &nodeInfo, infoNum);
88 if (ret != DEVICEMANAGER_OK || *infoNum <= 0 || nodeInfo == nullptr) {
89 DMLOG(DM_LOG_ERROR, "GetTrustDevices errCode:%d, num:%d", ret, *infoNum);
90 return ret;
91 }
92 *info = (DmDeviceInfo *)malloc(sizeof(DmDeviceInfo) * (*infoNum));
93 if (*info == nullptr) {
94 FreeNodeInfo(nodeInfo);
95 return DEVICEMANAGER_MALLOC_ERROR;
96 }
97 for (int32_t i = 0; i < *infoNum; ++i) {
98 NodeBasicInfo *nodeBasicInfo = nodeInfo + i;
99 DmDeviceInfo *deviceInfo = *info + i;
100 if (memcpy_s(deviceInfo->deviceId, sizeof(deviceInfo->deviceId), nodeBasicInfo->networkId,
101 std::min(sizeof(deviceInfo->deviceId), sizeof(nodeBasicInfo->networkId))) != DEVICEMANAGER_OK) {
102 DMLOG(DM_LOG_ERROR, "memcpy failed");
103 }
104 if (memcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), nodeBasicInfo->deviceName,
105 std::min(sizeof(deviceInfo->deviceName), sizeof(nodeBasicInfo->deviceName))) != DEVICEMANAGER_OK) {
106 DMLOG(DM_LOG_ERROR, "memcpy failed");
107 }
108 deviceInfo->deviceTypeId = (DMDeviceType)nodeBasicInfo->deviceTypeId;
109 }
110 FreeNodeInfo(nodeInfo);
111 DMLOG(DM_LOG_INFO, "success, pkgName:%s, deviceCount %d", pkgName.c_str(), *infoNum);
112 return DEVICEMANAGER_OK;
113 }
114
StartDeviceDiscovery(std::string & pkgName,DmSubscribeInfo & dmSubscribeInfo)115 int32_t IpcServerAdapter::StartDeviceDiscovery(std::string &pkgName, DmSubscribeInfo &dmSubscribeInfo)
116 {
117 DMLOG(DM_LOG_INFO, "In, pkgName: %s, subscribeId %d", pkgName.c_str(),
118 (int32_t)dmSubscribeInfo.subscribeId);
119
120 DMLOG(DM_LOG_INFO, "capability: %s", dmSubscribeInfo.capability);
121 SubscribeInfo subscribeInfo;
122
123 subscribeInfo.subscribeId = dmSubscribeInfo.subscribeId;
124 subscribeInfo.mode = (DiscoverMode)dmSubscribeInfo.mode;
125 subscribeInfo.medium = (ExchanageMedium)dmSubscribeInfo.medium;
126 subscribeInfo.freq = (ExchangeFreq)dmSubscribeInfo.freq;
127 subscribeInfo.isSameAccount = dmSubscribeInfo.isSameAccount;
128 subscribeInfo.isWakeRemote = dmSubscribeInfo.isWakeRemote;
129 subscribeInfo.capability = dmSubscribeInfo.capability;
130 subscribeInfo.capabilityData = nullptr;
131 subscribeInfo.dataLen = 0;
132 return SoftbusAdapter::StartDiscovery(pkgName, &subscribeInfo);
133 }
134
StopDiscovery(std::string & pkgName,uint16_t subscribeId)135 int32_t IpcServerAdapter::StopDiscovery(std::string &pkgName, uint16_t subscribeId)
136 {
137 DMLOG(DM_LOG_INFO, "In, pkgName: %s, subscribeId %d", pkgName.c_str(), (int32_t)subscribeId);
138 return SoftbusAdapter::StopDiscovery(pkgName, subscribeId);
139 }
140
AuthenticateDevice(std::string & pkgName,const DmDeviceInfo & deviceInfo,const DmAppImageInfo & imageInfo,std::string & extra)141 int32_t IpcServerAdapter::AuthenticateDevice(std::string &pkgName, const DmDeviceInfo &deviceInfo,
142 const DmAppImageInfo &imageInfo, std::string &extra)
143 {
144 if (pkgName.empty() || extra.empty()) {
145 DMLOG(DM_LOG_ERROR, "invalid para");
146 return DEVICEMANAGER_INVALID_VALUE;
147 }
148 nlohmann::json jsonObject = nlohmann::json::parse(extra, nullptr, false);
149 if (jsonObject.is_discarded()) {
150 DMLOG(DM_LOG_ERROR, "AuthenticateDevice extra jsonStr error");
151 return DEVICEMANAGER_INVALID_VALUE;
152 }
153 int32_t ret = CheckParamValid(jsonObject, imageInfo);
154 if (ret != DEVICEMANAGER_OK) {
155 DMLOG(DM_LOG_ERROR, "AuthenticateDevice para invalid, ret %d", ret);
156 return ret;
157 }
158 DMLOG(DM_LOG_INFO, "AuthenticateDevice In, pkgName: %s, deviceId %s", pkgName.c_str(),
159 GetAnonyString(deviceInfo.deviceId).c_str());
160
161 AuthManager::GetInstance().AuthDeviceGroup(pkgName, deviceInfo, imageInfo, extra);
162 return DEVICEMANAGER_OK;
163 }
164
CheckAuthentication(std::string & authPara)165 int32_t IpcServerAdapter::CheckAuthentication(std::string &authPara)
166 {
167 if (authPara.empty()) {
168 DMLOG(DM_LOG_INFO, " DeviceManagerIpcAdapter::CheckAuthentication check authPara failed");
169 return DEVICEMANAGER_INVALID_VALUE;
170 }
171 DMLOG(DM_LOG_INFO, " DeviceManagerIpcAdapter::CheckAuthentication");
172 return AuthManager::GetInstance().CheckAuthentication(authPara);
173 }
174
GetAuthenticationParam(std::string & pkgName,DmAuthParam & authParam)175 int32_t IpcServerAdapter::GetAuthenticationParam(std::string &pkgName, DmAuthParam &authParam)
176 {
177 if (pkgName.empty()) {
178 DMLOG(DM_LOG_ERROR, "invalid para");
179 return DEVICEMANAGER_INVALID_VALUE;
180 }
181
182 DmAbilityManager::GetInstance().StartAbilityDone();
183 AuthManager::GetInstance().GetAuthenticationParam(authParam);
184 return DEVICEMANAGER_OK;
185 }
186
SetUserOperation(std::string & pkgName,int32_t action)187 int32_t IpcServerAdapter::SetUserOperation(std::string &pkgName, int32_t action)
188 {
189 if (pkgName.empty()) {
190 DMLOG(DM_LOG_ERROR, "invalid para");
191 return DEVICEMANAGER_INVALID_VALUE;
192 }
193
194 AuthManager::GetInstance().OnUserOperate(action);
195 return SUCCESS;
196 }
197 } // namespace DistributedHardware
198 } // namespace OHOS
199