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 "nr_ssb_information.h"
17 
18 #include <cstdint>
19 #include <ctime>
20 #include <memory>
21 
22 #include "iosfwd"
23 #include "new"
24 #include "parcel.h"
25 #include "string"
26 
27 namespace OHOS {
28 namespace Telephony {
Marshalling(Parcel & parcel) const29 bool NrSsbInformation::Marshalling(Parcel &parcel) const
30 {
31     if (!parcel.WriteInt32(arfcn_)) {
32         return false;
33     }
34     if (!parcel.WriteInt64(cid_)) {
35         return false;
36     }
37     if (!parcel.WriteInt32(pci_)) {
38         return false;
39     }
40     if (!parcel.WriteInt32(rsrp_)) {
41         return false;
42     }
43     if (!parcel.WriteInt32(sinr_)) {
44         return false;
45     }
46     if (!parcel.WriteInt32(timeAdvance_)) {
47         return false;
48     }
49     for (int32_t i = 0; i < SCELL_SSB_LIST && i < static_cast<int32_t>(sCellSsbList_.size()); ++i) {
50         if (!parcel.WriteInt32(sCellSsbList_[i].ssbId)) {
51             return false;
52         }
53         if (!parcel.WriteInt32(sCellSsbList_[i].rsrp)) {
54             return false;
55         }
56     }
57     if (!MarshallingNbCellSsbId(parcel)) {
58         return false;
59     }
60     return true;
61 }
62 
MarshallingNbCellSsbId(Parcel & parcel) const63 bool NrSsbInformation::MarshallingNbCellSsbId(Parcel &parcel) const
64 {
65     if (nbCellCount_ > MAX_NBCELL_COUNT) {
66         return false;
67     }
68     if (!parcel.WriteInt32(nbCellCount_)) {
69         return false;
70     }
71     for (int32_t i = 0; i < nbCellCount_; ++i) {
72         if (!parcel.WriteInt32(nbCellSsbList_[i].pci)) {
73             return false;
74         }
75         if (!parcel.WriteInt32(nbCellSsbList_[i].arfcn)) {
76             return false;
77         }
78         if (!parcel.WriteInt32(nbCellSsbList_[i].rsrp)) {
79             return false;
80         }
81         if (!parcel.WriteInt32(nbCellSsbList_[i].sinr)) {
82             return false;
83         }
84         for (int32_t j = 0; j < NBCELL_SSB_LIST; ++j) {
85             if (!parcel.WriteInt32(nbCellSsbList_[i].ssbList[j].ssbId)) {
86                 return false;
87             }
88             if (!parcel.WriteInt32(nbCellSsbList_[i].ssbList[j].rsrp)) {
89                 return false;
90             }
91         }
92     }
93     return true;
94 }
95 
ReadFromParcel(Parcel & parcel)96 bool NrSsbInformation::ReadFromParcel(Parcel &parcel)
97 {
98     int32_t item_32;
99     int64_t item_64;
100     if (!parcel.ReadInt32(item_32)) {
101         return false;
102     }
103     arfcn_ = item_32;
104     if (!parcel.ReadInt64(item_64)) {
105         return false;
106     }
107     cid_ = item_64;
108     if (!parcel.ReadInt32(item_32)) {
109         return false;
110     }
111     pci_ = item_32;
112     if (!parcel.ReadInt32(item_32)) {
113         return false;
114     }
115     rsrp_ = item_32;
116     if (!parcel.ReadInt32(item_32)) {
117         return false;
118     }
119     sinr_ = item_32;
120     if (!parcel.ReadInt32(item_32)) {
121         return false;
122     }
123     timeAdvance_ = item_32;
124     for (int32_t i = 0; i < SCELL_SSB_LIST; ++i) {
125         SsbInfo ssbInfo;
126         if (!parcel.ReadInt32(item_32)) {
127             return false;
128         }
129         ssbInfo.ssbId = item_32;
130         if (!parcel.ReadInt32(item_32)) {
131             return false;
132         }
133         ssbInfo.rsrp = item_32;
134         sCellSsbList_.push_back(ssbInfo);
135     }
136     if (!ReadFromParcelForNbCell(parcel)) {
137         return false;
138     }
139     return true;
140 }
141 
ReadFromParcelForNbCell(Parcel & parcel)142 bool NrSsbInformation::ReadFromParcelForNbCell(Parcel &parcel)
143 {
144     int32_t item_32;
145     if (!parcel.ReadInt32(item_32)) {
146         return false;
147     }
148     nbCellCount_ = (item_32 > MAX_NBCELL_COUNT) ? MAX_NBCELL_COUNT : item_32;
149     for (int32_t i = 0; i < nbCellCount_; ++i) {
150         NeighboringCellSsbInformation nbCellSsbInfo;
151         if (!parcel.ReadInt32(item_32)) {
152             return false;
153         }
154         nbCellSsbInfo.pci = item_32;
155         if (!parcel.ReadInt32(item_32)) {
156             return false;
157         }
158         nbCellSsbInfo.arfcn = item_32;
159         if (!parcel.ReadInt32(item_32)) {
160             return false;
161         }
162         nbCellSsbInfo.rsrp = item_32;
163         if (!parcel.ReadInt32(item_32)) {
164             return false;
165         }
166         nbCellSsbInfo.sinr = item_32;
167         for (int32_t j = 0; j < NBCELL_SSB_LIST; ++j) {
168             SsbInfo ssbInfo;
169             if (!parcel.ReadInt32(item_32)) {
170                 return false;
171             }
172             ssbInfo.ssbId = item_32;
173             if (!parcel.ReadInt32(item_32)) {
174                 return false;
175             }
176             ssbInfo.rsrp = item_32;
177             nbCellSsbInfo.ssbList.push_back(ssbInfo);
178         }
179         nbCellSsbList_.push_back(nbCellSsbInfo);
180     }
181     return true;
182 }
183 
SetSsbBaseParam(int32_t arfcn,int64_t cid,int32_t pci,int32_t rsrp,int32_t sinr,int32_t timeAdvance)184 void NrSsbInformation::SetSsbBaseParam(
185     int32_t arfcn, int64_t cid, int32_t pci, int32_t rsrp, int32_t sinr, int32_t timeAdvance)
186 {
187     arfcn_ = arfcn;
188     cid_ = cid;
189     pci_ = pci;
190     rsrp_ = rsrp;
191     sinr_ = sinr;
192     timeAdvance_ = timeAdvance;
193 }
194 
SetSCellSsbList(std::vector<SsbInfo> sCellSsbList)195 void NrSsbInformation::SetSCellSsbList(std::vector<SsbInfo> sCellSsbList)
196 {
197     for (const auto &info : sCellSsbList) {
198         SsbInfo ssbInfo;
199         ssbInfo.ssbId = info.ssbId;
200         ssbInfo.rsrp = info.rsrp;
201         sCellSsbList_.push_back(ssbInfo);
202     }
203 }
204 
SetNbCellSsbList(int32_t nbCellCount,std::vector<NeighboringCellSsbInformation> nbCellSsbList)205 void NrSsbInformation::SetNbCellSsbList(int32_t nbCellCount, std::vector<NeighboringCellSsbInformation> nbCellSsbList)
206 {
207     nbCellCount_ = (nbCellCount > MAX_NBCELL_COUNT) ? MAX_NBCELL_COUNT : nbCellCount;
208     for (int32_t i = 0; i < nbCellCount_; ++i) {
209         NeighboringCellSsbInformation nbCellSsbInfo;
210         nbCellSsbInfo.pci = nbCellSsbList[i].pci;
211         nbCellSsbInfo.arfcn = nbCellSsbList[i].arfcn;
212         nbCellSsbInfo.rsrp = nbCellSsbList[i].rsrp;
213         nbCellSsbInfo.sinr = nbCellSsbList[i].sinr;
214         for (const auto &info : nbCellSsbList[i].ssbList) {
215             SsbInfo ssbInfo;
216             ssbInfo.ssbId = info.ssbId;
217             ssbInfo.rsrp = info.rsrp;
218             nbCellSsbInfo.ssbList.push_back(ssbInfo);
219         }
220         nbCellSsbList_.push_back(nbCellSsbInfo);
221     }
222 }
223 
GetArfcn() const224 int32_t NrSsbInformation::GetArfcn() const
225 {
226     return arfcn_;
227 }
228 
GetCid() const229 int64_t NrSsbInformation::GetCid() const
230 {
231     return cid_;
232 }
233 
GetPci() const234 int32_t NrSsbInformation::GetPci() const
235 {
236     return pci_;
237 }
238 
GetRsrp() const239 int32_t NrSsbInformation::GetRsrp() const
240 {
241     return rsrp_;
242 }
243 
GetSinr() const244 int32_t NrSsbInformation::GetSinr() const
245 {
246     return sinr_;
247 }
248 
GetTimeAdvance() const249 int32_t NrSsbInformation::GetTimeAdvance() const
250 {
251     return timeAdvance_;
252 }
253 
GetNbCellCount() const254 int32_t NrSsbInformation::GetNbCellCount() const
255 {
256     return nbCellCount_;
257 }
258 
GetSCellSsbIdList(std::vector<SsbInfo> & sCellSsbList) const259 void NrSsbInformation::GetSCellSsbIdList(std::vector<SsbInfo> &sCellSsbList) const
260 {
261     for (const auto &info : sCellSsbList_) {
262         SsbInfo ssbInfo;
263         ssbInfo.ssbId = info.ssbId;
264         ssbInfo.rsrp = info.rsrp;
265         sCellSsbList.push_back(ssbInfo);
266     }
267 }
268 
GetNbCellSsbIdList(std::vector<NeighboringCellSsbInformation> & nbCellSsbList) const269 void NrSsbInformation::GetNbCellSsbIdList(std::vector<NeighboringCellSsbInformation> &nbCellSsbList) const
270 {
271     for (int32_t i = 0; i < nbCellCount_; i++) {
272         NeighboringCellSsbInformation neighboringCellSsbInfo;
273         neighboringCellSsbInfo.pci = nbCellSsbList_[i].pci;
274         neighboringCellSsbInfo.arfcn = nbCellSsbList_[i].arfcn;
275         neighboringCellSsbInfo.rsrp = nbCellSsbList_[i].rsrp;
276         neighboringCellSsbInfo.sinr = nbCellSsbList_[i].sinr;
277         for (const auto &info : nbCellSsbList_[i].ssbList) {
278             SsbInfo ssbInfo;
279             ssbInfo.ssbId = info.ssbId;
280             ssbInfo.rsrp = info.rsrp;
281             neighboringCellSsbInfo.ssbList.push_back(ssbInfo);
282         }
283         nbCellSsbList.push_back(neighboringCellSsbInfo);
284     }
285 }
286 } // namespace Telephony
287 } // namespace OHOS
288