1 /*
2 * Copyright (C) 2022 - 2023 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 #include "taginfo_parcelable.h"
16 #include "loghelper.h"
17 #include "parcel.h"
18 #include "refbase.h"
19
20 namespace OHOS {
21 namespace NFC {
22 namespace KITS {
23 const uint32_t MAX_TECH_LIST_NUM = 12;
24
TagInfoParcelable(std::vector<int> tagTechList,std::vector<AppExecFwk::PacMap> tagTechExtrasData,std::string & tagUid,int tagRfDiscId,OHOS::sptr<IRemoteObject> tagServiceIface)25 TagInfoParcelable::TagInfoParcelable(std::vector<int> tagTechList,
26 std::vector<AppExecFwk::PacMap> tagTechExtrasData,
27 std::string& tagUid,
28 int tagRfDiscId,
29 OHOS::sptr<IRemoteObject> tagServiceIface)
30 {
31 tagRfDiscId_ = tagRfDiscId;
32 tagUid_ = tagUid;
33 tagTechList_ = std::move(tagTechList);
34 tagServiceIface_ = tagServiceIface;
35 tagTechExtrasData_ = std::move(tagTechExtrasData);
36 }
37
~TagInfoParcelable()38 TagInfoParcelable::~TagInfoParcelable()
39 {
40 tagUid_.clear();
41 tagTechList_.clear();
42 tagTechExtrasData_.clear();
43 tagRfDiscId_ = 0;
44 tagServiceIface_ = nullptr;
45 }
46
Marshalling(Parcel & parcel) const47 bool TagInfoParcelable::Marshalling(Parcel &parcel) const
48 {
49 const std::vector<int> tagTechList = std::move(tagTechList_);
50 parcel.WriteInt32Vector(tagTechList);
51 parcel.WriteInt32(tagTechExtrasData_.size());
52 for (unsigned int i = 0; i < tagTechExtrasData_.size(); i++) {
53 tagTechExtrasData_[i].Marshalling(parcel);
54 }
55 parcel.WriteString(tagUid_);
56 parcel.WriteInt32(tagRfDiscId_);
57 return true;
58 }
59
Unmarshalling(Parcel & parcel)60 TagInfoParcelable *TagInfoParcelable::Unmarshalling(Parcel &parcel)
61 {
62 std::vector<int> tagTechList;
63 parcel.ReadInt32Vector(&tagTechList);
64
65 int32_t extraLen = 0;
66 parcel.ReadInt32(extraLen);
67 if (extraLen >= static_cast<int32_t>(MAX_TECH_LIST_NUM) || tagTechList.size() > MAX_TECH_LIST_SIZE) {
68 return nullptr;
69 }
70 std::vector<AppExecFwk::PacMap> tagTechExtrasData;
71 for (int i = 0; i < extraLen; i++) {
72 AppExecFwk::PacMap* pacMap = AppExecFwk::PacMap::Unmarshalling(parcel);
73 if (pacMap == nullptr) {
74 ErrorLog("fail to unmarshall pacMap");
75 return nullptr;
76 }
77 tagTechExtrasData.push_back(*(pacMap));
78 delete pacMap;
79 pacMap = nullptr;
80 }
81 std::string tagUid;
82 parcel.ReadString(tagUid);
83
84 int tagRfDiscId = 0;
85 parcel.ReadInt32(tagRfDiscId);
86 TagInfoParcelable *taginfo = new (std::nothrow) TagInfoParcelable(tagTechList, tagTechExtrasData,
87 tagUid, tagRfDiscId, nullptr);
88 return taginfo;
89 }
90
GetUid()91 std::string TagInfoParcelable::GetUid()
92 {
93 return tagUid_;
94 }
95
GetTechList()96 std::vector<int> TagInfoParcelable::GetTechList()
97 {
98 return tagTechList_;
99 }
100
GetDiscId()101 int TagInfoParcelable::GetDiscId()
102 {
103 return tagRfDiscId_;
104 }
105
GetTechExtrasDataList()106 std::vector<AppExecFwk::PacMap> TagInfoParcelable::GetTechExtrasDataList()
107 {
108 return tagTechExtrasData_;
109 }
110
ToString()111 std::string TagInfoParcelable::ToString()
112 {
113 std::string res = "tagTechList: [";
114 if (tagTechList_.size() <= 0) {
115 res += "]";
116 return res;
117 }
118 for (uint32_t i = 0; i < tagTechList_.size() - 1; i++) {
119 res += std::to_string(tagTechList_[i]);
120 res += ", ";
121 }
122 res += std::to_string(tagTechList_[tagTechList_.size() - 1]);
123 res += "]";
124 return res;
125 }
126 } // namespace KITS
127 } // namespace NFC
128 } // namespace OHOS