Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 29dc1e0c authored by Hridya Valsaraju's avatar Hridya Valsaraju
Browse files

Default implementation Gnss HAL

Test: On Angler, GPS location available on Maps. All interfaces other
than AGnssRil, AGnssNavigationMessage and GnssNi are functionally tested. AGnssRil and
AGnssNavigationMessage are not implemented by conventional GPS HALs in Google devices that would be
upgrading to O.

Bug:31974439

Change-Id: I66225225ed79bc1735d6dd27954b1f9f69ffc557
parent 273c6d20
Loading
Loading
Loading
Loading
+188 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "GnssHAL_AGnssInterface"

#include "AGnss.h"

namespace android {
namespace hardware {
namespace gnss {
namespace V1_0 {
namespace implementation {

std::vector<std::unique_ptr<ThreadFuncArgs>> AGnss::sThreadFuncArgsList;
sp<IAGnssCallback> AGnss::sAGnssCbIface = nullptr;
bool AGnss::sInterfaceExists = false;

AGpsCallbacks AGnss::sAGnssCb = {
    .status_cb = statusCb,
    .create_thread_cb = createThreadCb
};

AGnss::AGnss(const AGpsInterface* aGpsIface) : mAGnssIface(aGpsIface) {
    /* Error out if an instance of the interface already exists. */
    LOG_ALWAYS_FATAL_IF(sInterfaceExists);
    sInterfaceExists = true;
}

AGnss::~AGnss() {
    sThreadFuncArgsList.clear();
}

void AGnss::statusCb(AGpsStatus* status) {
    if (sAGnssCbIface == nullptr) {
        ALOGE("%s: AGNSS Callback Interface configured incorrectly", __func__);
        return;
    }

    if (status == nullptr) {
        ALOGE("AGNSS status is invalid");
        return;
    }

    /*
     * Logic based on AGnssStatus processing by GnssLocationProvider. Size of
     * AGpsStatus is checked for backward compatibility since some devices may
     * be sending out an older version of AGpsStatus that only supports IPv4.
     */
    size_t statusSize = status->size;
    if (status->size == sizeof(AGpsStatus)) {
        switch (status->addr.ss_family)
        {
            case AF_INET:
                {
                    /*
                     * ss_family indicates IPv4.
                     */
                    struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(&(status->addr));
                    IAGnssCallback::AGnssStatusIpV4 aGnssStatusIpV4 = {
                        .type = static_cast<IAGnssCallback::AGnssType>(status->type),
                        .status = static_cast<IAGnssCallback::AGnssStatusValue>(status->status),
                        .ipV4Addr = in->sin_addr.s_addr,
                    };

                    /*
                     * Callback to client with agnssStatusIpV4Cb.
                     */
                    sAGnssCbIface->agnssStatusIpV4Cb(aGnssStatusIpV4);
                    break;
                }
            case AF_INET6:
                {
                    /*
                     * ss_family indicates IPv6. Callback to client with agnssStatusIpV6Cb.
                     */
                    IAGnssCallback::AGnssStatusIpV6 aGnssStatusIpV6;

                    aGnssStatusIpV6.type = static_cast<IAGnssCallback::AGnssType>(status->type);
                    aGnssStatusIpV6.status = static_cast<IAGnssCallback::AGnssStatusValue>(
                            status->status);

                    struct sockaddr_in6* in6 = reinterpret_cast<struct sockaddr_in6 *>(
                            &(status->addr));
                    memcpy(&(aGnssStatusIpV6.ipV6Addr[0]), in6->sin6_addr.s6_addr,
                           aGnssStatusIpV6.ipV6Addr.size());
                    sAGnssCbIface->agnssStatusIpV6Cb(aGnssStatusIpV6);
                    break;
                }
             default:
                    ALOGE("Invalid ss_family found: %d", status->addr.ss_family);
        }
    } else if (statusSize >= sizeof(AGpsStatus_v2)) {
        AGpsStatus_v2* statusV2 = reinterpret_cast<AGpsStatus_v2*>(status);
        uint32_t ipV4Addr = statusV2->ipaddr;
        IAGnssCallback::AGnssStatusIpV4 aGnssStatusIpV4 = {
            .type = static_cast<IAGnssCallback::AGnssType>(AF_INET),
            .status = static_cast<IAGnssCallback::AGnssStatusValue>(status->status),
            /*
             * For older versions of AGpsStatus, change IP addr to net order. This
             * was earlier being done in GnssLocationProvider.
             */
            .ipV4Addr = htonl(ipV4Addr)
        };
        /*
         * Callback to client with agnssStatusIpV4Cb.
         */
        sAGnssCbIface->agnssStatusIpV4Cb(aGnssStatusIpV4);
    } else {
        ALOGE("%s: Invalid size for AGPS Status", __func__);
    }
}

pthread_t AGnss::createThreadCb(const char* name, void (*start)(void*), void* arg) {
    return createPthread(name, start, arg, &sThreadFuncArgsList);
}

/*
 * Implementation of methods from ::android::hardware::gnss::V1_0::IAGnss follow.
 */
Return<void> AGnss::setCallback(const sp<IAGnssCallback>& callback) {
    if (mAGnssIface == nullptr) {
        ALOGE("%s: AGnss interface is unavailable", __func__);
        return Void();
    }

    sAGnssCbIface = callback;

    mAGnssIface->init(&sAGnssCb);
    return Void();
}

Return<bool> AGnss::dataConnClosed()  {
    if (mAGnssIface == nullptr) {
        ALOGE("%s: AGnss interface is unavailable", __func__);
        return false;
    }

    return (mAGnssIface->data_conn_closed() == 0);
}

Return<bool> AGnss::dataConnFailed()  {
    if (mAGnssIface == nullptr) {
        ALOGE("%s: AGnss interface is unavailable", __func__);
        return false;
    }

    return (mAGnssIface->data_conn_failed() == 0);
}

Return<bool> AGnss::setServer(IAGnssCallback::AGnssType type,
                              const hidl_string& hostname,
                              int32_t port) {
    if (mAGnssIface == nullptr) {
        ALOGE("%s: AGnss interface is unavailable", __func__);
        return false;
    }

    return (mAGnssIface->set_server(static_cast<AGpsType>(type), hostname.c_str(), port) == 0);
}

Return<bool> AGnss::dataConnOpen(const hidl_string& apn, IAGnss::ApnIpType apnIpType) {
    if (mAGnssIface == nullptr) {
        ALOGE("%s: AGnss interface is unavailable", __func__);
        return false;
    }

    return (mAGnssIface->data_conn_open_with_apn_ip_type(apn.c_str(),
                                                     static_cast<uint16_t>(apnIpType)) == 0);
}

}  // namespace implementation
}  // namespace V1_0
}  // namespace gnss
}  // namespace hardware
}  // namespace android
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef android_hardware_gnss_V1_0_AGnss_H_
#define android_hardware_gnss_V1_0_AGnss_H_

#include <ThreadCreationWrapper.h>
#include <android/hardware/gnss/1.0/IAGnss.h>
#include <hardware/gps_internal.h>
#include <hidl/Status.h>
#include <netinet/in.h>

namespace android {
namespace hardware {
namespace gnss {
namespace V1_0 {
namespace implementation {

using ::android::hardware::gnss::V1_0::IAGnss;
using ::android::hardware::gnss::V1_0::IAGnssCallback;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::hidl_string;
using ::android::sp;

/*
 * Extended interface for AGNSS support. Also contains wrapper methods to allow
 * methods from IAGnssCallback interface to be passed into the conventional
 * implementation of the GNSS HAL.
 */
struct AGnss : public IAGnss {
    AGnss(const AGpsInterface* agpsIface);
    ~AGnss();
    /*
     * Methods from ::android::hardware::gnss::V1_0::IAGnss interface follow.
     * These declarations were generated from IAGnss.hal.
     */
    Return<void> setCallback(const sp<IAGnssCallback>& callback) override;
    Return<bool> dataConnClosed() override;
    Return<bool> dataConnFailed() override;
    Return<bool> setServer(IAGnssCallback::AGnssType type,
                         const hidl_string& hostname, int32_t port) override;
    Return<bool> dataConnOpen(const hidl_string& apn,
                                           IAGnss::ApnIpType apnIpType) override;

    /*
     * Callback methods to be passed into the conventional GNSS HAL by the default
     * implementation. These methods are not part of the IAGnss base class.
     */
    static pthread_t createThreadCb(const char* name, void (*start)(void*), void* arg);
    static void statusCb(AGpsStatus* status);

    /*
     * Holds function pointers to the callback methods.
     */
    static AGpsCallbacks sAGnssCb;

 private:
    const AGpsInterface* mAGnssIface = nullptr;
    static sp<IAGnssCallback> sAGnssCbIface;
    static std::vector<std::unique_ptr<ThreadFuncArgs>> sThreadFuncArgsList;
    static bool sInterfaceExists;
};

}  // namespace implementation
}  // namespace V1_0
}  // namespace gnss
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_gnss_V1_0_AGnss_H_
+145 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "GnssHAL_AGnssRilInterface"

#include "AGnssRil.h"

namespace android {
namespace hardware {
namespace gnss {
namespace V1_0 {
namespace implementation {

std::vector<std::unique_ptr<ThreadFuncArgs>> AGnssRil::sThreadFuncArgsList;
sp<IAGnssRilCallback> AGnssRil::sAGnssRilCbIface = nullptr;
bool AGnssRil::sInterfaceExists = false;

AGpsRilCallbacks AGnssRil::sAGnssRilCb = {
    .request_setid = AGnssRil::requestSetId,
    .request_refloc = AGnssRil::requestRefLoc,
    .create_thread_cb = AGnssRil::createThreadCb
};

AGnssRil::AGnssRil(const AGpsRilInterface* aGpsRilIface) : mAGnssRilIface(aGpsRilIface) {
    /* Error out if an instance of the interface already exists. */
    LOG_ALWAYS_FATAL_IF(sInterfaceExists);
    sInterfaceExists = true;
}

AGnssRil::~AGnssRil() {
    sThreadFuncArgsList.clear();
}

void AGnssRil::requestSetId(uint32_t flags) {
    if (sAGnssRilCbIface == nullptr) {
        ALOGE("%s: AGNSSRil Callback Interface configured incorrectly", __func__);
        return;
    }

    sAGnssRilCbIface->requestSetIdCb(static_cast<IAGnssRilCallback::ID>(flags));
}

void AGnssRil::requestRefLoc(uint32_t /*flags*/) {
    if (sAGnssRilCbIface == nullptr) {
        ALOGE("%s: AGNSSRil Callback Interface configured incorrectly", __func__);
        return;
    }

    sAGnssRilCbIface->requestRefLocCb();
}

pthread_t AGnssRil::createThreadCb(const char* name, void (*start)(void*), void* arg) {
    return createPthread(name, start, arg, &sThreadFuncArgsList);
}

// Methods from ::android::hardware::gnss::V1_0::IAGnssRil follow.
Return<void> AGnssRil::setCallback(const sp<IAGnssRilCallback>& callback)  {
    if (mAGnssRilIface == nullptr) {
        ALOGE("%s: AGnssRil interface is unavailable", __func__);
        return Void();
    }

    sAGnssRilCbIface = callback;

    mAGnssRilIface->init(&sAGnssRilCb);
    return Void();
}

Return<void> AGnssRil::setRefLocation(const IAGnssRil::AGnssRefLocation& aGnssRefLocation)  {
    if (mAGnssRilIface == nullptr) {
        ALOGE("%s: AGnssRil interface is unavailable", __func__);
        return Void();
    }

    AGpsRefLocation aGnssRefloc;
    aGnssRefloc.type = static_cast<uint16_t>(aGnssRefLocation.type);

    auto& cellID = aGnssRefLocation.cellID;
    aGnssRefloc.u.cellID = {
        .type = static_cast<uint16_t>(cellID.type),
        .mcc = cellID.mcc,
        .mnc = cellID.mnc,
        .lac = cellID.lac,
        .cid = cellID.cid,
        .tac = cellID.tac,
        .pcid = cellID.pcid
    };

    mAGnssRilIface->set_ref_location(&aGnssRefloc, sizeof(aGnssRefloc));
    return Void();
}

Return<bool> AGnssRil::setSetId(IAGnssRil::SetIDType type, const hidl_string& setid)  {
    if (mAGnssRilIface == nullptr) {
        ALOGE("%s: AGnssRil interface is unavailable", __func__);
        return false;
    }

    mAGnssRilIface->set_set_id(static_cast<uint16_t>(type), setid.c_str());
    return true;
}

Return<bool> AGnssRil::updateNetworkState(bool connected,
                                          IAGnssRil::NetworkType type,
                                          bool roaming) {
    if (mAGnssRilIface == nullptr) {
        ALOGE("%s: AGnssRil interface is unavailable", __func__);
        return false;
    }

    mAGnssRilIface->update_network_state(connected,
                                         static_cast<int>(type),
                                         roaming,
                                         nullptr /* extra_info */);
    return true;
}

Return<bool> AGnssRil::updateNetworkAvailability(bool available, const hidl_string& apn)  {
    if (mAGnssRilIface == nullptr) {
        ALOGE("%s: AGnssRil interface is unavailable", __func__);
        return false;
    }

    mAGnssRilIface->update_network_availability(available, apn.c_str());
    return true;
}

}  // namespace implementation
}  // namespace V1_0
}  // namespace gnss
}  // namespace hardware
}  // namespace android
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef android_hardware_gnss_V1_0_AGnssRil_H_
#define android_hardware_gnss_V1_0_AGnssRil_H_

#include <ThreadCreationWrapper.h>
#include <android/hardware/gnss/1.0/IAGnssRil.h>
#include <hardware/gps.h>
#include <hidl/Status.h>

namespace android {
namespace hardware {
namespace gnss {
namespace V1_0 {
namespace implementation {

using ::android::hardware::gnss::V1_0::IAGnssRil;
using ::android::hardware::gnss::V1_0::IAGnssRilCallback;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::hidl_string;
using ::android::sp;

/*
 * Extended interface for AGNSS RIL support. An Assisted GNSS Radio Interface Layer interface
 * allows the GNSS chipset to request radio interface layer information from Android platform.
 * Examples of such information are reference location, unique subscriber ID, phone number string
 * and network availability changes. Also contains wrapper methods to allow methods from
 * IAGnssiRilCallback interface to be passed into the conventional implementation of the GNSS HAL.
 */
struct AGnssRil : public IAGnssRil {
    AGnssRil(const AGpsRilInterface* aGpsRilIface);
    ~AGnssRil();

    /*
     * Methods from ::android::hardware::gnss::V1_0::IAGnssRil follow.
     * These declarations were generated from IAGnssRil.hal.
     */
    Return<void> setCallback(const sp<IAGnssRilCallback>& callback) override;
    Return<void> setRefLocation(const IAGnssRil::AGnssRefLocation& agnssReflocation) override;
    Return<bool> setSetId(IAGnssRil::SetIDType type, const hidl_string& setid) override;
    Return<bool> updateNetworkState(bool connected,
                                    IAGnssRil::NetworkType type,
                                    bool roaming) override;
    Return<bool> updateNetworkAvailability(bool available, const hidl_string& apn) override;
    static void requestSetId(uint32_t flags);
    static void requestRefLoc(uint32_t flags);

    /*
     * Callback method to be passed into the conventional GNSS HAL by the default
     * implementation. This method is not part of the IAGnssRil base class.
     */
    static pthread_t createThreadCb(const char* name, void (*start)(void*), void* arg);

    /*
     * Holds function pointers to the callback methods.
     */
    static AGpsRilCallbacks sAGnssRilCb;

 private:
    const AGpsRilInterface* mAGnssRilIface = nullptr;
    static sp<IAGnssRilCallback> sAGnssRilCbIface;
    static std::vector<std::unique_ptr<ThreadFuncArgs>> sThreadFuncArgsList;
    static bool sInterfaceExists;
};

}  // namespace implementation
}  // namespace V1_0
}  // namespace gnss
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_gnss_V1_0_AGnssRil_H_
+29 −0
Original line number Diff line number Diff line
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := android.hardware.gnss@1.0-impl
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SRC_FILES := \
    ThreadCreationWrapper.cpp \
    AGnss.cpp \
    AGnssRil.cpp \
    Gnss.cpp \
    GnssDebug.cpp \
    GnssGeofencing.cpp \
    GnssMeasurement.cpp \
    GnssNavigationMessage.cpp \
    GnssNi.cpp \
    GnssXtra.cpp \
    GnssConfiguration.cpp \
    GnssUtils.cpp

LOCAL_SHARED_LIBRARIES := \
    liblog \
    libhidlbase \
    libhidltransport \
    libhwbinder \
    libutils \
    android.hardware.gnss@1.0 \
    libhardware

include $(BUILD_SHARED_LIBRARY)
Loading