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/wifi.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/wifi_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 //! chreWifiScanEvent along with the CHPP header as
36 //! struct ChppWifiScanEventWithHeader
chppWifiSizeOfScanEventFromChre(const struct chreWifiScanEvent * scanEvent)37 static size_t chppWifiSizeOfScanEventFromChre(
38 const struct chreWifiScanEvent *scanEvent) {
39 size_t encodedSize = sizeof(struct ChppWifiScanEventWithHeader);
40 encodedSize += scanEvent->scannedFreqListLen * sizeof(uint32_t);
41 encodedSize += scanEvent->resultCount * sizeof(struct ChppWifiScanResult);
42 return encodedSize;
43 }
44
45 //! @return number of bytes required to represent the given
46 //! chreWifiScanParams along with the CHPP header as
47 //! struct ChppWifiScanParamsWithHeader
chppWifiSizeOfScanParamsFromChre(const struct chreWifiScanParams * scanParams)48 static size_t chppWifiSizeOfScanParamsFromChre(
49 const struct chreWifiScanParams *scanParams) {
50 size_t encodedSize = sizeof(struct ChppWifiScanParamsWithHeader);
51 encodedSize += scanParams->frequencyListLen * sizeof(uint32_t);
52 encodedSize += scanParams->ssidListLen * sizeof(struct ChppWifiSsidListItem);
53 return encodedSize;
54 }
55
56 //! @return number of bytes required to represent the given
57 //! chreWifiRangingEvent along with the CHPP header as
58 //! struct ChppWifiRangingEventWithHeader
chppWifiSizeOfRangingEventFromChre(const struct chreWifiRangingEvent * rangingEvent)59 static size_t chppWifiSizeOfRangingEventFromChre(
60 const struct chreWifiRangingEvent *rangingEvent) {
61 size_t encodedSize = sizeof(struct ChppWifiRangingEventWithHeader);
62 encodedSize +=
63 rangingEvent->resultCount * sizeof(struct ChppWifiRangingResult);
64 return encodedSize;
65 }
66
67 //! @return number of bytes required to represent the given
68 //! chreWifiRangingParams along with the CHPP header as
69 //! struct ChppWifiRangingParamsWithHeader
chppWifiSizeOfRangingParamsFromChre(const struct chreWifiRangingParams * rangingParams)70 static size_t chppWifiSizeOfRangingParamsFromChre(
71 const struct chreWifiRangingParams *rangingParams) {
72 size_t encodedSize = sizeof(struct ChppWifiRangingParamsWithHeader);
73 encodedSize +=
74 rangingParams->targetListLen * sizeof(struct ChppWifiRangingTarget);
75 return encodedSize;
76 }
77
78 // Encoding (CHRE --> CHPP) conversion functions
79
chppWifiConvertScanResultFromChre(const struct chreWifiScanResult * in,struct ChppWifiScanResult * out)80 static void chppWifiConvertScanResultFromChre(
81 const struct chreWifiScanResult *in, struct ChppWifiScanResult *out) {
82 out->ageMs = in->ageMs;
83 out->capabilityInfo = in->capabilityInfo;
84 out->ssidLen = in->ssidLen;
85 memcpy(out->ssid, in->ssid, sizeof(out->ssid));
86 memcpy(out->bssid, in->bssid, sizeof(out->bssid));
87 out->flags = in->flags;
88 out->rssi = in->rssi;
89 out->band = in->band;
90 out->primaryChannel = in->primaryChannel;
91 out->centerFreqPrimary = in->centerFreqPrimary;
92 out->centerFreqSecondary = in->centerFreqSecondary;
93 out->channelWidth = in->channelWidth;
94 out->securityMode = in->securityMode;
95 out->radioChain = in->radioChain;
96 out->rssiChain0 = in->rssiChain0;
97 out->rssiChain1 = in->rssiChain1;
98 memset(&out->reserved, 0, sizeof(out->reserved));
99 }
100
chppWifiConvertScanEventFromChre(const struct chreWifiScanEvent * in,struct ChppWifiScanEvent * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)101 static void chppWifiConvertScanEventFromChre(const struct chreWifiScanEvent *in,
102 struct ChppWifiScanEvent *out,
103 uint8_t *payload,
104 size_t payloadSize,
105 uint16_t *vlaOffset) {
106 out->version = CHRE_WIFI_SCAN_EVENT_VERSION;
107 out->resultCount = in->resultCount;
108 out->resultTotal = in->resultTotal;
109 out->eventIndex = in->eventIndex;
110 out->scanType = in->scanType;
111 out->ssidSetSize = in->ssidSetSize;
112 out->scannedFreqListLen = in->scannedFreqListLen;
113 out->referenceTime = in->referenceTime;
114 out->scannedFreqList.length = in->scannedFreqListLen * sizeof(uint32_t);
115 CHPP_ASSERT((size_t)(*vlaOffset + out->scannedFreqList.length) <=
116 payloadSize);
117 if (out->scannedFreqList.length > 0 &&
118 *vlaOffset + out->scannedFreqList.length <= payloadSize) {
119 memcpy(&payload[*vlaOffset], in->scannedFreqList,
120 in->scannedFreqListLen * sizeof(uint32_t));
121 out->scannedFreqList.offset = *vlaOffset;
122 *vlaOffset += out->scannedFreqList.length;
123 } else {
124 out->scannedFreqList.offset = 0;
125 }
126
127 struct ChppWifiScanResult *results =
128 (struct ChppWifiScanResult *)&payload[*vlaOffset];
129 out->results.length = in->resultCount * sizeof(struct ChppWifiScanResult);
130 CHPP_ASSERT((size_t)(*vlaOffset + out->results.length) <= payloadSize);
131 if (out->results.length > 0 &&
132 *vlaOffset + out->results.length <= payloadSize) {
133 for (size_t i = 0; i < in->resultCount; i++) {
134 chppWifiConvertScanResultFromChre(&in->results[i], &results[i]);
135 }
136 out->results.offset = *vlaOffset;
137 *vlaOffset += out->results.length;
138 } else {
139 out->results.offset = 0;
140 }
141 out->radioChainPref = in->radioChainPref;
142 }
143
chppWifiConvertSsidListItemFromChre(const struct chreWifiSsidListItem * in,struct ChppWifiSsidListItem * out)144 static void chppWifiConvertSsidListItemFromChre(
145 const struct chreWifiSsidListItem *in, struct ChppWifiSsidListItem *out) {
146 out->ssidLen = in->ssidLen;
147 memcpy(out->ssid, in->ssid, sizeof(out->ssid));
148 }
149
chppWifiConvertScanParamsFromChre(const struct chreWifiScanParams * in,struct ChppWifiScanParams * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)150 static void chppWifiConvertScanParamsFromChre(
151 const struct chreWifiScanParams *in, struct ChppWifiScanParams *out,
152 uint8_t *payload, size_t payloadSize, uint16_t *vlaOffset) {
153 out->scanType = in->scanType;
154 out->maxScanAgeMs = in->maxScanAgeMs;
155 out->frequencyListLen = in->frequencyListLen;
156 out->frequencyList.length = in->frequencyListLen * sizeof(uint32_t);
157 CHPP_ASSERT((size_t)(*vlaOffset + out->frequencyList.length) <= payloadSize);
158 if (out->frequencyList.length > 0 &&
159 *vlaOffset + out->frequencyList.length <= payloadSize) {
160 memcpy(&payload[*vlaOffset], in->frequencyList,
161 in->frequencyListLen * sizeof(uint32_t));
162 out->frequencyList.offset = *vlaOffset;
163 *vlaOffset += out->frequencyList.length;
164 } else {
165 out->frequencyList.offset = 0;
166 }
167 out->ssidListLen = in->ssidListLen;
168
169 struct ChppWifiSsidListItem *ssidList =
170 (struct ChppWifiSsidListItem *)&payload[*vlaOffset];
171 out->ssidList.length = in->ssidListLen * sizeof(struct ChppWifiSsidListItem);
172 CHPP_ASSERT((size_t)(*vlaOffset + out->ssidList.length) <= payloadSize);
173 if (out->ssidList.length > 0 &&
174 *vlaOffset + out->ssidList.length <= payloadSize) {
175 for (size_t i = 0; i < in->ssidListLen; i++) {
176 chppWifiConvertSsidListItemFromChre(&in->ssidList[i], &ssidList[i]);
177 }
178 out->ssidList.offset = *vlaOffset;
179 *vlaOffset += out->ssidList.length;
180 } else {
181 out->ssidList.offset = 0;
182 }
183 out->radioChainPref = in->radioChainPref;
184 out->channelSet = in->channelSet;
185 }
186
chppWifiConvertLciFromChre(const struct chreWifiLci * in,struct ChppWifiLci * out)187 static void chppWifiConvertLciFromChre(const struct chreWifiLci *in,
188 struct ChppWifiLci *out) {
189 out->latitude = in->latitude;
190 out->longitude = in->longitude;
191 out->altitude = in->altitude;
192 out->latitudeUncertainty = in->latitudeUncertainty;
193 out->longitudeUncertainty = in->longitudeUncertainty;
194 out->altitudeType = in->altitudeType;
195 out->altitudeUncertainty = in->altitudeUncertainty;
196 }
197
chppWifiConvertRangingResultFromChre(const struct chreWifiRangingResult * in,struct ChppWifiRangingResult * out)198 static void chppWifiConvertRangingResultFromChre(
199 const struct chreWifiRangingResult *in, struct ChppWifiRangingResult *out) {
200 out->timestamp = in->timestamp;
201 memcpy(out->macAddress, in->macAddress, sizeof(out->macAddress));
202 out->status = in->status;
203 out->rssi = in->rssi;
204 out->distance = in->distance;
205 out->distanceStdDev = in->distanceStdDev;
206 chppWifiConvertLciFromChre(&in->lci, &out->lci);
207 out->flags = in->flags;
208 memset(&out->reserved, 0, sizeof(out->reserved));
209 }
210
chppWifiConvertRangingEventFromChre(const struct chreWifiRangingEvent * in,struct ChppWifiRangingEvent * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)211 static void chppWifiConvertRangingEventFromChre(
212 const struct chreWifiRangingEvent *in, struct ChppWifiRangingEvent *out,
213 uint8_t *payload, size_t payloadSize, uint16_t *vlaOffset) {
214 out->version = CHRE_WIFI_RANGING_EVENT_VERSION;
215 out->resultCount = in->resultCount;
216 memset(&out->reserved, 0, sizeof(out->reserved));
217
218 struct ChppWifiRangingResult *results =
219 (struct ChppWifiRangingResult *)&payload[*vlaOffset];
220 out->results.length = in->resultCount * sizeof(struct ChppWifiRangingResult);
221 CHPP_ASSERT((size_t)(*vlaOffset + out->results.length) <= payloadSize);
222 if (out->results.length > 0 &&
223 *vlaOffset + out->results.length <= payloadSize) {
224 for (size_t i = 0; i < in->resultCount; i++) {
225 chppWifiConvertRangingResultFromChre(&in->results[i], &results[i]);
226 }
227 out->results.offset = *vlaOffset;
228 *vlaOffset += out->results.length;
229 } else {
230 out->results.offset = 0;
231 }
232 }
233
chppWifiConvertRangingTargetFromChre(const struct chreWifiRangingTarget * in,struct ChppWifiRangingTarget * out)234 static void chppWifiConvertRangingTargetFromChre(
235 const struct chreWifiRangingTarget *in, struct ChppWifiRangingTarget *out) {
236 memcpy(out->macAddress, in->macAddress, sizeof(out->macAddress));
237 out->primaryChannel = in->primaryChannel;
238 out->centerFreqPrimary = in->centerFreqPrimary;
239 out->centerFreqSecondary = in->centerFreqSecondary;
240 out->channelWidth = in->channelWidth;
241 memset(&out->reserved, 0, sizeof(out->reserved));
242 }
243
chppWifiConvertRangingParamsFromChre(const struct chreWifiRangingParams * in,struct ChppWifiRangingParams * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)244 static void chppWifiConvertRangingParamsFromChre(
245 const struct chreWifiRangingParams *in, struct ChppWifiRangingParams *out,
246 uint8_t *payload, size_t payloadSize, uint16_t *vlaOffset) {
247 out->targetListLen = in->targetListLen;
248
249 struct ChppWifiRangingTarget *targetList =
250 (struct ChppWifiRangingTarget *)&payload[*vlaOffset];
251 out->targetList.length =
252 in->targetListLen * sizeof(struct ChppWifiRangingTarget);
253 CHPP_ASSERT((size_t)(*vlaOffset + out->targetList.length) <= payloadSize);
254 if (out->targetList.length > 0 &&
255 *vlaOffset + out->targetList.length <= payloadSize) {
256 for (size_t i = 0; i < in->targetListLen; i++) {
257 chppWifiConvertRangingTargetFromChre(&in->targetList[i], &targetList[i]);
258 }
259 out->targetList.offset = *vlaOffset;
260 *vlaOffset += out->targetList.length;
261 } else {
262 out->targetList.offset = 0;
263 }
264 }
265
266 // Encoding (CHRE --> CHPP) top-level functions
267
chppWifiScanEventFromChre(const struct chreWifiScanEvent * in,struct ChppWifiScanEventWithHeader ** out,size_t * outSize)268 bool chppWifiScanEventFromChre(const struct chreWifiScanEvent *in,
269 struct ChppWifiScanEventWithHeader **out,
270 size_t *outSize) {
271 CHPP_NOT_NULL(out);
272 CHPP_NOT_NULL(outSize);
273
274 size_t payloadSize = chppWifiSizeOfScanEventFromChre(in);
275 *out = chppMalloc(payloadSize);
276 if (*out != NULL) {
277 uint8_t *payload = (uint8_t *)&(*out)->payload;
278 uint16_t vlaOffset = sizeof(struct ChppWifiScanEvent);
279 chppWifiConvertScanEventFromChre(in, &(*out)->payload, payload, payloadSize,
280 &vlaOffset);
281 *outSize = payloadSize;
282 return true;
283 }
284 return false;
285 }
286
chppWifiScanParamsFromChre(const struct chreWifiScanParams * in,struct ChppWifiScanParamsWithHeader ** out,size_t * outSize)287 bool chppWifiScanParamsFromChre(const struct chreWifiScanParams *in,
288 struct ChppWifiScanParamsWithHeader **out,
289 size_t *outSize) {
290 CHPP_NOT_NULL(out);
291 CHPP_NOT_NULL(outSize);
292
293 size_t payloadSize = chppWifiSizeOfScanParamsFromChre(in);
294 *out = chppMalloc(payloadSize);
295 if (*out != NULL) {
296 uint8_t *payload = (uint8_t *)&(*out)->payload;
297 uint16_t vlaOffset = sizeof(struct ChppWifiScanParams);
298 chppWifiConvertScanParamsFromChre(in, &(*out)->payload, payload,
299 payloadSize, &vlaOffset);
300 *outSize = payloadSize;
301 return true;
302 }
303 return false;
304 }
305
chppWifiRangingEventFromChre(const struct chreWifiRangingEvent * in,struct ChppWifiRangingEventWithHeader ** out,size_t * outSize)306 bool chppWifiRangingEventFromChre(const struct chreWifiRangingEvent *in,
307 struct ChppWifiRangingEventWithHeader **out,
308 size_t *outSize) {
309 CHPP_NOT_NULL(out);
310 CHPP_NOT_NULL(outSize);
311
312 size_t payloadSize = chppWifiSizeOfRangingEventFromChre(in);
313 *out = chppMalloc(payloadSize);
314 if (*out != NULL) {
315 uint8_t *payload = (uint8_t *)&(*out)->payload;
316 uint16_t vlaOffset = sizeof(struct ChppWifiRangingEvent);
317 chppWifiConvertRangingEventFromChre(in, &(*out)->payload, payload,
318 payloadSize, &vlaOffset);
319 *outSize = payloadSize;
320 return true;
321 }
322 return false;
323 }
324
chppWifiRangingParamsFromChre(const struct chreWifiRangingParams * in,struct ChppWifiRangingParamsWithHeader ** out,size_t * outSize)325 bool chppWifiRangingParamsFromChre(const struct chreWifiRangingParams *in,
326 struct ChppWifiRangingParamsWithHeader **out,
327 size_t *outSize) {
328 CHPP_NOT_NULL(out);
329 CHPP_NOT_NULL(outSize);
330
331 size_t payloadSize = chppWifiSizeOfRangingParamsFromChre(in);
332 *out = chppMalloc(payloadSize);
333 if (*out != NULL) {
334 uint8_t *payload = (uint8_t *)&(*out)->payload;
335 uint16_t vlaOffset = sizeof(struct ChppWifiRangingParams);
336 chppWifiConvertRangingParamsFromChre(in, &(*out)->payload, payload,
337 payloadSize, &vlaOffset);
338 *outSize = payloadSize;
339 return true;
340 }
341 return false;
342 }
343
344 // Decoding (CHPP --> CHRE) conversion functions
345
chppWifiConvertScanResultToChre(const struct ChppWifiScanResult * in,struct chreWifiScanResult * out)346 static bool chppWifiConvertScanResultToChre(const struct ChppWifiScanResult *in,
347 struct chreWifiScanResult *out) {
348 out->ageMs = in->ageMs;
349 out->capabilityInfo = in->capabilityInfo;
350 out->ssidLen = in->ssidLen;
351 memcpy(out->ssid, in->ssid, sizeof(out->ssid));
352 memcpy(out->bssid, in->bssid, sizeof(out->bssid));
353 out->flags = in->flags;
354 out->rssi = in->rssi;
355 out->band = in->band;
356 out->primaryChannel = in->primaryChannel;
357 out->centerFreqPrimary = in->centerFreqPrimary;
358 out->centerFreqSecondary = in->centerFreqSecondary;
359 out->channelWidth = in->channelWidth;
360 out->securityMode = in->securityMode;
361 out->radioChain = in->radioChain;
362 out->rssiChain0 = in->rssiChain0;
363 out->rssiChain1 = in->rssiChain1;
364 memset(&out->reserved, 0, sizeof(out->reserved));
365
366 return true;
367 }
368
chppWifiConvertScanEventToChre(const struct ChppWifiScanEvent * in,struct chreWifiScanEvent * out,size_t inSize)369 static bool chppWifiConvertScanEventToChre(const struct ChppWifiScanEvent *in,
370 struct chreWifiScanEvent *out,
371 size_t inSize) {
372 out->version = CHRE_WIFI_SCAN_EVENT_VERSION;
373 out->resultCount = in->resultCount;
374 out->resultTotal = in->resultTotal;
375 out->eventIndex = in->eventIndex;
376 out->scanType = in->scanType;
377 out->ssidSetSize = in->ssidSetSize;
378 out->scannedFreqListLen = in->scannedFreqListLen;
379 out->referenceTime = in->referenceTime;
380
381 if (in->scannedFreqList.length == 0) {
382 out->scannedFreqList = NULL;
383 } else {
384 if (in->scannedFreqList.offset + in->scannedFreqList.length > inSize ||
385 in->scannedFreqList.length !=
386 in->scannedFreqListLen * sizeof(uint32_t)) {
387 return false;
388 }
389
390 uint32_t *scannedFreqListOut =
391 chppMalloc(in->scannedFreqListLen * sizeof(uint32_t));
392 if (scannedFreqListOut == NULL) {
393 return false;
394 }
395
396 memcpy(scannedFreqListOut,
397 &((const uint8_t *)in)[in->scannedFreqList.offset],
398 in->scannedFreqListLen * sizeof(uint32_t));
399 out->scannedFreqList = scannedFreqListOut;
400 }
401
402 if (in->results.length == 0) {
403 out->results = NULL;
404 } else {
405 if (in->results.offset + in->results.length > inSize ||
406 in->results.length !=
407 in->resultCount * sizeof(struct ChppWifiScanResult)) {
408 return false;
409 }
410
411 const struct ChppWifiScanResult *resultsIn =
412 (const struct ChppWifiScanResult *)&(
413 (const uint8_t *)in)[in->results.offset];
414
415 struct chreWifiScanResult *resultsOut =
416 chppMalloc(in->resultCount * sizeof(struct chreWifiScanResult));
417 if (resultsOut == NULL) {
418 return false;
419 }
420
421 for (size_t i = 0; i < in->resultCount; i++) {
422 if (!chppWifiConvertScanResultToChre(&resultsIn[i], &resultsOut[i])) {
423 return false;
424 }
425 }
426 out->results = resultsOut;
427 }
428
429 out->radioChainPref = in->radioChainPref;
430
431 return true;
432 }
433
chppWifiConvertSsidListItemToChre(const struct ChppWifiSsidListItem * in,struct chreWifiSsidListItem * out)434 static bool chppWifiConvertSsidListItemToChre(
435 const struct ChppWifiSsidListItem *in, struct chreWifiSsidListItem *out) {
436 out->ssidLen = in->ssidLen;
437 memcpy(out->ssid, in->ssid, sizeof(out->ssid));
438
439 return true;
440 }
441
chppWifiConvertScanParamsToChre(const struct ChppWifiScanParams * in,struct chreWifiScanParams * out,size_t inSize)442 static bool chppWifiConvertScanParamsToChre(const struct ChppWifiScanParams *in,
443 struct chreWifiScanParams *out,
444 size_t inSize) {
445 out->scanType = in->scanType;
446 out->maxScanAgeMs = in->maxScanAgeMs;
447 out->frequencyListLen = in->frequencyListLen;
448
449 if (in->frequencyList.length == 0) {
450 out->frequencyList = NULL;
451 } else {
452 if (in->frequencyList.offset + in->frequencyList.length > inSize ||
453 in->frequencyList.length != in->frequencyListLen * sizeof(uint32_t)) {
454 return false;
455 }
456
457 uint32_t *frequencyListOut =
458 chppMalloc(in->frequencyListLen * sizeof(uint32_t));
459 if (frequencyListOut == NULL) {
460 return false;
461 }
462
463 memcpy(frequencyListOut, &((const uint8_t *)in)[in->frequencyList.offset],
464 in->frequencyListLen * sizeof(uint32_t));
465 out->frequencyList = frequencyListOut;
466 }
467
468 out->ssidListLen = in->ssidListLen;
469
470 if (in->ssidList.length == 0) {
471 out->ssidList = NULL;
472 } else {
473 if (in->ssidList.offset + in->ssidList.length > inSize ||
474 in->ssidList.length !=
475 in->ssidListLen * sizeof(struct ChppWifiSsidListItem)) {
476 return false;
477 }
478
479 const struct ChppWifiSsidListItem *ssidListIn =
480 (const struct ChppWifiSsidListItem *)&(
481 (const uint8_t *)in)[in->ssidList.offset];
482
483 struct chreWifiSsidListItem *ssidListOut =
484 chppMalloc(in->ssidListLen * sizeof(struct chreWifiSsidListItem));
485 if (ssidListOut == NULL) {
486 return false;
487 }
488
489 for (size_t i = 0; i < in->ssidListLen; i++) {
490 if (!chppWifiConvertSsidListItemToChre(&ssidListIn[i], &ssidListOut[i])) {
491 return false;
492 }
493 }
494 out->ssidList = ssidListOut;
495 }
496
497 out->radioChainPref = in->radioChainPref;
498 out->channelSet = in->channelSet;
499
500 return true;
501 }
502
chppWifiConvertLciToChre(const struct ChppWifiLci * in,struct chreWifiLci * out)503 static bool chppWifiConvertLciToChre(const struct ChppWifiLci *in,
504 struct chreWifiLci *out) {
505 out->latitude = in->latitude;
506 out->longitude = in->longitude;
507 out->altitude = in->altitude;
508 out->latitudeUncertainty = in->latitudeUncertainty;
509 out->longitudeUncertainty = in->longitudeUncertainty;
510 out->altitudeType = in->altitudeType;
511 out->altitudeUncertainty = in->altitudeUncertainty;
512
513 return true;
514 }
515
chppWifiConvertRangingResultToChre(const struct ChppWifiRangingResult * in,struct chreWifiRangingResult * out)516 static bool chppWifiConvertRangingResultToChre(
517 const struct ChppWifiRangingResult *in, struct chreWifiRangingResult *out) {
518 out->timestamp = in->timestamp;
519 memcpy(out->macAddress, in->macAddress, sizeof(out->macAddress));
520 out->status = in->status;
521 out->rssi = in->rssi;
522 out->distance = in->distance;
523 out->distanceStdDev = in->distanceStdDev;
524 if (!chppWifiConvertLciToChre(&in->lci, &out->lci)) {
525 return false;
526 }
527 out->flags = in->flags;
528 memset(&out->reserved, 0, sizeof(out->reserved));
529
530 return true;
531 }
532
chppWifiConvertRangingEventToChre(const struct ChppWifiRangingEvent * in,struct chreWifiRangingEvent * out,size_t inSize)533 static bool chppWifiConvertRangingEventToChre(
534 const struct ChppWifiRangingEvent *in, struct chreWifiRangingEvent *out,
535 size_t inSize) {
536 out->version = CHRE_WIFI_RANGING_EVENT_VERSION;
537 out->resultCount = in->resultCount;
538 memset(&out->reserved, 0, sizeof(out->reserved));
539
540 if (in->results.length == 0) {
541 out->results = NULL;
542 } else {
543 if (in->results.offset + in->results.length > inSize ||
544 in->results.length !=
545 in->resultCount * sizeof(struct ChppWifiRangingResult)) {
546 return false;
547 }
548
549 const struct ChppWifiRangingResult *resultsIn =
550 (const struct ChppWifiRangingResult *)&(
551 (const uint8_t *)in)[in->results.offset];
552
553 struct chreWifiRangingResult *resultsOut =
554 chppMalloc(in->resultCount * sizeof(struct chreWifiRangingResult));
555 if (resultsOut == NULL) {
556 return false;
557 }
558
559 for (size_t i = 0; i < in->resultCount; i++) {
560 if (!chppWifiConvertRangingResultToChre(&resultsIn[i], &resultsOut[i])) {
561 return false;
562 }
563 }
564 out->results = resultsOut;
565 }
566
567 return true;
568 }
569
chppWifiConvertRangingTargetToChre(const struct ChppWifiRangingTarget * in,struct chreWifiRangingTarget * out)570 static bool chppWifiConvertRangingTargetToChre(
571 const struct ChppWifiRangingTarget *in, struct chreWifiRangingTarget *out) {
572 memcpy(out->macAddress, in->macAddress, sizeof(out->macAddress));
573 out->primaryChannel = in->primaryChannel;
574 out->centerFreqPrimary = in->centerFreqPrimary;
575 out->centerFreqSecondary = in->centerFreqSecondary;
576 out->channelWidth = in->channelWidth;
577 memset(&out->reserved, 0, sizeof(out->reserved));
578
579 return true;
580 }
581
chppWifiConvertRangingParamsToChre(const struct ChppWifiRangingParams * in,struct chreWifiRangingParams * out,size_t inSize)582 static bool chppWifiConvertRangingParamsToChre(
583 const struct ChppWifiRangingParams *in, struct chreWifiRangingParams *out,
584 size_t inSize) {
585 out->targetListLen = in->targetListLen;
586
587 if (in->targetList.length == 0) {
588 out->targetList = NULL;
589 } else {
590 if (in->targetList.offset + in->targetList.length > inSize ||
591 in->targetList.length !=
592 in->targetListLen * sizeof(struct ChppWifiRangingTarget)) {
593 return false;
594 }
595
596 const struct ChppWifiRangingTarget *targetListIn =
597 (const struct ChppWifiRangingTarget *)&(
598 (const uint8_t *)in)[in->targetList.offset];
599
600 struct chreWifiRangingTarget *targetListOut =
601 chppMalloc(in->targetListLen * sizeof(struct chreWifiRangingTarget));
602 if (targetListOut == NULL) {
603 return false;
604 }
605
606 for (size_t i = 0; i < in->targetListLen; i++) {
607 if (!chppWifiConvertRangingTargetToChre(&targetListIn[i],
608 &targetListOut[i])) {
609 return false;
610 }
611 }
612 out->targetList = targetListOut;
613 }
614
615 return true;
616 }
617
618 // Decoding (CHPP --> CHRE) top-level functions
619
chppWifiScanEventToChre(const struct ChppWifiScanEvent * in,size_t inSize)620 struct chreWifiScanEvent *chppWifiScanEventToChre(
621 const struct ChppWifiScanEvent *in, size_t inSize) {
622 struct chreWifiScanEvent *out = NULL;
623
624 if (inSize >= sizeof(struct ChppWifiScanEvent)) {
625 out = chppMalloc(sizeof(struct chreWifiScanEvent));
626 if (out != NULL) {
627 if (!chppWifiConvertScanEventToChre(in, out, inSize)) {
628 CHPP_FREE_AND_NULLIFY(out);
629 }
630 }
631 }
632
633 return out;
634 }
chppWifiScanParamsToChre(const struct ChppWifiScanParams * in,size_t inSize)635 struct chreWifiScanParams *chppWifiScanParamsToChre(
636 const struct ChppWifiScanParams *in, size_t inSize) {
637 struct chreWifiScanParams *out = NULL;
638
639 if (inSize >= sizeof(struct ChppWifiScanParams)) {
640 out = chppMalloc(sizeof(struct chreWifiScanParams));
641 if (out != NULL) {
642 if (!chppWifiConvertScanParamsToChre(in, out, inSize)) {
643 CHPP_FREE_AND_NULLIFY(out);
644 }
645 }
646 }
647
648 return out;
649 }
chppWifiRangingEventToChre(const struct ChppWifiRangingEvent * in,size_t inSize)650 struct chreWifiRangingEvent *chppWifiRangingEventToChre(
651 const struct ChppWifiRangingEvent *in, size_t inSize) {
652 struct chreWifiRangingEvent *out = NULL;
653
654 if (inSize >= sizeof(struct ChppWifiRangingEvent)) {
655 out = chppMalloc(sizeof(struct chreWifiRangingEvent));
656 if (out != NULL) {
657 if (!chppWifiConvertRangingEventToChre(in, out, inSize)) {
658 CHPP_FREE_AND_NULLIFY(out);
659 }
660 }
661 }
662
663 return out;
664 }
chppWifiRangingParamsToChre(const struct ChppWifiRangingParams * in,size_t inSize)665 struct chreWifiRangingParams *chppWifiRangingParamsToChre(
666 const struct ChppWifiRangingParams *in, size_t inSize) {
667 struct chreWifiRangingParams *out = NULL;
668
669 if (inSize >= sizeof(struct ChppWifiRangingParams)) {
670 out = chppMalloc(sizeof(struct chreWifiRangingParams));
671 if (out != NULL) {
672 if (!chppWifiConvertRangingParamsToChre(in, out, inSize)) {
673 CHPP_FREE_AND_NULLIFY(out);
674 }
675 }
676 }
677
678 return out;
679 }
680