/*
 * 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_GnssConfigurationInterface"

#include <log/log.h>

#include "GnssConfiguration.h"

namespace android {
namespace hardware {
namespace gnss {
namespace V1_1 {
namespace implementation {

GnssConfiguration::GnssConfiguration(const GnssConfigurationInterface_ext* gnssConfigInfc)
    : mGnssConfigIface(gnssConfigInfc) {}

// Methods from ::android::hardware::gps::V1_1::IGnssConfiguration follow.
Return<bool> GnssConfiguration::setSuplEs(bool enabled)  {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "SUPL_ES=" + std::to_string(enabled ? 1 : 0) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}

Return<bool> GnssConfiguration::setSuplVersion(uint32_t version)  {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "SUPL_VER=" + std::to_string(version) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());

    return true;
}

Return<bool> GnssConfiguration::setSuplMode(uint8_t mode)  {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "SUPL_MODE=" + std::to_string(mode) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}

Return<bool> GnssConfiguration::setLppProfile(uint8_t lppProfile) {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "LPP_PROFILE=" + std::to_string(lppProfile) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}

Return<bool> GnssConfiguration::setGlonassPositioningProtocol(uint8_t protocol) {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "A_GLONASS_POS_PROTOCOL_SELECT=" +
            std::to_string(protocol) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}

Return<bool> GnssConfiguration::setGpsLock(uint8_t lock) {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "GPS_LOCK=" + std::to_string(lock) + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}

Return<bool> GnssConfiguration::setEmergencySuplPdn(bool enabled) {
    if (mGnssConfigIface == nullptr) {
        ALOGE("%s: GNSS Configuration interface is not available.", __func__);
        return false;
    }

    std::string config = "USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL=" + std::to_string(enabled ? 1 : 0)
            + "\n";
    mGnssConfigIface->configuration_update(config.c_str(), config.size());
    return true;
}


// Methods from ::android::hardware::gnss::V1_1::IGnssConfiguration follow.
Return<bool> GnssConfiguration::setBlacklist(const hidl_vec<BlacklistedSource>& sourceList) {
    size_t listSize = sourceList.size();
    long long blackSvid[7];  /// V1_0::GnssConstellationType size is 7
    memset(blackSvid, 0x00, sizeof(long long)*7);

    for (size_t i = 0; i < listSize; i++) {
        int idx = (int) sourceList[i].constellation;
        if (idx > 6) {
            ALOGE("%s: GnssConstellation type is out of boundary.", __func__);
            continue;
        } else if (sourceList[i].svid == 0) { /// all sv are blocked
            blackSvid[idx] = (long long)(((long long)0xFFFFFFFFL << 32) | 0xFFFFFFFFL);
        } else {
            blackSvid[idx] |= (long long)((long long)0x01L << (sourceList[i].svid -1));
        }
    }

    mGnssConfigIface->setBlacklist(blackSvid, 7);
    return true;
}


}  // namespace implementation
}  // namespace V1_1
}  // namespace gnss
}  // namespace hardware
}  // namespace android
