mirror of
				https://github.com/oplus-giulia-dev/android_hardware_oplus
				synced 2025-11-04 13:55:35 +08:00 
			
		
		
		
	This configures the NR mode to "SA Preferred" upon each boot (a setting that persists even after a factory reset), which is desirable in most cases. The mode can be customized using ro.vendor.radio.auto_nr_mode: 0: NSA Preferred 1: NSA Only 2: SA Only 3: SA Preferred Change-Id: I86ad739167a51af04c041887a93462bf5bb5e32f
		
			
				
	
	
		
			90 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * SPDX-FileCopyrightText: 2025 The LineageOS Project
 | 
						|
 * SPDX-License-Identifier: Apache-2.0
 | 
						|
 */
 | 
						|
 | 
						|
#include <android-base/logging.h>
 | 
						|
#include <android-base/properties.h>
 | 
						|
#include <android/binder_manager.h>
 | 
						|
#include <android/binder_process.h>
 | 
						|
 | 
						|
#include <thread>
 | 
						|
 | 
						|
#include <aidl/vendor/oplus/hardware/radio/IOplusRadio.h>
 | 
						|
 | 
						|
#include "OplusRadioResponse.h"
 | 
						|
 | 
						|
using aidl::vendor::oplus::hardware::radio::IOplusRadio;
 | 
						|
using aidl::vendor::oplus::hardware::radio::OplusRadioResponse;
 | 
						|
using android::base::GetIntProperty;
 | 
						|
using std::chrono_literals::operator""s;
 | 
						|
 | 
						|
namespace {
 | 
						|
 | 
						|
enum OplusNrMode {
 | 
						|
    NSA_PRE,
 | 
						|
    NSA_ONLY,
 | 
						|
    SA_ONLY,
 | 
						|
    SA_PRE,
 | 
						|
    NR_MODE_MAX,
 | 
						|
};
 | 
						|
constexpr auto kDefaultAutoNrMode = SA_PRE;
 | 
						|
constexpr auto kPropertyDefaultAutoNrMode = "ro.vendor.radio.auto_nr_mode";
 | 
						|
 | 
						|
constexpr auto kIRadioAidlServiceName = "vendor.oplus.hardware.radio.IRadioStable/OplusRadio";
 | 
						|
constexpr auto kMaxSimSlot = 2;
 | 
						|
constexpr auto kOplusRilSerial = 1001;
 | 
						|
 | 
						|
OplusNrMode GetDefaultAutoNrMode() {
 | 
						|
    auto mode = GetIntProperty(kPropertyDefaultAutoNrMode, static_cast<int>(kDefaultAutoNrMode));
 | 
						|
    if (mode < NSA_PRE || mode >= NR_MODE_MAX) {
 | 
						|
        LOG(WARNING) << "Invalid NR mode: " << mode << ", using default: " << kDefaultAutoNrMode;
 | 
						|
        return kDefaultAutoNrMode;
 | 
						|
    }
 | 
						|
    return static_cast<OplusNrMode>(mode);
 | 
						|
}
 | 
						|
 | 
						|
};  // anonymous namespace
 | 
						|
 | 
						|
int main() {
 | 
						|
    int rc = EXIT_SUCCESS;
 | 
						|
 | 
						|
    ABinderProcess_setThreadPoolMaxThreadCount(1);
 | 
						|
    ABinderProcess_startThreadPool();
 | 
						|
 | 
						|
    auto mode = GetDefaultAutoNrMode();
 | 
						|
    for (auto i = 0; i < kMaxSimSlot; ++i) {
 | 
						|
        auto instance = kIRadioAidlServiceName + std::to_string(i);
 | 
						|
        if (!AServiceManager_isDeclared(instance.c_str())) {
 | 
						|
            // Instance not declared in manifest, skip.
 | 
						|
            continue;
 | 
						|
        }
 | 
						|
        auto radio = IOplusRadio::fromBinder(
 | 
						|
                ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())));
 | 
						|
        if (!radio) {
 | 
						|
            LOG(DEBUG) << "Failed to get service " << instance;
 | 
						|
            continue;
 | 
						|
        }
 | 
						|
 | 
						|
        int32_t in_result = 0;
 | 
						|
        auto resp_cb = ndk::SharedRefBase::make<OplusRadioResponse>(in_result);
 | 
						|
        radio->setCallback(resp_cb, nullptr);
 | 
						|
 | 
						|
        bool succeed = false;
 | 
						|
        for (auto retry = 100; retry > 0; --retry) {
 | 
						|
            auto status = radio->setNrMode(kOplusRilSerial, mode);
 | 
						|
            if (!status.isOk() || in_result != 0) {
 | 
						|
                LOG(ERROR) << "setNrMode failed for SIM" << i
 | 
						|
                           << ", (remaining retries: " << retry - 1 << ")";
 | 
						|
                std::this_thread::sleep_for(1s);
 | 
						|
                continue;
 | 
						|
            }
 | 
						|
            succeed = true;
 | 
						|
            break;
 | 
						|
        }
 | 
						|
        if (!succeed) rc = EXIT_FAILURE;
 | 
						|
    }
 | 
						|
 | 
						|
    return rc;
 | 
						|
}
 |