diff --git a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/NativeFMInterface.kt b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/NativeFMInterface.kt index d2cacc6..f4d96ce 100644 --- a/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/NativeFMInterface.kt +++ b/universal7885-common/apps/FMRadio/src/com/eurekateam/fmradio/NativeFMInterface.kt @@ -1,22 +1,20 @@ package com.eurekateam.fmradio import vendor.eureka.hardware.fmradio.IFMDevControl -import vendor.eureka.hardware.fmradio.IFMSysfsSupport import vendor.eureka.hardware.fmradio.GetType import vendor.eureka.hardware.fmradio.SetType -import vendor.eureka.hardware.fmradio.Direction import android.os.ServiceManager class NativeFMInterface { private val mDevCtl : IFMDevControl - private val mSysfsCtl : IFMSysfsSupport - private var mSysfs = false + private val mSysfsCtl : IFMDevControl + private val mDefaultCtl : IFMDevControl init { mDevCtl = IFMDevControl.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.fmradio.IFMDevControl/default")) - mSysfsCtl = IFMSysfsSupport.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.fmradio.IFMSysfsSupport/default")) - mSysfs = mSysfsCtl.isAvailable() + mSysfsCtl = IFMDevControl.Stub.asInterface(ServiceManager.waitForDeclaredService("vendor.eureka.hardware.fmradio.IFMDevControl/support")) + mDefaultCtl = if (mSysfsCtl.getValue(GetType.GET_TYPE_FM_SYSFS_IF) == 0) mSysfsCtl else mDevCtl } fun openFMDevice(): Int { @@ -31,27 +29,13 @@ class NativeFMInterface { fun getFmUpper(a: Int) : Int = mDevCtl.getValue(GetType.GET_TYPE_FM_UPPER_LIMIT) fun getFMLower(a: Int): Int = mDevCtl.getValue(GetType.GET_TYPE_FM_LOWER_LIMIT) fun getRMSSI(a: Int): Int = mDevCtl.getValue(GetType.GET_TYPE_FM_RMSSI) - fun getFMTracks(fd: Int): IntArray = if (mSysfs) { - var mRes = intArrayOf() - for (a in 1..30) { - mSysfsCtl.adjustFreqByStep(Direction.UP) - var freq = mSysfsCtl.getFreqFromSysfs() - if (mRes.contains(freq)) continue else mRes += freq - } - mRes - } else mDevCtl.getFreqsList() + fun getFMTracks(fd: Int): IntArray = mDefaultCtl.getFreqsList() fun setFMThread(a: Int, run: Boolean) = mDevCtl.setValue(SetType.SET_TYPE_FM_THREAD, if (run) 1 else 0) - fun getNextChannel(a: Int): Int = if (mSysfs) { - mSysfsCtl.adjustFreqByStep(Direction.UP) - mSysfsCtl.getFreqFromSysfs() - } else mDevCtl.getValue(GetType.GET_TYPE_FM_NEXT_CHANNEL) - fun getBeforeChannel(a: Int): Int = if (mSysfs) { - mSysfsCtl.adjustFreqByStep(Direction.DOWN) - mSysfsCtl.getFreqFromSysfs() - } else mDevCtl.getValue(GetType.GET_TYPE_FM_BEFORE_CHANNEL) + fun getNextChannel(a: Int): Int = mDefaultCtl.getValue(GetType.GET_TYPE_FM_NEXT_CHANNEL) + fun getBeforeChannel(a: Int): Int = mDefaultCtl.getValue(GetType.GET_TYPE_FM_BEFORE_CHANNEL) fun stopSearching(a: Int) = mDevCtl.setValue(SetType.SET_TYPE_FM_SEARCH_CANCEL, 0) fun setFMRSSI(a: Int, rssi: Long) = mDevCtl.setValue(SetType.SET_TYPE_FM_RMSSI, rssi.toInt()) fun closeFMDevice(fd: Int) = mDevCtl.close() - fun getSysfsSupport(): Boolean = mSysfs + fun getSysfsSupport(): Boolean = mSysfsCtl.getValue(GetType.GET_TYPE_FM_SYSFS_IF) == 0 external fun setAudioRoute(speaker: Boolean): Int } diff --git a/universal7885-common/apps/aidl-support/fm/default/FMDevControl.cpp b/universal7885-common/apps/aidl-support/fm/default/FMDevControl.cpp index 6aa42d7..7e4fc73 100644 --- a/universal7885-common/apps/aidl-support/fm/default/FMDevControl.cpp +++ b/universal7885-common/apps/aidl-support/fm/default/FMDevControl.cpp @@ -52,6 +52,8 @@ static int fd = -1; case GetType::GET_TYPE_FM_NEXT_CHANNEL: *_aidl_return = fm_radio_slsi::next_channel(fd); break; + case GetType::GET_TYPE_FM_SYSFS_IF: + [[fallthrough]]; default: break; }; diff --git a/universal7885-common/apps/aidl-support/fm/default/FMSupport.cpp b/universal7885-common/apps/aidl-support/fm/default/FMSupport.cpp index f193acf..7d16e13 100644 --- a/universal7885-common/apps/aidl-support/fm/default/FMSupport.cpp +++ b/universal7885-common/apps/aidl-support/fm/default/FMSupport.cpp @@ -13,15 +13,22 @@ // limitations under the License. #include "FMSupport.h" -#include + #include -#include + #include #include +#include #include -static int mChannelSpacing = 3; +#include + +#define NOT_SUPPORTED \ + ({ \ + LOG(ERROR) << __func__ << ": Attempted to invoke unsupported operation"; \ + return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); \ + }) namespace aidl::vendor::eureka::hardware::fmradio { @@ -30,55 +37,67 @@ constexpr const char *FM_FREQ_CTL = constexpr const char *FM_FREQ_SEEK = "/sys/devices/virtual/s610_radio/s610_radio/radio_freq_seek"; -::ndk::ScopedAStatus FMSupport::setManualFreq(float freq) { - FileIO::writeline(FM_FREQ_CTL, freq * 1000); - return ndk::ScopedAStatus::ok(); -} +::ndk::ScopedAStatus FMSupport::open(void) { NOT_SUPPORTED; } -::ndk::ScopedAStatus FMSupport::adjustFreqByStep(Direction dir) { - std::string value = ""; - if (dir == Direction::UP) { - value = "1 " + std::to_string(mChannelSpacing * 10); - } else if (dir == Direction::DOWN) { - value = "0 " + std::to_string(mChannelSpacing * 10); - } - FileIO::writeline(FM_FREQ_SEEK, value); - return ndk::ScopedAStatus::ok(); -} -::ndk::ScopedAStatus FMSupport::isAvailable(bool *_aidl_return) { - struct stat info; - *_aidl_return = - stat("/sys/devices/virtual/s610_radio/s610_radio/", &info) != 0; - return ndk::ScopedAStatus::ok(); -} -::ndk::ScopedAStatus FMSupport::setChannelSpacing(Space space) { - mChannelSpacing = static_cast(space); - return ndk::ScopedAStatus::ok(); -} -::ndk::ScopedAStatus FMSupport::getFreqFromSysfs(int32_t *_aidl_return) { - *_aidl_return = FileIO::readline(FM_FREQ_CTL); - return ndk::ScopedAStatus::ok(); -} -::ndk::ScopedAStatus FMSupport::getChannelSpacing(Space *_aidl_return) { - switch (mChannelSpacing) { - case 1: - *_aidl_return = Space::CHANNEL_SPACING_10HZ; +::ndk::ScopedAStatus FMSupport::getValue(GetType type, int *_aidl_return) { + switch (type) { + case GetType::GET_TYPE_FM_FREQ: + *_aidl_return = FileIO::readline(FM_FREQ_CTL); break; - case 2: - *_aidl_return = Space::CHANNEL_SPACING_20HZ; + case GetType::GET_TYPE_FM_UPPER_LIMIT: + case GetType::GET_TYPE_FM_LOWER_LIMIT: + case GetType::GET_TYPE_FM_RMSSI: + NOT_SUPPORTED; + case GetType::GET_TYPE_FM_BEFORE_CHANNEL: + FileIO::writeline(FM_FREQ_SEEK, "0 " + std::to_string(3 * 10)); + *_aidl_return = FileIO::readline(FM_FREQ_CTL); break; - case 3: - *_aidl_return = Space::CHANNEL_SPACING_30HZ; - break; - case 4: - *_aidl_return = Space::CHANNEL_SPACING_40HZ; - break; - case 5: - *_aidl_return = Space::CHANNEL_SPACING_50HZ; + case GetType::GET_TYPE_FM_NEXT_CHANNEL: + FileIO::writeline(FM_FREQ_SEEK, "1 " + std::to_string(3 * 10)); + *_aidl_return = FileIO::readline(FM_FREQ_CTL); break; + case GetType::GET_TYPE_FM_SYSFS_IF: + *_aidl_return = access("/sys/devices/virtual/s610_radio/s610_radio/", F_OK); default: break; - } + }; return ndk::ScopedAStatus::ok(); } -} // namespace vendor::eureka::hardware::fmradio::V1_2 +:ndk::ScopedAStatus FMSupport::setValue(SetType type, int value) { + switch (type) { + case SetType::SET_TYPE_FM_FREQ: + FileIO::writeline(FM_FREQ_CTL, freq * 1000); + break; + case SetType::SET_TYPE_FM_MUTE: + case SetType::SET_TYPE_FM_VOLUME: + case SetType::SET_TYPE_FM_THREAD: + case SetType::SET_TYPE_FM_RMSSI: + case SetType::SET_TYPE_FM_SEARCH_CANCEL: + NOT_SUPPORTED; + default: + break; + }; + return ::ndk::ScopedAStatus::ok(); +} + +static inline bool vector_contains(const std::vector vec, + const int search) { + for (auto i : vec) { + if (i == search) + return true; + } + return false; +} + +::ndk::ScopedAStatus FMSupport::getFreqsList(std::vector *_aidl_return) { + for (int i = 0; i < TRACK_SIZE; i++) { + FileIO::writeline(FM_FREQ_SEEK, "1 " + std::to_string(3 * 10)); + int freq = FileIO::readline(FM_FREQ_CTL); + if (vector_contains(*_aidl_return, freq)) + continue; + _aidl_return->push_back(freq); + } + return ::ndk::ScopedAStatus::ok(); +} +::ndk::ScopedAStatus FMSupport::close() { NOT_SUPPORTED; } +} // namespace aidl::vendor::eureka::hardware::fmradio diff --git a/universal7885-common/apps/aidl-support/fm/default/FMSupport.h b/universal7885-common/apps/aidl-support/fm/default/FMSupport.h index cb1a463..1d43119 100644 --- a/universal7885-common/apps/aidl-support/fm/default/FMSupport.h +++ b/universal7885-common/apps/aidl-support/fm/default/FMSupport.h @@ -14,19 +14,18 @@ #pragma once -#include +#include namespace aidl::vendor::eureka::hardware::fmradio { -struct FMSupport : public BnFMSysfsSupport { +struct FMSupport : public BnFMDevControl { public: FMSupport() = default; // Methods from aidl::vendor::eureka::hardware::fmradio::IFMRadio follow. - ::ndk::ScopedAStatus setManualFreq(float freq) override; - ::ndk::ScopedAStatus adjustFreqByStep(Direction dir) override; - ::ndk::ScopedAStatus isAvailable(bool *aidl_return) override; - ::ndk::ScopedAStatus getFreqFromSysfs(int32_t *aidl_return) override; - ::ndk::ScopedAStatus setChannelSpacing(Space space) override; - ::ndk::ScopedAStatus getChannelSpacing(Space *_aidl_return) override; + ::ndk::ScopedAStatus open(void) override; + ::ndk::ScopedAStatus getValue(GetType type, int *_aidl_return) override; + ::ndk::ScopedAStatus setValue(SetType type, int value) override; + ::ndk::ScopedAStatus getFreqsList(std::vector *_aidl_return) override; + ::ndk::ScopedAStatus close(void) override; }; } // namespace aidl::vendor::eureka::hardware::fmradio diff --git a/universal7885-common/apps/aidl-support/fm/default/service.cpp b/universal7885-common/apps/aidl-support/fm/default/service.cpp index d7ac6ee..3c34406 100644 --- a/universal7885-common/apps/aidl-support/fm/default/service.cpp +++ b/universal7885-common/apps/aidl-support/fm/default/service.cpp @@ -22,19 +22,19 @@ using ::aidl::vendor::eureka::hardware::fmradio::FMDevControl; using ::aidl::vendor::eureka::hardware::fmradio::FMSupport; -template static void registerAsService(std::shared_ptr service) { - const std::string instance = std::string() + C::descriptor + "/default"; +template static void registerAsService(std::shared_ptr service, const char *inst) { + const std::string instance = std::string() + C::descriptor + "/" + inst; binder_status_t status = AServiceManager_addService(service->asBinder().get(), instance.c_str()); CHECK(status == STATUS_OK); - LOG(INFO) << "Register done for " << C::descriptor; + LOG(INFO) << "Register done for instance " << inst; } int main() { ABinderProcess_setThreadPoolMaxThreadCount(0); - registerAsService(ndk::SharedRefBase::make()); - registerAsService(ndk::SharedRefBase::make()); + registerAsService(ndk::SharedRefBase::make(), "support"); + registerAsService(ndk::SharedRefBase::make(), "default"); ABinderProcess_joinThreadPool(); return -1; // should never get here diff --git a/universal7885-common/apps/aidl-support/fm/default/vendor.eureka.hardware.fmradio.xml b/universal7885-common/apps/aidl-support/fm/default/vendor.eureka.hardware.fmradio.xml index 8233be1..f2e5c17 100644 --- a/universal7885-common/apps/aidl-support/fm/default/vendor.eureka.hardware.fmradio.xml +++ b/universal7885-common/apps/aidl-support/fm/default/vendor.eureka.hardware.fmradio.xml @@ -1,7 +1,7 @@ vendor.eureka.hardware.fmradio - IFMSysfsSupport/default + IFMDevControl/support IFMDevControl/default diff --git a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Direction.aidl b/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Direction.aidl deleted file mode 100644 index f3897dc..0000000 --- a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Direction.aidl +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (C) 2022 Eureka Team -// -// 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. - -package vendor.eureka.hardware.fmradio; - -@Backing(type="int") -enum Direction { - UP, - DOWN, -} diff --git a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/GetType.aidl b/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/GetType.aidl index 7d7ee30..0649c0a 100644 --- a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/GetType.aidl +++ b/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/GetType.aidl @@ -21,5 +21,6 @@ enum GetType { GET_TYPE_FM_LOWER_LIMIT, GET_TYPE_FM_RMSSI, GET_TYPE_FM_BEFORE_CHANNEL, - GET_TYPE_FM_NEXT_CHANNEL + GET_TYPE_FM_NEXT_CHANNEL, + GET_TYPE_FM_SYSFS_IF, } diff --git a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/IFMSysfsSupport.aidl b/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/IFMSysfsSupport.aidl deleted file mode 100644 index f914bc5..0000000 --- a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/IFMSysfsSupport.aidl +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (C) 2022 Eureka Team -// -// 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. - -package vendor.eureka.hardware.fmradio; - -import vendor.eureka.hardware.fmradio.Direction; -import vendor.eureka.hardware.fmradio.Space; - -interface IFMSysfsSupport { - void adjustFreqByStep(in Direction dir); - - Space getChannelSpacing(); - - int getFreqFromSysfs(); - - boolean isAvailable(); - - void setChannelSpacing(in Space space); - - void setManualFreq(in float freq); -} diff --git a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Space.aidl b/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Space.aidl deleted file mode 100644 index 80f6975..0000000 --- a/universal7885-common/apps/aidl-support/fm/vendor/eureka/hardware/fmradio/Space.aidl +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2022 Eureka Team -// -// 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. - -package vendor.eureka.hardware.fmradio; - -@Backing(type="int") -enum Space { - CHANNEL_SPACING_10HZ = 1, - CHANNEL_SPACING_20HZ, - CHANNEL_SPACING_30HZ, - CHANNEL_SPACING_40HZ, - CHANNEL_SPACING_50HZ = 5, -} diff --git a/universal7885-common/sepolicy/private/service_contexts b/universal7885-common/sepolicy/private/service_contexts index 248ed5f..5fb0ead 100644 --- a/universal7885-common/sepolicy/private/service_contexts +++ b/universal7885-common/sepolicy/private/service_contexts @@ -7,4 +7,3 @@ vendor.eureka.hardware.parts.ISmartCharge u:object_r:hal_parts_service:s0 # FMRadio vendor.eureka.hardware.fmradio.IFMDevControl u:object_r:hal_fmradio_service:s0 -vendor.eureka.hardware.fmradio.IFMSysfsSupport u:object_r:hal_fmradio_service:s0