1/*
2 * Copyright (C) 2016 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#include "GnssUtils.h"
18
19namespace android {
20namespace hardware {
21namespace gnss {
22namespace V1_0 {
23namespace implementation {
24
25using android::hardware::gnss::V1_0::GnssLocation;
26
27GnssLocation convertToGnssLocation(GpsLocation* location) {
28    GnssLocation gnssLocation = {};
29    if (location != nullptr) {
30        gnssLocation = {
31            // Bit operation AND with 1f below is needed to clear vertical accuracy,
32            // speed accuracy and bearing accuracy flags as some vendors are found
33            // to be setting these bits in pre-Android-O devices
34            .gnssLocationFlags = static_cast<uint16_t>(location->flags & 0x1f),
35            .latitudeDegrees = location->latitude,
36            .longitudeDegrees = location->longitude,
37            .altitudeMeters = location->altitude,
38            .speedMetersPerSec = location->speed,
39            .bearingDegrees = location->bearing,
40            .horizontalAccuracyMeters = location->accuracy,
41            // Older chipsets do not provide the following 3 fields, hence the flags
42            // HAS_VERTICAL_ACCURACY, HAS_SPEED_ACCURACY and HAS_BEARING_ACCURACY are
43            // not set and the field are set to zeros.
44            .verticalAccuracyMeters = 0,
45            .speedAccuracyMetersPerSecond = 0,
46            .bearingAccuracyDegrees = 0,
47            .timestamp = location->timestamp
48        };
49    }
50
51    return gnssLocation;
52}
53
54GnssLocation convertToGnssLocation(FlpLocation* flpLocation) {
55    GnssLocation gnssLocation = {};
56    if (flpLocation != nullptr) {
57        gnssLocation = {.gnssLocationFlags = 0,  // clear here and set below
58                        .latitudeDegrees = flpLocation->latitude,
59                        .longitudeDegrees = flpLocation->longitude,
60                        .altitudeMeters = flpLocation->altitude,
61                        .speedMetersPerSec = flpLocation->speed,
62                        .bearingDegrees = flpLocation->bearing,
63                        .horizontalAccuracyMeters = flpLocation->accuracy,
64                        .verticalAccuracyMeters = 0,
65                        .speedAccuracyMetersPerSecond = 0,
66                        .bearingAccuracyDegrees = 0,
67                        .timestamp = flpLocation->timestamp};
68        // FlpLocation flags different from GnssLocation flags
69        if (flpLocation->flags & FLP_LOCATION_HAS_LAT_LONG) {
70            gnssLocation.gnssLocationFlags |= GPS_LOCATION_HAS_LAT_LONG;
71        }
72        if (flpLocation->flags & FLP_LOCATION_HAS_ALTITUDE) {
73            gnssLocation.gnssLocationFlags |= GPS_LOCATION_HAS_ALTITUDE;
74        }
75        if (flpLocation->flags & FLP_LOCATION_HAS_SPEED) {
76            gnssLocation.gnssLocationFlags |= GPS_LOCATION_HAS_SPEED;
77        }
78        if (flpLocation->flags & FLP_LOCATION_HAS_BEARING) {
79            gnssLocation.gnssLocationFlags |= GPS_LOCATION_HAS_BEARING;
80        }
81        if (flpLocation->flags & FLP_LOCATION_HAS_ACCURACY) {
82            gnssLocation.gnssLocationFlags |= GPS_LOCATION_HAS_HORIZONTAL_ACCURACY;
83        }
84    }
85
86    return gnssLocation;
87}
88
89}  // namespace implementation
90}  // namespace V1_0
91}  // namespace gnss
92}  // namespace hardware
93}  // namespace android
94