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
16 #include "capability_utils.h"
17
18 #include "capability_info.h"
19 #include "constants.h"
20 #include "distributed_hardware_errno.h"
21 #include "distributed_hardware_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "CapabilityUtils"
27
GetCapabilityByValue(const std::string & value,std::shared_ptr<CapabilityInfo> & capPtr)28 int32_t CapabilityUtils::GetCapabilityByValue(const std::string &value, std::shared_ptr<CapabilityInfo> &capPtr)
29 {
30 capPtr = std::make_shared<CapabilityInfo>();
31 return capPtr->FromJsonString(value);
32 }
33
GetCapabilityKey(const std::string & deviceId,const std::string & dhId)34 std::string CapabilityUtils::GetCapabilityKey(const std::string &deviceId, const std::string &dhId)
35 {
36 return deviceId + RESOURCE_SEPARATOR + dhId;
37 }
38
IsCapKeyMatchDeviceId(const std::string & key,const std::string & deviceId)39 bool CapabilityUtils::IsCapKeyMatchDeviceId(const std::string &key, const std::string &deviceId)
40 {
41 std::size_t separatorPos = key.find(RESOURCE_SEPARATOR);
42 if (separatorPos == std::string::npos) {
43 return false;
44 }
45 std::string keyDevId = key.substr(0, separatorPos);
46 return keyDevId.compare(deviceId) == 0;
47 }
48
IsCapInfoJsonEqual(const std::string & firstData,const std::string & lastData)49 bool CapabilityUtils::IsCapInfoJsonEqual(const std::string& firstData, const std::string& lastData)
50 {
51 nlohmann::json firstJson = nlohmann::json::parse(firstData, nullptr, false);
52 if (firstJson.is_discarded()) {
53 DHLOGE("firstData parse failed");
54 return false;
55 }
56 CapabilityInfo firstCapInfo;
57 FromJson(firstJson, firstCapInfo);
58 nlohmann::json lastJson = nlohmann::json::parse(lastData, nullptr, false);
59 if (lastJson.is_discarded()) {
60 DHLOGE("lastData parse failed");
61 return false;
62 }
63 CapabilityInfo lastCapInfo;
64 FromJson(lastJson, lastCapInfo);
65 return firstCapInfo.Compare(lastCapInfo);
66 }
67 } // namespace DistributedHardware
68 } // namespace OHOS
69