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 #include "dm_kv_info.h"
16
17 #include "cJSON.h"
18 #include "dm_anonymous.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 constexpr const char* UDID_HASH_KEY = "udidHash";
23 constexpr const char* APP_ID_KEY = "appID";
24 constexpr const char* ANOY_DEVICE_ID_KEY = "anoyDeviceId";
25 constexpr const char* SALT_KEY = "salt";
26 constexpr const char* LAST_MODIFY_TIME_KEY = "lastModifyTime";
27
ConvertDmKVValueToJson(const DmKVValue & kvValue,std::string & result)28 void ConvertDmKVValueToJson(const DmKVValue &kvValue, std::string &result)
29 {
30 JsonObject jsonObj;
31 jsonObj[UDID_HASH_KEY] = kvValue.udidHash;
32 jsonObj[APP_ID_KEY] = kvValue.appID;
33 jsonObj[ANOY_DEVICE_ID_KEY] = kvValue.anoyDeviceId;
34 jsonObj[SALT_KEY] = kvValue.salt;
35 jsonObj[LAST_MODIFY_TIME_KEY] = kvValue.lastModifyTime;
36 result = SafetyDump(jsonObj);
37 }
38
ConvertJsonToDmKVValue(const std::string & result,DmKVValue & kvValue)39 void ConvertJsonToDmKVValue(const std::string &result, DmKVValue &kvValue)
40 {
41 if (result.empty()) {
42 return;
43 }
44 JsonObject resultJson(result);
45 if (resultJson.IsDiscarded()) {
46 return;
47 }
48 if (IsString(resultJson, UDID_HASH_KEY)) {
49 kvValue.udidHash = resultJson[UDID_HASH_KEY].Get<std::string>();
50 }
51 if (IsString(resultJson, APP_ID_KEY)) {
52 kvValue.appID = resultJson[APP_ID_KEY].Get<std::string>();
53 }
54 if (IsString(resultJson, ANOY_DEVICE_ID_KEY)) {
55 kvValue.anoyDeviceId = resultJson[ANOY_DEVICE_ID_KEY].Get<std::string>();
56 }
57 if (IsString(resultJson, SALT_KEY)) {
58 kvValue.salt = resultJson[SALT_KEY].Get<std::string>();
59 }
60 if (IsInt64(resultJson, LAST_MODIFY_TIME_KEY)) {
61 kvValue.lastModifyTime = resultJson[LAST_MODIFY_TIME_KEY].Get<int64_t>();
62 }
63 }
64 } // namespace DistributedHardware
65 } // namespace OHOS
66