1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This file was automatically generated by chre_api_to_chpp.py
18 // Date: 2021-05-24 15:46:25 UTC
19 // Source: chre_api/include/chre_api/chre/wwan.h @ commit 833a6241
20 
21 // DO NOT modify this file directly, as those changes will be lost the next
22 // time the script is executed
23 
24 #include "chpp/common/wwan_types.h"
25 #include "chpp/macros.h"
26 #include "chpp/memory.h"
27 
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <string.h>
31 
32 // Encoding (CHRE --> CHPP) size functions
33 
34 //! @return number of bytes required to represent the given
35 //! chreWwanCellInfoResult along with the CHPP header as
36 //! struct ChppWwanCellInfoResultWithHeader
chppWwanSizeOfCellInfoResultFromChre(const struct chreWwanCellInfoResult * cellInfoResult)37 static size_t chppWwanSizeOfCellInfoResultFromChre(
38     const struct chreWwanCellInfoResult *cellInfoResult) {
39   size_t encodedSize = sizeof(struct ChppWwanCellInfoResultWithHeader);
40   encodedSize +=
41       cellInfoResult->cellInfoCount * sizeof(struct ChppWwanCellInfo);
42   return encodedSize;
43 }
44 
45 // Encoding (CHRE --> CHPP) conversion functions
46 
chppWwanConvertCellIdentityCdmaFromChre(const struct chreWwanCellIdentityCdma * in,struct ChppWwanCellIdentityCdma * out)47 static void chppWwanConvertCellIdentityCdmaFromChre(
48     const struct chreWwanCellIdentityCdma *in,
49     struct ChppWwanCellIdentityCdma *out) {
50   out->networkId = in->networkId;
51   out->systemId = in->systemId;
52   out->basestationId = in->basestationId;
53   out->longitude = in->longitude;
54   out->latitude = in->latitude;
55 }
56 
chppWwanConvertSignalStrengthCdmaFromChre(const struct chreWwanSignalStrengthCdma * in,struct ChppWwanSignalStrengthCdma * out)57 static void chppWwanConvertSignalStrengthCdmaFromChre(
58     const struct chreWwanSignalStrengthCdma *in,
59     struct ChppWwanSignalStrengthCdma *out) {
60   out->dbm = in->dbm;
61   out->ecio = in->ecio;
62 }
63 
chppWwanConvertSignalStrengthEvdoFromChre(const struct chreWwanSignalStrengthEvdo * in,struct ChppWwanSignalStrengthEvdo * out)64 static void chppWwanConvertSignalStrengthEvdoFromChre(
65     const struct chreWwanSignalStrengthEvdo *in,
66     struct ChppWwanSignalStrengthEvdo *out) {
67   out->dbm = in->dbm;
68   out->ecio = in->ecio;
69   out->signalNoiseRatio = in->signalNoiseRatio;
70 }
71 
chppWwanConvertCellInfoCdmaFromChre(const struct chreWwanCellInfoCdma * in,struct ChppWwanCellInfoCdma * out)72 static void chppWwanConvertCellInfoCdmaFromChre(
73     const struct chreWwanCellInfoCdma *in, struct ChppWwanCellInfoCdma *out) {
74   chppWwanConvertCellIdentityCdmaFromChre(&in->cellIdentityCdma,
75                                           &out->cellIdentityCdma);
76   chppWwanConvertSignalStrengthCdmaFromChre(&in->signalStrengthCdma,
77                                             &out->signalStrengthCdma);
78   chppWwanConvertSignalStrengthEvdoFromChre(&in->signalStrengthEvdo,
79                                             &out->signalStrengthEvdo);
80 }
81 
chppWwanConvertCellIdentityGsmFromChre(const struct chreWwanCellIdentityGsm * in,struct ChppWwanCellIdentityGsm * out)82 static void chppWwanConvertCellIdentityGsmFromChre(
83     const struct chreWwanCellIdentityGsm *in,
84     struct ChppWwanCellIdentityGsm *out) {
85   out->mcc = in->mcc;
86   out->mnc = in->mnc;
87   out->lac = in->lac;
88   out->cid = in->cid;
89   out->arfcn = in->arfcn;
90   out->bsic = in->bsic;
91   memset(&out->reserved, 0, sizeof(out->reserved));
92 }
93 
chppWwanConvertSignalStrengthGsmFromChre(const struct chreWwanSignalStrengthGsm * in,struct ChppWwanSignalStrengthGsm * out)94 static void chppWwanConvertSignalStrengthGsmFromChre(
95     const struct chreWwanSignalStrengthGsm *in,
96     struct ChppWwanSignalStrengthGsm *out) {
97   out->signalStrength = in->signalStrength;
98   out->bitErrorRate = in->bitErrorRate;
99   out->timingAdvance = in->timingAdvance;
100 }
101 
chppWwanConvertCellInfoGsmFromChre(const struct chreWwanCellInfoGsm * in,struct ChppWwanCellInfoGsm * out)102 static void chppWwanConvertCellInfoGsmFromChre(
103     const struct chreWwanCellInfoGsm *in, struct ChppWwanCellInfoGsm *out) {
104   chppWwanConvertCellIdentityGsmFromChre(&in->cellIdentityGsm,
105                                          &out->cellIdentityGsm);
106   chppWwanConvertSignalStrengthGsmFromChre(&in->signalStrengthGsm,
107                                            &out->signalStrengthGsm);
108 }
109 
chppWwanConvertCellIdentityLteFromChre(const struct chreWwanCellIdentityLte * in,struct ChppWwanCellIdentityLte * out)110 static void chppWwanConvertCellIdentityLteFromChre(
111     const struct chreWwanCellIdentityLte *in,
112     struct ChppWwanCellIdentityLte *out) {
113   out->mcc = in->mcc;
114   out->mnc = in->mnc;
115   out->ci = in->ci;
116   out->pci = in->pci;
117   out->tac = in->tac;
118   out->earfcn = in->earfcn;
119 }
120 
chppWwanConvertSignalStrengthLteFromChre(const struct chreWwanSignalStrengthLte * in,struct ChppWwanSignalStrengthLte * out)121 static void chppWwanConvertSignalStrengthLteFromChre(
122     const struct chreWwanSignalStrengthLte *in,
123     struct ChppWwanSignalStrengthLte *out) {
124   out->signalStrength = in->signalStrength;
125   out->rsrp = in->rsrp;
126   out->rsrq = in->rsrq;
127   out->rssnr = in->rssnr;
128   out->cqi = in->cqi;
129   out->timingAdvance = in->timingAdvance;
130 }
131 
chppWwanConvertCellInfoLteFromChre(const struct chreWwanCellInfoLte * in,struct ChppWwanCellInfoLte * out)132 static void chppWwanConvertCellInfoLteFromChre(
133     const struct chreWwanCellInfoLte *in, struct ChppWwanCellInfoLte *out) {
134   chppWwanConvertCellIdentityLteFromChre(&in->cellIdentityLte,
135                                          &out->cellIdentityLte);
136   chppWwanConvertSignalStrengthLteFromChre(&in->signalStrengthLte,
137                                            &out->signalStrengthLte);
138 }
139 
chppWwanConvertCellIdentityNrFromChre(const struct chreWwanCellIdentityNr * in,struct ChppWwanCellIdentityNr * out)140 static void chppWwanConvertCellIdentityNrFromChre(
141     const struct chreWwanCellIdentityNr *in,
142     struct ChppWwanCellIdentityNr *out) {
143   out->mcc = in->mcc;
144   out->mnc = in->mnc;
145   out->nci0 = in->nci0;
146   out->nci1 = in->nci1;
147   out->pci = in->pci;
148   out->tac = in->tac;
149   out->nrarfcn = in->nrarfcn;
150 }
151 
chppWwanConvertSignalStrengthNrFromChre(const struct chreWwanSignalStrengthNr * in,struct ChppWwanSignalStrengthNr * out)152 static void chppWwanConvertSignalStrengthNrFromChre(
153     const struct chreWwanSignalStrengthNr *in,
154     struct ChppWwanSignalStrengthNr *out) {
155   out->ssRsrp = in->ssRsrp;
156   out->ssRsrq = in->ssRsrq;
157   out->ssSinr = in->ssSinr;
158   out->csiRsrp = in->csiRsrp;
159   out->csiRsrq = in->csiRsrq;
160   out->csiSinr = in->csiSinr;
161 }
162 
chppWwanConvertCellInfoNrFromChre(const struct chreWwanCellInfoNr * in,struct ChppWwanCellInfoNr * out)163 static void chppWwanConvertCellInfoNrFromChre(
164     const struct chreWwanCellInfoNr *in, struct ChppWwanCellInfoNr *out) {
165   chppWwanConvertCellIdentityNrFromChre(&in->cellIdentityNr,
166                                         &out->cellIdentityNr);
167   chppWwanConvertSignalStrengthNrFromChre(&in->signalStrengthNr,
168                                           &out->signalStrengthNr);
169 }
170 
chppWwanConvertCellIdentityTdscdmaFromChre(const struct chreWwanCellIdentityTdscdma * in,struct ChppWwanCellIdentityTdscdma * out)171 static void chppWwanConvertCellIdentityTdscdmaFromChre(
172     const struct chreWwanCellIdentityTdscdma *in,
173     struct ChppWwanCellIdentityTdscdma *out) {
174   out->mcc = in->mcc;
175   out->mnc = in->mnc;
176   out->lac = in->lac;
177   out->cid = in->cid;
178   out->cpid = in->cpid;
179 }
180 
chppWwanConvertSignalStrengthTdscdmaFromChre(const struct chreWwanSignalStrengthTdscdma * in,struct ChppWwanSignalStrengthTdscdma * out)181 static void chppWwanConvertSignalStrengthTdscdmaFromChre(
182     const struct chreWwanSignalStrengthTdscdma *in,
183     struct ChppWwanSignalStrengthTdscdma *out) {
184   out->rscp = in->rscp;
185 }
186 
chppWwanConvertCellInfoTdscdmaFromChre(const struct chreWwanCellInfoTdscdma * in,struct ChppWwanCellInfoTdscdma * out)187 static void chppWwanConvertCellInfoTdscdmaFromChre(
188     const struct chreWwanCellInfoTdscdma *in,
189     struct ChppWwanCellInfoTdscdma *out) {
190   chppWwanConvertCellIdentityTdscdmaFromChre(&in->cellIdentityTdscdma,
191                                              &out->cellIdentityTdscdma);
192   chppWwanConvertSignalStrengthTdscdmaFromChre(&in->signalStrengthTdscdma,
193                                                &out->signalStrengthTdscdma);
194 }
195 
chppWwanConvertCellIdentityWcdmaFromChre(const struct chreWwanCellIdentityWcdma * in,struct ChppWwanCellIdentityWcdma * out)196 static void chppWwanConvertCellIdentityWcdmaFromChre(
197     const struct chreWwanCellIdentityWcdma *in,
198     struct ChppWwanCellIdentityWcdma *out) {
199   out->mcc = in->mcc;
200   out->mnc = in->mnc;
201   out->lac = in->lac;
202   out->cid = in->cid;
203   out->psc = in->psc;
204   out->uarfcn = in->uarfcn;
205 }
206 
chppWwanConvertSignalStrengthWcdmaFromChre(const struct chreWwanSignalStrengthWcdma * in,struct ChppWwanSignalStrengthWcdma * out)207 static void chppWwanConvertSignalStrengthWcdmaFromChre(
208     const struct chreWwanSignalStrengthWcdma *in,
209     struct ChppWwanSignalStrengthWcdma *out) {
210   out->signalStrength = in->signalStrength;
211   out->bitErrorRate = in->bitErrorRate;
212 }
213 
chppWwanConvertCellInfoWcdmaFromChre(const struct chreWwanCellInfoWcdma * in,struct ChppWwanCellInfoWcdma * out)214 static void chppWwanConvertCellInfoWcdmaFromChre(
215     const struct chreWwanCellInfoWcdma *in, struct ChppWwanCellInfoWcdma *out) {
216   chppWwanConvertCellIdentityWcdmaFromChre(&in->cellIdentityWcdma,
217                                            &out->cellIdentityWcdma);
218   chppWwanConvertSignalStrengthWcdmaFromChre(&in->signalStrengthWcdma,
219                                              &out->signalStrengthWcdma);
220 }
221 
chppWwanConvertCellInfoFromChre(const struct chreWwanCellInfo * in,struct ChppWwanCellInfo * out)222 static void chppWwanConvertCellInfoFromChre(const struct chreWwanCellInfo *in,
223                                             struct ChppWwanCellInfo *out) {
224   out->timeStamp = in->timeStamp;
225   out->cellInfoType = in->cellInfoType;
226   out->timeStampType = in->timeStampType;
227   out->registered = in->registered;
228   out->reserved = 0;
229   memset(&out->CellInfo, 0, sizeof(out->CellInfo));
230   switch (in->cellInfoType) {
231     case CHRE_WWAN_CELL_INFO_TYPE_GSM:
232       chppWwanConvertCellInfoGsmFromChre(&in->CellInfo.gsm, &out->CellInfo.gsm);
233       break;
234     case CHRE_WWAN_CELL_INFO_TYPE_CDMA:
235       chppWwanConvertCellInfoCdmaFromChre(&in->CellInfo.cdma,
236                                           &out->CellInfo.cdma);
237       break;
238     case CHRE_WWAN_CELL_INFO_TYPE_LTE:
239       chppWwanConvertCellInfoLteFromChre(&in->CellInfo.lte, &out->CellInfo.lte);
240       break;
241     case CHRE_WWAN_CELL_INFO_TYPE_WCDMA:
242       chppWwanConvertCellInfoWcdmaFromChre(&in->CellInfo.wcdma,
243                                            &out->CellInfo.wcdma);
244       break;
245     case CHRE_WWAN_CELL_INFO_TYPE_TD_SCDMA:
246       chppWwanConvertCellInfoTdscdmaFromChre(&in->CellInfo.tdscdma,
247                                              &out->CellInfo.tdscdma);
248       break;
249     case CHRE_WWAN_CELL_INFO_TYPE_NR:
250       chppWwanConvertCellInfoNrFromChre(&in->CellInfo.nr, &out->CellInfo.nr);
251       break;
252     default:
253       CHPP_ASSERT(false);
254   }
255 }
256 
chppWwanConvertCellInfoResultFromChre(const struct chreWwanCellInfoResult * in,struct ChppWwanCellInfoResult * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)257 static void chppWwanConvertCellInfoResultFromChre(
258     const struct chreWwanCellInfoResult *in, struct ChppWwanCellInfoResult *out,
259     uint8_t *payload, size_t payloadSize, uint16_t *vlaOffset) {
260   out->version = CHRE_WWAN_CELL_INFO_RESULT_VERSION;
261   out->errorCode = in->errorCode;
262   out->cellInfoCount = in->cellInfoCount;
263   out->reserved = 0;
264   out->cookie = 0;
265 
266   struct ChppWwanCellInfo *cells =
267       (struct ChppWwanCellInfo *)&payload[*vlaOffset];
268   out->cells.length = in->cellInfoCount * sizeof(struct ChppWwanCellInfo);
269   CHPP_ASSERT((size_t)(*vlaOffset + out->cells.length) <= payloadSize);
270   if (out->cells.length > 0 && *vlaOffset + out->cells.length <= payloadSize) {
271     for (size_t i = 0; i < in->cellInfoCount; i++) {
272       chppWwanConvertCellInfoFromChre(&in->cells[i], &cells[i]);
273     }
274     out->cells.offset = *vlaOffset;
275     *vlaOffset += out->cells.length;
276   } else {
277     out->cells.offset = 0;
278   }
279 }
280 
281 // Encoding (CHRE --> CHPP) top-level functions
282 
chppWwanCellInfoResultFromChre(const struct chreWwanCellInfoResult * in,struct ChppWwanCellInfoResultWithHeader ** out,size_t * outSize)283 bool chppWwanCellInfoResultFromChre(
284     const struct chreWwanCellInfoResult *in,
285     struct ChppWwanCellInfoResultWithHeader **out, size_t *outSize) {
286   CHPP_NOT_NULL(out);
287   CHPP_NOT_NULL(outSize);
288 
289   size_t payloadSize = chppWwanSizeOfCellInfoResultFromChre(in);
290   *out = chppMalloc(payloadSize);
291   if (*out != NULL) {
292     uint8_t *payload = (uint8_t *)&(*out)->payload;
293     uint16_t vlaOffset = sizeof(struct ChppWwanCellInfoResult);
294     chppWwanConvertCellInfoResultFromChre(in, &(*out)->payload, payload,
295                                           payloadSize, &vlaOffset);
296     *outSize = payloadSize;
297     return true;
298   }
299   return false;
300 }
301 
302 // Decoding (CHPP --> CHRE) conversion functions
303 
chppWwanConvertCellIdentityCdmaToChre(const struct ChppWwanCellIdentityCdma * in,struct chreWwanCellIdentityCdma * out)304 static bool chppWwanConvertCellIdentityCdmaToChre(
305     const struct ChppWwanCellIdentityCdma *in,
306     struct chreWwanCellIdentityCdma *out) {
307   out->networkId = in->networkId;
308   out->systemId = in->systemId;
309   out->basestationId = in->basestationId;
310   out->longitude = in->longitude;
311   out->latitude = in->latitude;
312 
313   return true;
314 }
315 
chppWwanConvertSignalStrengthCdmaToChre(const struct ChppWwanSignalStrengthCdma * in,struct chreWwanSignalStrengthCdma * out)316 static bool chppWwanConvertSignalStrengthCdmaToChre(
317     const struct ChppWwanSignalStrengthCdma *in,
318     struct chreWwanSignalStrengthCdma *out) {
319   out->dbm = in->dbm;
320   out->ecio = in->ecio;
321 
322   return true;
323 }
324 
chppWwanConvertSignalStrengthEvdoToChre(const struct ChppWwanSignalStrengthEvdo * in,struct chreWwanSignalStrengthEvdo * out)325 static bool chppWwanConvertSignalStrengthEvdoToChre(
326     const struct ChppWwanSignalStrengthEvdo *in,
327     struct chreWwanSignalStrengthEvdo *out) {
328   out->dbm = in->dbm;
329   out->ecio = in->ecio;
330   out->signalNoiseRatio = in->signalNoiseRatio;
331 
332   return true;
333 }
334 
chppWwanConvertCellInfoCdmaToChre(const struct ChppWwanCellInfoCdma * in,struct chreWwanCellInfoCdma * out)335 static bool chppWwanConvertCellInfoCdmaToChre(
336     const struct ChppWwanCellInfoCdma *in, struct chreWwanCellInfoCdma *out) {
337   if (!chppWwanConvertCellIdentityCdmaToChre(&in->cellIdentityCdma,
338                                              &out->cellIdentityCdma)) {
339     return false;
340   }
341   if (!chppWwanConvertSignalStrengthCdmaToChre(&in->signalStrengthCdma,
342                                                &out->signalStrengthCdma)) {
343     return false;
344   }
345   if (!chppWwanConvertSignalStrengthEvdoToChre(&in->signalStrengthEvdo,
346                                                &out->signalStrengthEvdo)) {
347     return false;
348   }
349 
350   return true;
351 }
352 
chppWwanConvertCellIdentityGsmToChre(const struct ChppWwanCellIdentityGsm * in,struct chreWwanCellIdentityGsm * out)353 static bool chppWwanConvertCellIdentityGsmToChre(
354     const struct ChppWwanCellIdentityGsm *in,
355     struct chreWwanCellIdentityGsm *out) {
356   out->mcc = in->mcc;
357   out->mnc = in->mnc;
358   out->lac = in->lac;
359   out->cid = in->cid;
360   out->arfcn = in->arfcn;
361   out->bsic = in->bsic;
362   memset(&out->reserved, 0, sizeof(out->reserved));
363 
364   return true;
365 }
366 
chppWwanConvertSignalStrengthGsmToChre(const struct ChppWwanSignalStrengthGsm * in,struct chreWwanSignalStrengthGsm * out)367 static bool chppWwanConvertSignalStrengthGsmToChre(
368     const struct ChppWwanSignalStrengthGsm *in,
369     struct chreWwanSignalStrengthGsm *out) {
370   out->signalStrength = in->signalStrength;
371   out->bitErrorRate = in->bitErrorRate;
372   out->timingAdvance = in->timingAdvance;
373 
374   return true;
375 }
376 
chppWwanConvertCellInfoGsmToChre(const struct ChppWwanCellInfoGsm * in,struct chreWwanCellInfoGsm * out)377 static bool chppWwanConvertCellInfoGsmToChre(
378     const struct ChppWwanCellInfoGsm *in, struct chreWwanCellInfoGsm *out) {
379   if (!chppWwanConvertCellIdentityGsmToChre(&in->cellIdentityGsm,
380                                             &out->cellIdentityGsm)) {
381     return false;
382   }
383   if (!chppWwanConvertSignalStrengthGsmToChre(&in->signalStrengthGsm,
384                                               &out->signalStrengthGsm)) {
385     return false;
386   }
387 
388   return true;
389 }
390 
chppWwanConvertCellIdentityLteToChre(const struct ChppWwanCellIdentityLte * in,struct chreWwanCellIdentityLte * out)391 static bool chppWwanConvertCellIdentityLteToChre(
392     const struct ChppWwanCellIdentityLte *in,
393     struct chreWwanCellIdentityLte *out) {
394   out->mcc = in->mcc;
395   out->mnc = in->mnc;
396   out->ci = in->ci;
397   out->pci = in->pci;
398   out->tac = in->tac;
399   out->earfcn = in->earfcn;
400 
401   return true;
402 }
403 
chppWwanConvertSignalStrengthLteToChre(const struct ChppWwanSignalStrengthLte * in,struct chreWwanSignalStrengthLte * out)404 static bool chppWwanConvertSignalStrengthLteToChre(
405     const struct ChppWwanSignalStrengthLte *in,
406     struct chreWwanSignalStrengthLte *out) {
407   out->signalStrength = in->signalStrength;
408   out->rsrp = in->rsrp;
409   out->rsrq = in->rsrq;
410   out->rssnr = in->rssnr;
411   out->cqi = in->cqi;
412   out->timingAdvance = in->timingAdvance;
413 
414   return true;
415 }
416 
chppWwanConvertCellInfoLteToChre(const struct ChppWwanCellInfoLte * in,struct chreWwanCellInfoLte * out)417 static bool chppWwanConvertCellInfoLteToChre(
418     const struct ChppWwanCellInfoLte *in, struct chreWwanCellInfoLte *out) {
419   if (!chppWwanConvertCellIdentityLteToChre(&in->cellIdentityLte,
420                                             &out->cellIdentityLte)) {
421     return false;
422   }
423   if (!chppWwanConvertSignalStrengthLteToChre(&in->signalStrengthLte,
424                                               &out->signalStrengthLte)) {
425     return false;
426   }
427 
428   return true;
429 }
430 
chppWwanConvertCellIdentityNrToChre(const struct ChppWwanCellIdentityNr * in,struct chreWwanCellIdentityNr * out)431 static bool chppWwanConvertCellIdentityNrToChre(
432     const struct ChppWwanCellIdentityNr *in,
433     struct chreWwanCellIdentityNr *out) {
434   out->mcc = in->mcc;
435   out->mnc = in->mnc;
436   out->nci0 = in->nci0;
437   out->nci1 = in->nci1;
438   out->pci = in->pci;
439   out->tac = in->tac;
440   out->nrarfcn = in->nrarfcn;
441 
442   return true;
443 }
444 
chppWwanConvertSignalStrengthNrToChre(const struct ChppWwanSignalStrengthNr * in,struct chreWwanSignalStrengthNr * out)445 static bool chppWwanConvertSignalStrengthNrToChre(
446     const struct ChppWwanSignalStrengthNr *in,
447     struct chreWwanSignalStrengthNr *out) {
448   out->ssRsrp = in->ssRsrp;
449   out->ssRsrq = in->ssRsrq;
450   out->ssSinr = in->ssSinr;
451   out->csiRsrp = in->csiRsrp;
452   out->csiRsrq = in->csiRsrq;
453   out->csiSinr = in->csiSinr;
454 
455   return true;
456 }
457 
chppWwanConvertCellInfoNrToChre(const struct ChppWwanCellInfoNr * in,struct chreWwanCellInfoNr * out)458 static bool chppWwanConvertCellInfoNrToChre(const struct ChppWwanCellInfoNr *in,
459                                             struct chreWwanCellInfoNr *out) {
460   if (!chppWwanConvertCellIdentityNrToChre(&in->cellIdentityNr,
461                                            &out->cellIdentityNr)) {
462     return false;
463   }
464   if (!chppWwanConvertSignalStrengthNrToChre(&in->signalStrengthNr,
465                                              &out->signalStrengthNr)) {
466     return false;
467   }
468 
469   return true;
470 }
471 
chppWwanConvertCellIdentityTdscdmaToChre(const struct ChppWwanCellIdentityTdscdma * in,struct chreWwanCellIdentityTdscdma * out)472 static bool chppWwanConvertCellIdentityTdscdmaToChre(
473     const struct ChppWwanCellIdentityTdscdma *in,
474     struct chreWwanCellIdentityTdscdma *out) {
475   out->mcc = in->mcc;
476   out->mnc = in->mnc;
477   out->lac = in->lac;
478   out->cid = in->cid;
479   out->cpid = in->cpid;
480 
481   return true;
482 }
483 
chppWwanConvertSignalStrengthTdscdmaToChre(const struct ChppWwanSignalStrengthTdscdma * in,struct chreWwanSignalStrengthTdscdma * out)484 static bool chppWwanConvertSignalStrengthTdscdmaToChre(
485     const struct ChppWwanSignalStrengthTdscdma *in,
486     struct chreWwanSignalStrengthTdscdma *out) {
487   out->rscp = in->rscp;
488 
489   return true;
490 }
491 
chppWwanConvertCellInfoTdscdmaToChre(const struct ChppWwanCellInfoTdscdma * in,struct chreWwanCellInfoTdscdma * out)492 static bool chppWwanConvertCellInfoTdscdmaToChre(
493     const struct ChppWwanCellInfoTdscdma *in,
494     struct chreWwanCellInfoTdscdma *out) {
495   if (!chppWwanConvertCellIdentityTdscdmaToChre(&in->cellIdentityTdscdma,
496                                                 &out->cellIdentityTdscdma)) {
497     return false;
498   }
499   if (!chppWwanConvertSignalStrengthTdscdmaToChre(
500           &in->signalStrengthTdscdma, &out->signalStrengthTdscdma)) {
501     return false;
502   }
503 
504   return true;
505 }
506 
chppWwanConvertCellIdentityWcdmaToChre(const struct ChppWwanCellIdentityWcdma * in,struct chreWwanCellIdentityWcdma * out)507 static bool chppWwanConvertCellIdentityWcdmaToChre(
508     const struct ChppWwanCellIdentityWcdma *in,
509     struct chreWwanCellIdentityWcdma *out) {
510   out->mcc = in->mcc;
511   out->mnc = in->mnc;
512   out->lac = in->lac;
513   out->cid = in->cid;
514   out->psc = in->psc;
515   out->uarfcn = in->uarfcn;
516 
517   return true;
518 }
519 
chppWwanConvertSignalStrengthWcdmaToChre(const struct ChppWwanSignalStrengthWcdma * in,struct chreWwanSignalStrengthWcdma * out)520 static bool chppWwanConvertSignalStrengthWcdmaToChre(
521     const struct ChppWwanSignalStrengthWcdma *in,
522     struct chreWwanSignalStrengthWcdma *out) {
523   out->signalStrength = in->signalStrength;
524   out->bitErrorRate = in->bitErrorRate;
525 
526   return true;
527 }
528 
chppWwanConvertCellInfoWcdmaToChre(const struct ChppWwanCellInfoWcdma * in,struct chreWwanCellInfoWcdma * out)529 static bool chppWwanConvertCellInfoWcdmaToChre(
530     const struct ChppWwanCellInfoWcdma *in, struct chreWwanCellInfoWcdma *out) {
531   if (!chppWwanConvertCellIdentityWcdmaToChre(&in->cellIdentityWcdma,
532                                               &out->cellIdentityWcdma)) {
533     return false;
534   }
535   if (!chppWwanConvertSignalStrengthWcdmaToChre(&in->signalStrengthWcdma,
536                                                 &out->signalStrengthWcdma)) {
537     return false;
538   }
539 
540   return true;
541 }
542 
chppWwanConvertCellInfoToChre(const struct ChppWwanCellInfo * in,struct chreWwanCellInfo * out)543 static bool chppWwanConvertCellInfoToChre(const struct ChppWwanCellInfo *in,
544                                           struct chreWwanCellInfo *out) {
545   out->timeStamp = in->timeStamp;
546   out->cellInfoType = in->cellInfoType;
547   out->timeStampType = in->timeStampType;
548   out->registered = in->registered;
549   out->reserved = 0;
550   memset(&out->CellInfo, 0, sizeof(out->CellInfo));
551   switch (in->cellInfoType) {
552     case CHRE_WWAN_CELL_INFO_TYPE_GSM:
553       if (!chppWwanConvertCellInfoGsmToChre(&in->CellInfo.gsm,
554                                             &out->CellInfo.gsm)) {
555         return false;
556       }
557       break;
558     case CHRE_WWAN_CELL_INFO_TYPE_CDMA:
559       if (!chppWwanConvertCellInfoCdmaToChre(&in->CellInfo.cdma,
560                                              &out->CellInfo.cdma)) {
561         return false;
562       }
563       break;
564     case CHRE_WWAN_CELL_INFO_TYPE_LTE:
565       if (!chppWwanConvertCellInfoLteToChre(&in->CellInfo.lte,
566                                             &out->CellInfo.lte)) {
567         return false;
568       }
569       break;
570     case CHRE_WWAN_CELL_INFO_TYPE_WCDMA:
571       if (!chppWwanConvertCellInfoWcdmaToChre(&in->CellInfo.wcdma,
572                                               &out->CellInfo.wcdma)) {
573         return false;
574       }
575       break;
576     case CHRE_WWAN_CELL_INFO_TYPE_TD_SCDMA:
577       if (!chppWwanConvertCellInfoTdscdmaToChre(&in->CellInfo.tdscdma,
578                                                 &out->CellInfo.tdscdma)) {
579         return false;
580       }
581       break;
582     case CHRE_WWAN_CELL_INFO_TYPE_NR:
583       if (!chppWwanConvertCellInfoNrToChre(&in->CellInfo.nr,
584                                            &out->CellInfo.nr)) {
585         return false;
586       }
587       break;
588     default:
589       CHPP_ASSERT(false);
590   }
591 
592   return true;
593 }
594 
chppWwanConvertCellInfoResultToChre(const struct ChppWwanCellInfoResult * in,struct chreWwanCellInfoResult * out,size_t inSize)595 static bool chppWwanConvertCellInfoResultToChre(
596     const struct ChppWwanCellInfoResult *in, struct chreWwanCellInfoResult *out,
597     size_t inSize) {
598   out->version = CHRE_WWAN_CELL_INFO_RESULT_VERSION;
599   out->errorCode = in->errorCode;
600   out->cellInfoCount = in->cellInfoCount;
601   out->reserved = 0;
602   out->cookie = 0;
603 
604   if (in->cells.length == 0) {
605     out->cells = NULL;
606   } else {
607     if (in->cells.offset + in->cells.length > inSize ||
608         in->cells.length !=
609             in->cellInfoCount * sizeof(struct ChppWwanCellInfo)) {
610       return false;
611     }
612 
613     const struct ChppWwanCellInfo *cellsIn = (const struct ChppWwanCellInfo *)&(
614         (const uint8_t *)in)[in->cells.offset];
615 
616     struct chreWwanCellInfo *cellsOut =
617         chppMalloc(in->cellInfoCount * sizeof(struct chreWwanCellInfo));
618     if (cellsOut == NULL) {
619       return false;
620     }
621 
622     for (size_t i = 0; i < in->cellInfoCount; i++) {
623       if (!chppWwanConvertCellInfoToChre(&cellsIn[i], &cellsOut[i])) {
624         return false;
625       }
626     }
627     out->cells = cellsOut;
628   }
629 
630   return true;
631 }
632 
633 // Decoding (CHPP --> CHRE) top-level functions
634 
chppWwanCellInfoResultToChre(const struct ChppWwanCellInfoResult * in,size_t inSize)635 struct chreWwanCellInfoResult *chppWwanCellInfoResultToChre(
636     const struct ChppWwanCellInfoResult *in, size_t inSize) {
637   struct chreWwanCellInfoResult *out = NULL;
638 
639   if (inSize >= sizeof(struct ChppWwanCellInfoResult)) {
640     out = chppMalloc(sizeof(struct chreWwanCellInfoResult));
641     if (out != NULL) {
642       if (!chppWwanConvertCellInfoResultToChre(in, out, inSize)) {
643         CHPP_FREE_AND_NULLIFY(out);
644       }
645     }
646   }
647 
648   return out;
649 }
650