1 /*
2 * Copyright (c) 2025 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 "business_event.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 = "BusinessEvent";
26 }
27
BusinessEvent()28 BusinessEvent::BusinessEvent()
29 {
30 }
31
~BusinessEvent()32 BusinessEvent::~BusinessEvent()
33 {
34 }
35
GetBusinessKey() const36 std::string BusinessEvent::GetBusinessKey() const
37 {
38 return businessKey_;
39 }
40
SetBusinessKey(const std::string & businessKey)41 void BusinessEvent::SetBusinessKey(const std::string& businessKey)
42 {
43 businessKey_ = businessKey;
44 }
45
GetBusinessValue() const46 std::string BusinessEvent::GetBusinessValue() const
47 {
48 return businessValue_;
49 }
50
SetBusinessValue(const std::string & businessValue)51 void BusinessEvent::SetBusinessValue(const std::string& businessValue)
52 {
53 businessValue_ = businessValue;
54 }
55
Marshalling(MessageParcel & parcel) const56 bool BusinessEvent::Marshalling(MessageParcel& parcel) const
57 {
58 WRITE_HELPER(parcel, String, businessKey_);
59 WRITE_HELPER(parcel, String, businessValue_);
60 return true;
61 }
62
UnMarshalling(MessageParcel & parcel)63 bool BusinessEvent::UnMarshalling(MessageParcel& parcel)
64 {
65 businessKey_ = parcel.ReadString();
66 businessValue_ = parcel.ReadString();
67 return true;
68 }
69
dump() const70 std::string BusinessEvent::dump() const
71 {
72 cJSON* json = cJSON_CreateObject();
73 if (!cJSON_IsObject(json)) {
74 cJSON_Delete(json);
75 return EMPTY_STRING;
76 }
77 cJSON_AddStringToObject(json, BUSINESS_KEY.c_str(), businessKey_.c_str());
78 cJSON_AddStringToObject(json, BUSINESS_VALUE.c_str(), businessValue_.c_str());
79 char* jsonChars = cJSON_PrintUnformatted(json);
80 if (jsonChars == NULL) {
81 cJSON_Delete(json);
82 HILOGE("cJSON formatted to string failed!");
83 return EMPTY_STRING;
84 }
85 std::string jsonStr = jsonChars;
86 cJSON_Delete(json);
87 cJSON_free(jsonChars);
88 return jsonStr;
89 }
90
BusinessEventExt()91 BusinessEventExt::BusinessEventExt()
92 {
93 }
94
~BusinessEventExt()95 BusinessEventExt::~BusinessEventExt()
96 {
97 }
98
GetBusinessKey() const99 std::string BusinessEventExt::GetBusinessKey() const
100 {
101 return businessKey_;
102 }
103
SetBusinessKey(const std::string & businessKey)104 void BusinessEventExt::SetBusinessKey(const std::string& businessKey)
105 {
106 businessKey_ = businessKey;
107 }
108
GetBusinessValue() const109 std::string BusinessEventExt::GetBusinessValue() const
110 {
111 return businessValue_;
112 }
113
SetBusinessValue(const std::string & businessValue)114 void BusinessEventExt::SetBusinessValue(const std::string& businessValue)
115 {
116 businessValue_ = businessValue;
117 }
118
Marshalling(Parcel & parcel) const119 bool BusinessEventExt::Marshalling(Parcel& parcel) const
120 {
121 WRITE_HELPER(parcel, String, businessKey_);
122 WRITE_HELPER(parcel, String, businessValue_);
123 return true;
124 }
125
Unmarshalling(Parcel & parcel)126 BusinessEventExt *BusinessEventExt::Unmarshalling(Parcel& parcel)
127 {
128 BusinessEventExt* obj = new (std::nothrow) BusinessEventExt();
129 if (!obj) {
130 return nullptr;
131 }
132 obj->businessKey_ = parcel.ReadString();
133 obj->businessValue_ = parcel.ReadString();
134 return obj;
135 }
136 } // namespace DistributedDeviceProfile
137 } // namespace OHOS
138
139