• 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_info.h"
17 
18 #include <string>
19 
20 #include "nlohmann/json.hpp"
21 
22 #include "anonymous_string.h"
23 #include "constants.h"
24 #include "distributed_hardware_errno.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "CapabilityInfo"
30 
GetDHId() const31 std::string CapabilityInfo::GetDHId() const
32 {
33     return dhId_;
34 }
35 
SetDHId(const std::string & dhId)36 void CapabilityInfo::SetDHId(const std::string &dhId)
37 {
38     this->dhId_ = dhId;
39 }
40 
GetDeviceId() const41 std::string CapabilityInfo::GetDeviceId() const
42 {
43     return deviceId_;
44 }
45 
SetDeviceId(const std::string & deviceId)46 void CapabilityInfo::SetDeviceId(const std::string &deviceId)
47 {
48     this->deviceId_ = deviceId;
49 }
50 
GetDeviceName() const51 std::string CapabilityInfo::GetDeviceName() const
52 {
53     return deviceName_;
54 }
55 
SetDeviceName(const std::string & deviceName)56 void CapabilityInfo::SetDeviceName(const std::string &deviceName)
57 {
58     this->deviceName_ = deviceName;
59 }
60 
GetDeviceType() const61 uint16_t CapabilityInfo::GetDeviceType() const
62 {
63     return deviceType_;
64 }
65 
SetDeviceType(uint16_t deviceType)66 void CapabilityInfo::SetDeviceType(uint16_t deviceType)
67 {
68     this->deviceType_ = deviceType;
69 }
70 
GetDHType() const71 DHType CapabilityInfo::GetDHType() const
72 {
73     return dhType_;
74 }
75 
SetDHType(const DHType dhType)76 void CapabilityInfo::SetDHType(const DHType dhType)
77 {
78     this->dhType_ = dhType;
79 }
80 
GetDHAttrs() const81 std::string CapabilityInfo::GetDHAttrs() const
82 {
83     return dhAttrs_;
84 }
85 
SetDHAttrs(const std::string & dhAttrs)86 void CapabilityInfo::SetDHAttrs(const std::string &dhAttrs)
87 {
88     this->dhAttrs_ = dhAttrs;
89 }
90 
GetKey() const91 std::string CapabilityInfo::GetKey() const
92 {
93     std::string kvStoreKey;
94     kvStoreKey.append(deviceId_);
95     kvStoreKey.append(RESOURCE_SEPARATOR);
96     kvStoreKey.append(dhId_);
97     return kvStoreKey;
98 }
99 
GetAnonymousKey() const100 std::string CapabilityInfo::GetAnonymousKey() const
101 {
102     std::string kvStoreKey;
103     kvStoreKey.append(GetAnonyString(deviceId_));
104     kvStoreKey.append(RESOURCE_SEPARATOR);
105     kvStoreKey.append(GetAnonyString(dhId_));
106     return kvStoreKey;
107 }
108 
FromJsonString(const std::string & jsonStr)109 int32_t CapabilityInfo::FromJsonString(const std::string &jsonStr)
110 {
111     nlohmann::json jsonObj = nlohmann::json::parse(jsonStr);
112     FromJson(jsonObj, *this);
113     return DH_FWK_SUCCESS;
114 }
115 
ToJsonString()116 std::string CapabilityInfo::ToJsonString()
117 {
118     nlohmann::json jsonObj;
119     ToJson(jsonObj, *this);
120     return jsonObj.dump();
121 }
122 
ToJson(nlohmann::json & jsonObject,const CapabilityInfo & capability)123 void ToJson(nlohmann::json &jsonObject, const CapabilityInfo &capability)
124 {
125     jsonObject[DH_ID] = capability.GetDHId();
126     jsonObject[DEV_ID] = capability.GetDeviceId();
127     jsonObject[DEV_NAME] = capability.GetDeviceName();
128     jsonObject[DEV_TYPE] = capability.GetDeviceType();
129     jsonObject[DH_TYPE] = capability.GetDHType();
130     jsonObject[DH_ATTRS] = capability.GetDHAttrs();
131 }
132 
FromJson(const nlohmann::json & jsonObject,CapabilityInfo & capability)133 void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability)
134 {
135     if (jsonObject.find(DH_ID) != jsonObject.end()) {
136         capability.SetDHId(jsonObject.at(DH_ID).get<std::string>());
137     }
138     if (jsonObject.find(DEV_ID) != jsonObject.end()) {
139         capability.SetDeviceId(jsonObject.at(DEV_ID).get<std::string>());
140     }
141     if (jsonObject.find(DEV_NAME) != jsonObject.end()) {
142         capability.SetDeviceName(jsonObject.at(DEV_NAME).get<std::string>());
143     }
144     if (jsonObject.find(DEV_TYPE) != jsonObject.end()) {
145         capability.SetDeviceType(jsonObject.at(DEV_TYPE).get<uint16_t>());
146     }
147     if (jsonObject.find(DH_TYPE) != jsonObject.end()) {
148         capability.SetDHType(jsonObject.at(DH_TYPE).get<DHType>());
149     }
150     if (jsonObject.find(DH_ATTRS) != jsonObject.end()) {
151         capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get<std::string>());
152     }
153 }
154 }
155 }
156