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 >= MAX_TECH_LIST_NUM) {
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         tagTechExtrasData.push_back(*(pacMap));
74     }
75     std::string tagUid;
76     parcel.ReadString(tagUid);
77 
78     int tagRfDiscId;
79     parcel.ReadInt32(tagRfDiscId);
80     TagInfoParcelable *taginfo = new (std::nothrow) TagInfoParcelable(tagTechList, tagTechExtrasData,
81         tagUid, tagRfDiscId, nullptr);
82     return taginfo;
83 }
84 
GetUid()85 std::string TagInfoParcelable::GetUid()
86 {
87     return tagUid_;
88 }
89 
GetTechList()90 std::vector<int> TagInfoParcelable::GetTechList()
91 {
92     return tagTechList_;
93 }
94 
GetDiscId()95 int TagInfoParcelable::GetDiscId()
96 {
97     return tagRfDiscId_;
98 }
99 
GetTechExtrasDataList()100 std::vector<AppExecFwk::PacMap> TagInfoParcelable::GetTechExtrasDataList()
101 {
102     return tagTechExtrasData_;
103 }
104 
ToString()105 std::string TagInfoParcelable::ToString()
106 {
107     std::string res = "tagTechList: [";
108     if (tagTechList_.size() <= 0) {
109         res += "]";
110         return res;
111     }
112     for (uint32_t i = 0; i < tagTechList_.size() - 1; i++) {
113         res += std::to_string(tagTechList_[i]);
114         res += ", ";
115     }
116     res += std::to_string(tagTechList_[tagTechList_.size() - 1]);
117     res += "]";
118     return res;
119 }
120 }  // namespace KITS
121 }  // namespace NFC
122 }  // namespace OHOS