1 /*
2  * Copyright (c) 2023-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 
16 #include "trust_device_profile.h"
17 #include "cJSON.h"
18 #include "distributed_device_profile_constants.h"
19 #include "macro_utils.h"
20 #include "profile_utils.h"
21 
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 namespace {
25     const std::string TAG = "TrustDeviceProfile";
26 }
GetDeviceId() const27 std::string TrustDeviceProfile::GetDeviceId() const
28 {
29     return deviceId_;
30 }
31 
SetDeviceId(const std::string & deviceId)32 void TrustDeviceProfile::SetDeviceId(const std::string& deviceId)
33 {
34     deviceId_ = deviceId;
35 }
36 
GetDeviceIdType() const37 int32_t TrustDeviceProfile::GetDeviceIdType() const
38 {
39     return deviceIdType_;
40 }
41 
SetDeviceIdType(int32_t deviceType)42 void TrustDeviceProfile::SetDeviceIdType(int32_t deviceType)
43 {
44     deviceIdType_ = deviceType;
45 }
46 
GetDeviceIdHash() const47 std::string TrustDeviceProfile::GetDeviceIdHash() const
48 {
49     return deviceIdHash_;
50 }
51 
SetDeviceIdHash(const std::string & deviceIdHash)52 void TrustDeviceProfile::SetDeviceIdHash(const std::string& deviceIdHash)
53 {
54     deviceIdHash_ = deviceIdHash;
55 }
56 
GetStatus() const57 int32_t TrustDeviceProfile::GetStatus() const
58 {
59     return status_;
60 }
61 
SetStatus(int32_t status)62 void TrustDeviceProfile::SetStatus(int32_t status)
63 {
64     status_ = status;
65 }
66 
GetBindType() const67 uint32_t TrustDeviceProfile::GetBindType() const
68 {
69     return bindType_;
70 }
71 
SetBindType(uint32_t bindType)72 void TrustDeviceProfile::SetBindType(uint32_t bindType)
73 {
74     bindType_ = bindType;
75 }
76 
Marshalling(MessageParcel & parcel) const77 bool TrustDeviceProfile::Marshalling(MessageParcel& parcel) const
78 {
79     WRITE_HELPER_RET(parcel, String, deviceId_, false);
80     WRITE_HELPER_RET(parcel, Uint32, deviceIdType_, false);
81     WRITE_HELPER_RET(parcel, String, deviceIdHash_, false);
82     WRITE_HELPER_RET(parcel, Int32, status_, false);
83     WRITE_HELPER_RET(parcel, Uint32, bindType_, false);
84     return true;
85 }
86 
UnMarshalling(MessageParcel & parcel)87 bool TrustDeviceProfile::UnMarshalling(MessageParcel& parcel)
88 {
89     READ_HELPER_RET(parcel, String, deviceId_, false);
90     READ_HELPER_RET(parcel, Uint32, deviceIdType_, false);
91     READ_HELPER_RET(parcel, String, deviceIdHash_, false);
92     READ_HELPER_RET(parcel, Int32, status_, false);
93     READ_HELPER_RET(parcel, Uint32, bindType_, false);
94     return true;
95 }
96 
dump() const97 std::string TrustDeviceProfile::dump() const
98 {
99     cJSON* json = cJSON_CreateObject();
100     if (!cJSON_IsObject(json)) {
101         cJSON_Delete(json);
102         return EMPTY_STRING;
103     }
104     cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
105     cJSON_AddNumberToObject(json, DEVICE_ID_TYPE.c_str(), deviceIdType_);
106     cJSON_AddStringToObject(json, DEVICE_ID_HASH.c_str(), deviceIdHash_.c_str());
107     cJSON_AddNumberToObject(json, STATUS.c_str(), status_);
108     char* jsonChars = cJSON_PrintUnformatted(json);
109     if (jsonChars == NULL) {
110         cJSON_Delete(json);
111         HILOGE("cJSON formatted to string failed!");
112         return EMPTY_STRING;
113     }
114     std::string jsonStr = jsonChars;
115     cJSON_Delete(json);
116     cJSON_free(jsonChars);
117     return jsonStr;
118 }
119 } // namespace DistributedDeviceProfile
120 } // namespace OHOS