• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (capPtr == nullptr) {
31         capPtr = std::make_shared<CapabilityInfo>();
32     }
33     return capPtr->FromJsonString(value);
34 }
35 
GetCapabilityKey(const std::string & deviceId,const std::string & dhId)36 std::string CapabilityUtils::GetCapabilityKey(const std::string &deviceId, const std::string &dhId)
37 {
38     return deviceId + RESOURCE_SEPARATOR + dhId;
39 }
40 
IsCapKeyMatchDeviceId(const std::string & key,const std::string & deviceId)41 bool CapabilityUtils::IsCapKeyMatchDeviceId(const std::string &key, const std::string &deviceId)
42 {
43     std::size_t separatorPos = key.find(RESOURCE_SEPARATOR);
44     if (separatorPos == std::string::npos) {
45         return false;
46     }
47     std::string keyDevId = key.substr(0, separatorPos);
48     return keyDevId.compare(deviceId) == 0;
49 }
50 
IsCapInfoJsonEqual(const std::string & firstData,const std::string & lastData)51 bool CapabilityUtils::IsCapInfoJsonEqual(const std::string& firstData, const std::string& lastData)
52 {
53     nlohmann::json firstJson = nlohmann::json::parse(firstData, nullptr, false);
54     if (firstJson.is_discarded()) {
55         DHLOGE("firstData parse failed");
56         return false;
57     }
58     CapabilityInfo firstCapInfo;
59     FromJson(firstJson, firstCapInfo);
60     nlohmann::json lastJson = nlohmann::json::parse(lastData, nullptr, false);
61     if (lastJson.is_discarded()) {
62         DHLOGE("lastData parse failed");
63         return false;
64     }
65     CapabilityInfo lastCapInfo;
66     FromJson(lastJson, lastCapInfo);
67     return firstCapInfo.Compare(lastCapInfo);
68 }
69 } // namespace DistributedHardware
70 } // namespace OHOS
71