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:26 UTC
19 // Source: chre_api/include/chre_api/chre/gnss.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/gnss_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 //! chreGnssDataEvent along with the CHPP header as
36 //! struct ChppGnssDataEventWithHeader
chppGnssSizeOfDataEventFromChre(const struct chreGnssDataEvent * dataEvent)37 static size_t chppGnssSizeOfDataEventFromChre(
38     const struct chreGnssDataEvent *dataEvent) {
39   size_t encodedSize = sizeof(struct ChppGnssDataEventWithHeader);
40   encodedSize +=
41       dataEvent->measurement_count * sizeof(struct ChppGnssMeasurement);
42   return encodedSize;
43 }
44 
45 // Encoding (CHRE --> CHPP) conversion functions
46 
chppGnssConvertClockFromChre(const struct chreGnssClock * in,struct ChppGnssClock * out)47 static void chppGnssConvertClockFromChre(const struct chreGnssClock *in,
48                                          struct ChppGnssClock *out) {
49   out->time_ns = in->time_ns;
50   out->full_bias_ns = in->full_bias_ns;
51   out->bias_ns = in->bias_ns;
52   out->drift_nsps = in->drift_nsps;
53   out->bias_uncertainty_ns = in->bias_uncertainty_ns;
54   out->drift_uncertainty_nsps = in->drift_uncertainty_nsps;
55   out->hw_clock_discontinuity_count = in->hw_clock_discontinuity_count;
56   out->flags = in->flags;
57   memset(&out->reserved, 0, sizeof(out->reserved));
58 }
59 
chppGnssConvertMeasurementFromChre(const struct chreGnssMeasurement * in,struct ChppGnssMeasurement * out)60 static void chppGnssConvertMeasurementFromChre(
61     const struct chreGnssMeasurement *in, struct ChppGnssMeasurement *out) {
62   out->time_offset_ns = in->time_offset_ns;
63   out->accumulated_delta_range_um = in->accumulated_delta_range_um;
64   out->received_sv_time_in_ns = in->received_sv_time_in_ns;
65   out->received_sv_time_uncertainty_in_ns =
66       in->received_sv_time_uncertainty_in_ns;
67   out->pseudorange_rate_mps = in->pseudorange_rate_mps;
68   out->pseudorange_rate_uncertainty_mps = in->pseudorange_rate_uncertainty_mps;
69   out->accumulated_delta_range_uncertainty_m =
70       in->accumulated_delta_range_uncertainty_m;
71   out->c_n0_dbhz = in->c_n0_dbhz;
72   out->snr_db = in->snr_db;
73   out->state = in->state;
74   out->accumulated_delta_range_state = in->accumulated_delta_range_state;
75   out->svid = in->svid;
76   out->constellation = in->constellation;
77   out->multipath_indicator = in->multipath_indicator;
78   out->carrier_frequency_hz = in->carrier_frequency_hz;
79 }
80 
chppGnssConvertDataEventFromChre(const struct chreGnssDataEvent * in,struct ChppGnssDataEvent * out,uint8_t * payload,size_t payloadSize,uint16_t * vlaOffset)81 static void chppGnssConvertDataEventFromChre(const struct chreGnssDataEvent *in,
82                                              struct ChppGnssDataEvent *out,
83                                              uint8_t *payload,
84                                              size_t payloadSize,
85                                              uint16_t *vlaOffset) {
86   out->version = CHRE_GNSS_DATA_EVENT_VERSION;
87   out->measurement_count = in->measurement_count;
88   memset(&out->reserved, 0, sizeof(out->reserved));
89   chppGnssConvertClockFromChre(&in->clock, &out->clock);
90 
91   struct ChppGnssMeasurement *measurements =
92       (struct ChppGnssMeasurement *)&payload[*vlaOffset];
93   out->measurements.length =
94       in->measurement_count * sizeof(struct ChppGnssMeasurement);
95   CHPP_ASSERT((size_t)(*vlaOffset + out->measurements.length) <= payloadSize);
96   if (out->measurements.length > 0 &&
97       *vlaOffset + out->measurements.length <= payloadSize) {
98     for (size_t i = 0; i < in->measurement_count; i++) {
99       chppGnssConvertMeasurementFromChre(&in->measurements[i],
100                                          &measurements[i]);
101     }
102     out->measurements.offset = *vlaOffset;
103     *vlaOffset += out->measurements.length;
104   } else {
105     out->measurements.offset = 0;
106   }
107 }
108 
chppGnssConvertLocationEventFromChre(const struct chreGnssLocationEvent * in,struct ChppGnssLocationEvent * out)109 static void chppGnssConvertLocationEventFromChre(
110     const struct chreGnssLocationEvent *in, struct ChppGnssLocationEvent *out) {
111   out->timestamp = in->timestamp;
112   out->latitude_deg_e7 = in->latitude_deg_e7;
113   out->longitude_deg_e7 = in->longitude_deg_e7;
114   out->altitude = in->altitude;
115   out->speed = in->speed;
116   out->bearing = in->bearing;
117   out->accuracy = in->accuracy;
118   out->flags = in->flags;
119   memset(&out->reserved, 0, sizeof(out->reserved));
120   out->altitude_accuracy = in->altitude_accuracy;
121   out->speed_accuracy = in->speed_accuracy;
122   out->bearing_accuracy = in->bearing_accuracy;
123 }
124 
125 // Encoding (CHRE --> CHPP) top-level functions
126 
chppGnssDataEventFromChre(const struct chreGnssDataEvent * in,struct ChppGnssDataEventWithHeader ** out,size_t * outSize)127 bool chppGnssDataEventFromChre(const struct chreGnssDataEvent *in,
128                                struct ChppGnssDataEventWithHeader **out,
129                                size_t *outSize) {
130   CHPP_NOT_NULL(out);
131   CHPP_NOT_NULL(outSize);
132 
133   size_t payloadSize = chppGnssSizeOfDataEventFromChre(in);
134   *out = chppMalloc(payloadSize);
135   if (*out != NULL) {
136     uint8_t *payload = (uint8_t *)&(*out)->payload;
137     uint16_t vlaOffset = sizeof(struct ChppGnssDataEvent);
138     chppGnssConvertDataEventFromChre(in, &(*out)->payload, payload, payloadSize,
139                                      &vlaOffset);
140     *outSize = payloadSize;
141     return true;
142   }
143   return false;
144 }
145 
chppGnssLocationEventFromChre(const struct chreGnssLocationEvent * in,struct ChppGnssLocationEventWithHeader ** out,size_t * outSize)146 bool chppGnssLocationEventFromChre(const struct chreGnssLocationEvent *in,
147                                    struct ChppGnssLocationEventWithHeader **out,
148                                    size_t *outSize) {
149   CHPP_NOT_NULL(out);
150   CHPP_NOT_NULL(outSize);
151 
152   size_t payloadSize = sizeof(struct ChppGnssLocationEventWithHeader);
153   *out = chppMalloc(payloadSize);
154   if (*out != NULL) {
155     chppGnssConvertLocationEventFromChre(in, &(*out)->payload);
156     *outSize = payloadSize;
157     return true;
158   }
159   return false;
160 }
161 
162 // Decoding (CHPP --> CHRE) conversion functions
163 
chppGnssConvertClockToChre(const struct ChppGnssClock * in,struct chreGnssClock * out)164 static bool chppGnssConvertClockToChre(const struct ChppGnssClock *in,
165                                        struct chreGnssClock *out) {
166   out->time_ns = in->time_ns;
167   out->full_bias_ns = in->full_bias_ns;
168   out->bias_ns = in->bias_ns;
169   out->drift_nsps = in->drift_nsps;
170   out->bias_uncertainty_ns = in->bias_uncertainty_ns;
171   out->drift_uncertainty_nsps = in->drift_uncertainty_nsps;
172   out->hw_clock_discontinuity_count = in->hw_clock_discontinuity_count;
173   out->flags = in->flags;
174   memset(&out->reserved, 0, sizeof(out->reserved));
175 
176   return true;
177 }
178 
chppGnssConvertMeasurementToChre(const struct ChppGnssMeasurement * in,struct chreGnssMeasurement * out)179 static bool chppGnssConvertMeasurementToChre(
180     const struct ChppGnssMeasurement *in, struct chreGnssMeasurement *out) {
181   out->time_offset_ns = in->time_offset_ns;
182   out->accumulated_delta_range_um = in->accumulated_delta_range_um;
183   out->received_sv_time_in_ns = in->received_sv_time_in_ns;
184   out->received_sv_time_uncertainty_in_ns =
185       in->received_sv_time_uncertainty_in_ns;
186   out->pseudorange_rate_mps = in->pseudorange_rate_mps;
187   out->pseudorange_rate_uncertainty_mps = in->pseudorange_rate_uncertainty_mps;
188   out->accumulated_delta_range_uncertainty_m =
189       in->accumulated_delta_range_uncertainty_m;
190   out->c_n0_dbhz = in->c_n0_dbhz;
191   out->snr_db = in->snr_db;
192   out->state = in->state;
193   out->accumulated_delta_range_state = in->accumulated_delta_range_state;
194   out->svid = in->svid;
195   out->constellation = in->constellation;
196   out->multipath_indicator = in->multipath_indicator;
197   out->carrier_frequency_hz = in->carrier_frequency_hz;
198 
199   return true;
200 }
201 
chppGnssConvertDataEventToChre(const struct ChppGnssDataEvent * in,struct chreGnssDataEvent * out,size_t inSize)202 static bool chppGnssConvertDataEventToChre(const struct ChppGnssDataEvent *in,
203                                            struct chreGnssDataEvent *out,
204                                            size_t inSize) {
205   out->version = CHRE_GNSS_DATA_EVENT_VERSION;
206   out->measurement_count = in->measurement_count;
207   memset(&out->reserved, 0, sizeof(out->reserved));
208   if (!chppGnssConvertClockToChre(&in->clock, &out->clock)) {
209     return false;
210   }
211 
212   if (in->measurements.length == 0) {
213     out->measurements = NULL;
214   } else {
215     if (in->measurements.offset + in->measurements.length > inSize ||
216         in->measurements.length !=
217             in->measurement_count * sizeof(struct ChppGnssMeasurement)) {
218       return false;
219     }
220 
221     const struct ChppGnssMeasurement *measurementsIn =
222         (const struct ChppGnssMeasurement *)&(
223             (const uint8_t *)in)[in->measurements.offset];
224 
225     struct chreGnssMeasurement *measurementsOut =
226         chppMalloc(in->measurement_count * sizeof(struct chreGnssMeasurement));
227     if (measurementsOut == NULL) {
228       return false;
229     }
230 
231     for (size_t i = 0; i < in->measurement_count; i++) {
232       if (!chppGnssConvertMeasurementToChre(&measurementsIn[i],
233                                             &measurementsOut[i])) {
234         return false;
235       }
236     }
237     out->measurements = measurementsOut;
238   }
239 
240   return true;
241 }
242 
chppGnssConvertLocationEventToChre(const struct ChppGnssLocationEvent * in,struct chreGnssLocationEvent * out)243 static bool chppGnssConvertLocationEventToChre(
244     const struct ChppGnssLocationEvent *in, struct chreGnssLocationEvent *out) {
245   out->timestamp = in->timestamp;
246   out->latitude_deg_e7 = in->latitude_deg_e7;
247   out->longitude_deg_e7 = in->longitude_deg_e7;
248   out->altitude = in->altitude;
249   out->speed = in->speed;
250   out->bearing = in->bearing;
251   out->accuracy = in->accuracy;
252   out->flags = in->flags;
253   memset(&out->reserved, 0, sizeof(out->reserved));
254   out->altitude_accuracy = in->altitude_accuracy;
255   out->speed_accuracy = in->speed_accuracy;
256   out->bearing_accuracy = in->bearing_accuracy;
257 
258   return true;
259 }
260 
261 // Decoding (CHPP --> CHRE) top-level functions
262 
chppGnssDataEventToChre(const struct ChppGnssDataEvent * in,size_t inSize)263 struct chreGnssDataEvent *chppGnssDataEventToChre(
264     const struct ChppGnssDataEvent *in, size_t inSize) {
265   struct chreGnssDataEvent *out = NULL;
266 
267   if (inSize >= sizeof(struct ChppGnssDataEvent)) {
268     out = chppMalloc(sizeof(struct chreGnssDataEvent));
269     if (out != NULL) {
270       if (!chppGnssConvertDataEventToChre(in, out, inSize)) {
271         CHPP_FREE_AND_NULLIFY(out);
272       }
273     }
274   }
275 
276   return out;
277 }
chppGnssLocationEventToChre(const struct ChppGnssLocationEvent * in,size_t inSize)278 struct chreGnssLocationEvent *chppGnssLocationEventToChre(
279     const struct ChppGnssLocationEvent *in, size_t inSize) {
280   struct chreGnssLocationEvent *out = NULL;
281 
282   if (inSize >= sizeof(struct ChppGnssLocationEvent)) {
283     out = chppMalloc(sizeof(struct chreGnssLocationEvent));
284     if (out != NULL) {
285       if (!chppGnssConvertLocationEventToChre(in, out)) {
286         CHPP_FREE_AND_NULLIFY(out);
287       }
288     }
289   }
290 
291   return out;
292 }
293