• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <string>
17 #include <algorithm>
18 #include <locale>
19 #include <codecvt>
20 #include "profile_utils.h"
21 #include "distributed_device_profile_log.h"
22 #include "distributed_device_profile_errors.h"
23 #include "rdb_errno.h"
24 #include "device_manager.h"
25 #include "nlohmann/json.hpp"
26 
27 namespace OHOS {
28 namespace DistributedDeviceProfile {
29 using namespace OHOS::DistributedHardware;
30 
31 namespace {
32     const std::string TAG = "ProfileUtils";
33 }
34 
GetAnonyString(const std::string & value)35 std::string ProfileUtils::GetAnonyString(const std::string& value)
36 {
37     constexpr size_t INT32_SHORT_ID_LENGTH = 20;
38     constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
39     constexpr size_t INT32_MIN_ID_LENGTH = 3;
40     std::string res;
41     std::string tmpStr("******");
42     size_t strLen = value.length();
43     if (strLen < INT32_MIN_ID_LENGTH) {
44         return tmpStr;
45     }
46 
47     if (strLen <= INT32_SHORT_ID_LENGTH) {
48         res += value[0];
49         res += tmpStr;
50         res += value[strLen - 1];
51     } else {
52         res.append(value, 0, INT32_PLAINTEXT_LENGTH);
53         res += tmpStr;
54         res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
55     }
56     return res;
57 }
58 
GetOnlineDevices()59 std::vector<std::string> ProfileUtils::GetOnlineDevices()
60 {
61     std::vector<std::string> targetDevices;
62     std::vector<DmDeviceInfo> allOnlineDeviceInfos;
63     int32_t result = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", allOnlineDeviceInfos);
64     if (result != DP_SUCCESS || allOnlineDeviceInfos.empty()) {
65         HILOGE("GetTrustedDeviceList Failed!");
66         return {};
67     }
68     for (const DmDeviceInfo& dmDeviceInfo : allOnlineDeviceInfos) {
69         targetDevices.push_back(dmDeviceInfo.networkId);
70     }
71     return targetDevices;
72 }
73 
GetLocalUdidFromDM()74 std::string ProfileUtils::GetLocalUdidFromDM()
75 {
76     std::string udid = "";
77     if (DeviceManager::GetInstance().GetLocalDeviceId(PKG_NAME, udid) != DP_SUCCESS) {
78         HILOGE("Get local udid fail from DM!");
79         return "";
80     }
81     return udid;
82 }
83 
FilterOnlineDevices(const std::vector<std::string> & deviceList)84 std::vector<std::string> ProfileUtils::FilterOnlineDevices(const std::vector<std::string>& deviceList)
85 {
86     if (deviceList.size() == 0 || deviceList.size() > MAX_DEVICE_SIZE) {
87         HILOGE("This deviceList size is invalid, size: %{public}zu!", deviceList.size());
88         return {};
89     }
90     std::vector<DmDeviceInfo> allOnlineDeviceInfos;
91     int32_t result = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", allOnlineDeviceInfos);
92     if (result != DP_SUCCESS || allOnlineDeviceInfos.empty()) {
93         HILOGE("GetTrustedDeviceList Failed!");
94         return {};
95     }
96     std::vector<std::string> targetDevices;
97     for (const DmDeviceInfo& dmDeviceInfo : allOnlineDeviceInfos) {
98         if (std::find(deviceList.begin(), deviceList.end(), dmDeviceInfo.networkId) == deviceList.end()) {
99             HILOGE("This device is not online, networkId: %s!",
100                    ProfileUtils::GetAnonyString(dmDeviceInfo.networkId).c_str());
101             continue;
102         }
103         targetDevices.push_back(dmDeviceInfo.networkId);
104     }
105     return targetDevices;
106 }
107 
GetProfileType(const std::string & key)108 ProfileType ProfileUtils::GetProfileType(const std::string& key)
109 {
110     if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
111         HILOGE("This key is invalid, value: %s!", key.c_str());
112         return ProfileType::PROFILE_TYPE_MIN;
113     }
114     ProfileType profileType = ProfileType::PROFILE_TYPE_MIN;
115     if (StartsWith(key, DEV_PREFIX)) {
116         profileType = ProfileType::DEVICE_PROFILE;
117     }
118     if (StartsWith(key, SVR_PREFIX)) {
119         profileType = ProfileType::SERVICE_PROFILE;
120     }
121     if (StartsWith(key, CHAR_PREFIX)) {
122         profileType = ProfileType::CHAR_PROFILE;
123     }
124     return profileType;
125 }
126 
StartsWith(const std::string & str,const std::string prefix)127 bool ProfileUtils::StartsWith(const std::string& str, const std::string prefix)
128 {
129     return (str.find(prefix, 0) == 0);
130 }
131 
GetProfileKey(const std::string & dbKey)132 std::string ProfileUtils::GetProfileKey(const std::string& dbKey)
133 {
134     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
135         return "";
136     }
137     std::size_t pos = dbKey.find_last_of("#");
138     return dbKey.substr(0, pos);
139 }
140 
GetDeviceIdByDBKey(const std::string & dbKey)141 std::string ProfileUtils::GetDeviceIdByDBKey(const std::string& dbKey)
142 {
143     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
144         return "";
145     }
146     std::vector<std::string> res;
147     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
148         return "";
149     }
150     if (res.size() < NUM_1) {
151         return "";
152     }
153     return res[NUM_1];
154 }
155 
GetServiceNameByDBKey(const std::string & dbKey)156 std::string ProfileUtils::GetServiceNameByDBKey(const std::string& dbKey)
157 {
158     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
159         return "";
160     }
161     std::vector<std::string> res;
162     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
163         return "";
164     }
165     if (res.size() < NUM_2) {
166         return "";
167     }
168     return res[NUM_2];
169 }
170 
GetCharKeyByDBKey(const std::string & dbKey)171 std::string ProfileUtils::GetCharKeyByDBKey(const std::string& dbKey)
172 {
173     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
174         return "";
175     }
176     std::vector<std::string> res;
177     if (SplitString(dbKey, SEPARATOR, res) != DP_SUCCESS) {
178         return "";
179     }
180     if (res.size() < NUM_3) {
181         return "";
182     }
183     return res[NUM_3];
184 }
185 
SplitString(const std::string & str,const std::string & splits,std::vector<std::string> & res)186 int32_t ProfileUtils::SplitString(const std::string& str, const std::string& splits, std::vector<std::string>& res)
187 {
188     if (str == "") {
189         return DP_INVALID_PARAMS;
190     }
191     std::string strs = str + splits;
192     size_t pos = strs.find(splits);
193     int32_t step = splits.size();
194 
195     while (pos != strs.npos) {
196         std::string temp = strs.substr(0, pos);
197         res.push_back(temp);
198         strs = strs.substr(pos + step, strs.size());
199         pos = strs.find(splits);
200     }
201     return DP_SUCCESS;
202 }
203 
IsKeyValid(const std::string & key)204 bool ProfileUtils::IsKeyValid(const std::string& key)
205 {
206     if (key.length() == 0 || key.length() > MAX_STRING_LEN) {
207         return false;
208     }
209     size_t found = key.find(SEPARATOR);
210     if (found != std::string::npos) {
211         return false;
212     }
213     return true;
214 }
215 
GenerateDeviceProfileKey(const std::string & deviceId)216 std::string ProfileUtils::GenerateDeviceProfileKey(const std::string& deviceId)
217 {
218     return DEV_PREFIX + SEPARATOR + deviceId;
219 }
220 
GenerateServiceProfileKey(const std::string & deviceId,const std::string & serviceName)221 std::string ProfileUtils::GenerateServiceProfileKey(const std::string& deviceId, const std::string& serviceName)
222 {
223     return SVR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName;
224 }
225 
GenerateCharProfileKey(const std::string & deviceId,const std::string & serviceName,const std::string & charKey)226 std::string ProfileUtils::GenerateCharProfileKey(const std::string& deviceId, const std::string& serviceName,
227     const std::string& charKey)
228 {
229     return CHAR_PREFIX + SEPARATOR + deviceId + SEPARATOR + serviceName + SEPARATOR + charKey;
230 }
231 
TrustDeviceProfileToEntries(const TrustDeviceProfile & profile,ValuesBucket & values)232 int32_t ProfileUtils::TrustDeviceProfileToEntries(const TrustDeviceProfile& profile, ValuesBucket& values)
233 {
234     values.PutString(DEVICE_ID, profile.GetDeviceId());
235     values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
236     values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
237     values.PutInt(STATUS, profile.GetStatus());
238     return DP_SUCCESS;
239 }
240 
AccessControlProfileToEntries(const AccessControlProfile & profile,ValuesBucket & values)241 int32_t ProfileUtils::AccessControlProfileToEntries(const AccessControlProfile& profile, ValuesBucket& values)
242 {
243     values.PutLong(ACCESS_CONTROL_ID, profile.GetAccessControlId());
244     values.PutLong(ACCESSER_ID, profile.GetAccesserId());
245     values.PutLong(ACCESSEE_ID, profile.GetAccesseeId());
246     values.PutString(TRUST_DEVICE_ID, profile.GetTrustDeviceId());
247     values.PutInt(AUTHENTICATION_TYPE, profile.GetAuthenticationType());
248     values.PutInt(DEVICE_ID_TYPE, profile.GetDeviceIdType());
249     values.PutString(DEVICE_ID_HASH, profile.GetDeviceIdHash());
250     values.PutInt(BIND_TYPE, profile.GetBindType());
251     values.PutString(SESSION_KEY, profile.GetSessionKey());
252     values.PutInt(LAST_AUTH_TIME, profile.GetLastAuthTime());
253     values.PutInt(VALID_PERIOD, profile.GetValidPeriod());
254     values.PutInt(STATUS, profile.GetStatus());
255     values.PutInt(BIND_LEVEL, profile.GetBindLevel());
256     return DP_SUCCESS;
257 }
258 
AccesserToEntries(const AccessControlProfile & aclProfile,ValuesBucket & values)259 int32_t ProfileUtils::AccesserToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
260 {
261     Accesser accesser = aclProfile.GetAccesser();
262     values.PutLong(ACCESSER_ID, accesser.GetAccesserId());
263     values.PutString(ACCESSER_DEVICE_ID, accesser.GetAccesserDeviceId());
264     values.PutInt(ACCESSER_USER_ID, accesser.GetAccesserUserId());
265     values.PutString(ACCESSER_ACCOUNT_ID, accesser.GetAccesserAccountId());
266     values.PutLong(ACCESSER_TOKEN_ID, accesser.GetAccesserTokenId());
267     values.PutString(ACCESSER_BUNDLE_NAME, accesser.GetAccesserBundleName());
268     values.PutString(ACCESSER_HAP_SIGNATURE, accesser.GetAccesserHapSignature());
269     values.PutInt(ACCESSER_BIND_LEVEL, accesser.GetAccesserBindLevel());
270     return DP_SUCCESS;
271 }
272 
AccesseeToEntries(const AccessControlProfile & aclProfile,ValuesBucket & values)273 int32_t ProfileUtils::AccesseeToEntries(const AccessControlProfile& aclProfile, ValuesBucket& values)
274 {
275     Accessee accessee = aclProfile.GetAccessee();
276     values.PutLong(ACCESSEE_ID, accessee.GetAccesseeId());
277     values.PutString(ACCESSEE_DEVICE_ID, accessee.GetAccesseeDeviceId());
278     values.PutInt(ACCESSEE_USER_ID, accessee.GetAccesseeUserId());
279     values.PutString(ACCESSEE_ACCOUNT_ID, accessee.GetAccesseeAccountId());
280     values.PutLong(ACCESSEE_TOKEN_ID, accessee.GetAccesseeTokenId());
281     values.PutString(ACCESSEE_BUNDLE_NAME, accessee.GetAccesseeBundleName());
282     values.PutString(ACCESSEE_HAP_SIGNATURE, accessee.GetAccesseeHapSignature());
283     values.PutInt(ACCESSEE_BIND_LEVEL, accessee.GetAccesseeBindLevel());
284     return DP_SUCCESS;
285 }
286 
DeviceProfileToEntries(const DeviceProfile & profile,std::map<std::string,std::string> & values)287 int32_t ProfileUtils::DeviceProfileToEntries(const DeviceProfile& profile, std::map<std::string, std::string>& values)
288 {
289     std::string deviceProfileKey = GenerateDeviceProfileKey(profile.GetDeviceId());
290     values[GenerateDBKey(deviceProfileKey, DEVICE_ID)] = profile.GetDeviceId();
291     values[GenerateDBKey(deviceProfileKey, DEVICE_TYPE_ID)] = std::to_string(profile.GetDeviceTypeId());
292     values[GenerateDBKey(deviceProfileKey, DEVICE_TYPE_NAME)] = profile.GetDeviceTypeName();
293     values[GenerateDBKey(deviceProfileKey, DEVICE_NAME)] = profile.GetDeviceName();
294     values[GenerateDBKey(deviceProfileKey, MANUFACTURE_NAME)] = profile.GetManufactureName();
295     values[GenerateDBKey(deviceProfileKey, DEVICE_MODEL)] = profile.GetDeviceModel();
296     values[GenerateDBKey(deviceProfileKey, SERIAL_NUMBER_ID)] = profile.GetSerialNumberId();
297     values[GenerateDBKey(deviceProfileKey, STORAGE_CAPACITY)] = std::to_string(profile.GetStorageCapability());
298     values[GenerateDBKey(deviceProfileKey, OS_SYS_CAPACITY)] = profile.GetOsSysCap();
299     values[GenerateDBKey(deviceProfileKey, OS_API_LEVEL)] = std::to_string(profile.GetOsApiLevel());
300     values[GenerateDBKey(deviceProfileKey, OS_VERSION)] = profile.GetOsVersion();
301     values[GenerateDBKey(deviceProfileKey, OS_TYPE)] = std::to_string(profile.GetOsType());
302     return DP_SUCCESS;
303 }
304 
ServiceProfileToEntries(const ServiceProfile & profile,std::map<std::string,std::string> & values)305 int32_t ProfileUtils::ServiceProfileToEntries(const ServiceProfile& profile, std::map<std::string, std::string>& values)
306 {
307     std::string serviceProfileKey = GenerateServiceProfileKey(profile.GetDeviceId(), profile.GetServiceName());
308     values[GenerateDBKey(serviceProfileKey, SERVICE_NAME)] = profile.GetServiceName();
309     values[GenerateDBKey(serviceProfileKey, SERVICE_TYPE)] = profile.GetServiceType();
310     return DP_SUCCESS;
311 }
312 
CharacteristicProfileToEntries(const CharacteristicProfile & profile,std::map<std::string,std::string> & values)313 int32_t ProfileUtils::CharacteristicProfileToEntries(const CharacteristicProfile& profile,
314                                                      std::map<std::string, std::string>& values)
315 {
316     std::string charProfileKey = GenerateCharProfileKey(profile.GetDeviceId(), profile.GetServiceName(),
317         profile.GetCharacteristicKey());
318     values[GenerateDBKey(charProfileKey, CHARACTERISTIC_KEY)] = profile.GetCharacteristicKey();
319     values[GenerateDBKey(charProfileKey, CHARACTERISTIC_VALUE)] = profile.GetCharacteristicValue();
320     return DP_SUCCESS;
321 }
322 
EntriesToTrustDeviceProfile(const ValuesBucket & values,TrustDeviceProfile & profile)323 int32_t ProfileUtils::EntriesToTrustDeviceProfile(const ValuesBucket& values, TrustDeviceProfile& profile)
324 {
325     ValueObject valueObject;
326     std::string strValue = "";
327     int32_t intValue = 0;
328     if (values.GetObject(DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
329         profile.SetDeviceId(strValue);
330     }
331     if (values.GetObject(DEVICE_ID_HASH, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
332         profile.SetDeviceIdHash(strValue);
333     }
334     if (values.GetObject(DEVICE_TYPE_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
335         profile.SetDeviceIdType(intValue);
336     }
337     if (values.GetObject(STATUS, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
338         profile.SetStatus(intValue);
339     }
340     return DP_SUCCESS;
341 }
342 
EntriesToAccessControlProfile(const ValuesBucket & values,AccessControlProfile & profile)343 int32_t ProfileUtils::EntriesToAccessControlProfile(const ValuesBucket& values, AccessControlProfile& profile)
344 {
345     std::string strValue = "";
346     int32_t intValue = 0;
347     int64_t int64Value = 0;
348     if (GetLongValue(values, ACCESS_CONTROL_ID, int64Value)) {
349         profile.SetAccessControlId(int64Value);
350     }
351     if (GetLongValue(values, ACCESSER_ID, int64Value)) {
352         profile.SetAccesserId(int64Value);
353     }
354     if (GetLongValue(values, ACCESSEE_ID, int64Value)) {
355         profile.SetAccesseeId(int64Value);
356     }
357     if (GetStringValue(values, SESSION_KEY, strValue)) {
358         profile.SetSessionKey(strValue);
359     }
360     if (GetIntValue(values, BIND_TYPE, intValue)) {
361         profile.SetBindType(intValue);
362     }
363     if (GetIntValue(values, AUTHENTICATION_TYPE, intValue)) {
364         profile.SetAuthenticationType(intValue);
365     }
366     if (GetIntValue(values, BIND_LEVEL, intValue)) {
367         profile.SetBindLevel(intValue);
368     }
369     if (GetIntValue(values, STATUS, intValue)) {
370         profile.SetStatus(intValue);
371     }
372     if (GetIntValue(values, VALID_PERIOD, intValue)) {
373         profile.SetValidPeriod(intValue);
374     }
375     if (GetIntValue(values, LAST_AUTH_TIME, intValue)) {
376         profile.SetLastAuthTime(intValue);
377     }
378     if (GetStringValue(values, TRUST_DEVICE_ID, strValue)) {
379         profile.SetTrustDeviceId(strValue);
380     }
381     if (GetIntValue(values, DEVICE_TYPE_ID, intValue)) {
382         profile.SetDeviceIdType(intValue);
383     }
384     if (GetStringValue(values, DEVICE_ID_HASH, strValue)) {
385         profile.SetDeviceIdHash(strValue);
386     }
387     return DP_SUCCESS;
388 }
389 
EntriesToAccesser(const ValuesBucket & values,Accesser & accesser)390 int32_t ProfileUtils::EntriesToAccesser(const ValuesBucket& values, Accesser& accesser)
391 {
392     ValueObject valueObject;
393     std::string strValue = "";
394     int32_t intValue = 0;
395     int64_t int64Value = 0;
396     if (values.GetObject(ACCESSER_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
397         accesser.SetAccesserId(int64Value);
398     }
399     if (values.GetObject(ACCESSER_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
400         accesser.SetAccesserDeviceId(strValue);
401     }
402     if (values.GetObject(ACCESSER_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
403         accesser.SetAccesserUserId(intValue);
404     }
405     if (values.GetObject(ACCESSER_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
406         accesser.SetAccesserAccountId(strValue);
407     }
408     if (values.GetObject(ACCESSER_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
409         accesser.SetAccesserTokenId(int64Value);
410     }
411     if (values.GetObject(ACCESSER_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
412         accesser.SetAccesserBundleName(strValue);
413     }
414     if (values.GetObject(ACCESSER_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
415         accesser.SetAccesserBindLevel(intValue);
416     }
417     return DP_SUCCESS;
418 }
419 
EntriesToAccessee(const ValuesBucket & values,Accessee & accessee)420 int32_t ProfileUtils::EntriesToAccessee(const ValuesBucket& values, Accessee& accessee)
421 {
422     ValueObject valueObject;
423     std::string strValue = "";
424     int32_t intValue = 0;
425     int64_t int64Value = 0;
426     if (values.GetObject(ACCESSEE_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
427         accessee.SetAccesseeId(int64Value);
428     }
429     if (values.GetObject(ACCESSEE_DEVICE_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
430         accessee.SetAccesseeDeviceId(strValue);
431     }
432     if (values.GetObject(ACCESSEE_USER_ID, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
433         accessee.SetAccesseeUserId(intValue);
434     }
435     if (values.GetObject(ACCESSEE_ACCOUNT_ID, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
436         accessee.SetAccesseeAccountId(strValue);
437     }
438     if (values.GetObject(ACCESSEE_TOKEN_ID, valueObject) && valueObject.GetLong(int64Value) == NativeRdb::E_OK) {
439         accessee.SetAccesseeTokenId(int64Value);
440     }
441     if (values.GetObject(ACCESSEE_BUNDLE_NAME, valueObject) && valueObject.GetString(strValue) == NativeRdb::E_OK) {
442         accessee.SetAccesseeBundleName(strValue);
443     }
444     if (values.GetObject(ACCESSEE_BIND_LEVEL, valueObject) && valueObject.GetInt(intValue) == NativeRdb::E_OK) {
445         accessee.SetAccesseeBindLevel(intValue);
446     }
447     return DP_SUCCESS;
448 }
449 
EntriesToDeviceProfile(std::map<std::string,std::string> values,DeviceProfile & profile)450 int32_t ProfileUtils::EntriesToDeviceProfile(std::map<std::string, std::string> values, DeviceProfile& profile)
451 {
452     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
453         HILOGI("Entries size is invalid!");
454         return DP_INVALID_PARAMS;
455     }
456     auto propertiesMap = GetProfilePropertiesMap(values);
457     if (IsPropertyValid(propertiesMap, DEVICE_ID, MAX_STRING_LEN)) {
458         profile.SetDeviceId(propertiesMap[DEVICE_ID]);
459     }
460     if (IsPropertyValid(propertiesMap, DEVICE_TYPE_ID, static_cast<uint32_t>(DeviceIdType::MIN),
461         static_cast<uint32_t>(DeviceIdType::MAX))) {
462         uint32_t deviceTypeId = std::atoi(propertiesMap[DEVICE_TYPE_ID].c_str());
463         profile.SetDeviceTypeId(deviceTypeId);
464     }
465     if (IsPropertyValid(propertiesMap, DEVICE_TYPE_NAME, MAX_STRING_LEN)) {
466         profile.SetDeviceTypeName(propertiesMap[DEVICE_TYPE_NAME]);
467     }
468     if (IsPropertyValid(propertiesMap, DEVICE_NAME, MAX_STRING_LEN)) {
469         profile.SetDeviceName(propertiesMap[DEVICE_NAME]);
470     }
471     if (IsPropertyValid(propertiesMap, MANUFACTURE_NAME, MAX_STRING_LEN)) {
472         profile.SetManufactureName(propertiesMap[MANUFACTURE_NAME]);
473     }
474     if (IsPropertyValid(propertiesMap, DEVICE_MODEL, MAX_STRING_LEN)) {
475         profile.SetDeviceModel(propertiesMap[DEVICE_MODEL]);
476     }
477     if (IsPropertyValid(propertiesMap, SERIAL_NUMBER_ID, MAX_STRING_LEN)) {
478         profile.SetSerialNumberId(propertiesMap[SERIAL_NUMBER_ID]);
479     }
480     if (IsPropertyValid(propertiesMap, STORAGE_CAPACITY, MIN_STORAGE_KB, MAX_STORAGE_KB)) {
481         int64_t storageCapability = std::atoi(propertiesMap[STORAGE_CAPACITY].c_str());
482         profile.SetStorageCapability(storageCapability);
483     }
484     if (IsPropertyValid(propertiesMap, OS_SYS_CAPACITY, MAX_STRING_LEN)) {
485         profile.SetOsSysCap(propertiesMap[OS_SYS_CAPACITY]);
486     }
487     if (IsPropertyValid(propertiesMap, OS_API_LEVEL, MIN_OS_API_LEVEL, MAX_OS_API_LEVEL)) {
488         int32_t osApiLevel = std::atoi(propertiesMap[OS_API_LEVEL].c_str());
489         profile.SetOsApiLevel(osApiLevel);
490     }
491     if (IsPropertyValid(propertiesMap, OS_VERSION, MAX_STRING_LEN)) {
492         profile.SetOsVersion(propertiesMap[OS_VERSION]);
493     }
494     if (IsPropertyValid(propertiesMap, OS_TYPE, MIN_OS_TYPE, MAX_OS_TYPE)) {
495         int32_t osType = std::atoi(propertiesMap[OS_TYPE].c_str());
496         profile.SetOsType(osType);
497     }
498     return DP_SUCCESS;
499 }
500 
EntriesToServiceProfile(std::map<std::string,std::string> values,ServiceProfile & profile)501 int32_t ProfileUtils::EntriesToServiceProfile(std::map<std::string, std::string> values, ServiceProfile& profile)
502 {
503     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
504         HILOGI("Entries size is invalid!");
505         return DP_INVALID_PARAMS;
506     }
507     auto iter = values.begin();
508     profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
509     auto propertiesMap = GetProfilePropertiesMap(values);
510     if (propertiesMap.count(SERVICE_NAME) != 0 && 0 < values[SERVICE_NAME].length() &&
511         values[SERVICE_NAME].length() < MAX_STRING_LEN) {
512         profile.SetServiceName(propertiesMap[SERVICE_NAME]);
513     }
514     if (propertiesMap.count(SERVICE_TYPE) != 0 && 0 < propertiesMap[SERVICE_TYPE].length() &&
515         propertiesMap[SERVICE_TYPE].length() < MAX_STRING_LEN) {
516         profile.SetServiceType(propertiesMap[SERVICE_TYPE]);
517     }
518     return DP_SUCCESS;
519 }
520 
EntriesToCharProfile(std::map<std::string,std::string> values,CharacteristicProfile & profile)521 int32_t ProfileUtils::EntriesToCharProfile(std::map<std::string, std::string> values, CharacteristicProfile& profile)
522 {
523     if (values.empty() || values.size() > MAX_DB_RECORD_SIZE) {
524         HILOGI("Entries size is invalid!");
525         return DP_INVALID_PARAMS;
526     }
527     auto iter = values.begin();
528     profile.SetDeviceId(GetDeviceIdByDBKey(iter->first));
529     profile.SetServiceName(GetServiceNameByDBKey(iter->first));
530     auto propertiesMap = GetProfilePropertiesMap(values);
531     if (propertiesMap.count(CHARACTERISTIC_KEY) != 0 && 0 < propertiesMap[CHARACTERISTIC_KEY].length() &&
532         propertiesMap[CHARACTERISTIC_KEY].length() < MAX_STRING_LEN) {
533         profile.SetCharacteristicKey(propertiesMap[CHARACTERISTIC_KEY]);
534     }
535     if (propertiesMap.count(CHARACTERISTIC_VALUE) != 0 && 0 < propertiesMap[CHARACTERISTIC_VALUE].length() &&
536         propertiesMap[CHARACTERISTIC_VALUE].length() < MAX_STRING_LEN) {
537         profile.SetCharacteristicValue(propertiesMap[CHARACTERISTIC_VALUE]);
538     }
539     return DP_SUCCESS;
540 }
541 
GenerateDBKey(const std::string & profileKey,const std::string & profileProperty)542 std::string ProfileUtils::GenerateDBKey(const std::string& profileKey, const std::string& profileProperty)
543 {
544     return profileKey + SEPARATOR + profileProperty;
545 }
546 
GetProfileProperty(const std::string & dbKey)547 std::string ProfileUtils::GetProfileProperty(const std::string& dbKey)
548 {
549     if (dbKey.length() == 0 || dbKey.length() > MAX_STRING_LEN) {
550         return "";
551     }
552     std::size_t pos = dbKey.find_last_of("#");
553     return dbKey.substr(pos + 1);
554 }
555 
GetProfilePropertiesMap(std::map<std::string,std::string> dbEntries)556 std::map<std::string, std::string> ProfileUtils::GetProfilePropertiesMap(std::map<std::string, std::string> dbEntries)
557 {
558     std::map<std::string, std::string> propertiesMap;
559     for (const auto& item : dbEntries) {
560         std::string profileProperty = GetProfileProperty(item.first);
561         if (profileProperty.empty()) {
562             HILOGE("GetProfileProperty fail, %s!", item.first.c_str());
563             continue;
564         }
565         propertiesMap[profileProperty] = item.second;
566     }
567     return propertiesMap;
568 }
569 
toString(const std::u16string & str16)570 std::string ProfileUtils::toString(const std::u16string& str16)
571 {
572     return std::wstring_convert< std::codecvt_utf8_utf16<char16_t>, char16_t >{}.to_bytes(str16);
573 }
574 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,int32_t maxValue)575 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
576     int32_t maxValue)
577 {
578     if (propertyMap.count(property) != 0 && 0 < propertyMap.at(property).length() &&
579         propertyMap.at(property).length() < maxValue) {
580         return true;
581     }
582     return false;
583 }
584 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,int32_t minValue,int32_t maxValue)585 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
586    int32_t minValue, int32_t maxValue)
587 {
588     if (propertyMap.count(property) != 0 && minValue < std::atoi(propertyMap.at(property).c_str()) &&
589         std::atoi(propertyMap.at(property).c_str()) < maxValue) {
590         return true;
591     }
592     return false;
593 }
594 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,uint32_t minValue,uint32_t maxValue)595 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
596    uint32_t minValue, uint32_t maxValue)
597 {
598     if (propertyMap.count(property) != 0 && static_cast<int32_t>(minValue) < std::atoi(propertyMap.at(property).c_str())
599         && std::atoi(propertyMap.at(property).c_str()) < static_cast<int32_t>(maxValue)) {
600         return true;
601     }
602     return false;
603 }
604 
IsPropertyValid(const std::map<std::string,std::string> & propertyMap,const std::string & property,int64_t minValue,int64_t maxValue)605 bool ProfileUtils::IsPropertyValid(const std::map<std::string, std::string>& propertyMap, const std::string& property,
606    int64_t minValue, int64_t maxValue)
607 {
608     if (propertyMap.count(property) != 0 && minValue < std::atoi(propertyMap.at(property).c_str()) &&
609         std::atoi(propertyMap.at(property).c_str()) < maxValue) {
610         return true;
611     }
612     return false;
613 }
614 
GetIntValue(const ValuesBucket & values,const std::string & property,int32_t & value)615 bool ProfileUtils::GetIntValue(const ValuesBucket& values, const std::string& property, int32_t& value)
616 {
617     ValueObject valueObject;
618     if (values.GetObject(property, valueObject) && valueObject.GetInt(value) == NativeRdb::E_OK) {
619         return true;
620     }
621     return false;
622 }
623 
GetStringValue(const ValuesBucket & values,const std::string & property,std::string & value)624 bool ProfileUtils::GetStringValue(const ValuesBucket& values, const std::string& property, std::string& value)
625 {
626     ValueObject valueObject;
627     if (values.GetObject(property, valueObject) && valueObject.GetString(value) == NativeRdb::E_OK) {
628         return true;
629     }
630     return false;
631 }
632 
GetLongValue(const ValuesBucket & values,const std::string & property,int64_t & value)633 bool ProfileUtils::GetLongValue(const ValuesBucket& values, const std::string& property, int64_t& value)
634 {
635     ValueObject valueObject;
636     if (values.GetObject(property, valueObject) && valueObject.GetLong(value) == NativeRdb::E_OK) {
637         return true;
638     }
639     return false;
640 }
641 } // namespace DistributedDeviceProfile
642 } // namespace OHOS
643