universal7885: fm-aidl: Run clang-format

This commit is contained in:
roynatech2544 2022-11-09 19:03:21 +09:00
commit 7d23777f39
8 changed files with 203 additions and 188 deletions

View file

@ -2,14 +2,14 @@
#include <android-base/logging.h> #include <android-base/logging.h>
#define RETURN_IF_FAILED_LOCK \ #define RETURN_IF_FAILED_LOCK \
({ \ ({ \
if (!lock.try_lock_for(std::chrono::milliseconds(100))) \ if (!lock.try_lock_for(std::chrono::milliseconds(100))) \
return ::ndk::ScopedAStatus::fromServiceSpecificError(-ETIME); \ return ::ndk::ScopedAStatus::fromServiceSpecificError(-ETIME); \
}) })
#define NOT_SUPPORTED \ #define NOT_SUPPORTED \
({ \ ({ \
LOG(ERROR) << __func__ << ": Attempted to invoke unsupported operation"; \ LOG(ERROR) << __func__ << ": Attempted to invoke unsupported operation"; \
return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); \ return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); \
}) })

View file

@ -16,16 +16,16 @@
#include "FMDevControl.h" #include "FMDevControl.h"
#include <fm_slsi-impl.h>
#include <LogFormat.h> #include <LogFormat.h>
#include <fm_slsi-impl.h>
#include <android/binder_manager.h> #include <android/binder_manager.h>
#include <log/log.h> #include <log/log.h>
#include <algorithm>
#include <cassert> #include <cassert>
#include <cerrno> #include <cerrno>
#include <chrono> #include <chrono>
#include <algorithm>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
@ -37,145 +37,154 @@
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
::ndk::ScopedAStatus FMDevControl::open(void) { ::ndk::ScopedAStatus FMDevControl::open(void) {
fd = fm_radio_slsi::open_device(); fd = fm_radio_slsi::open_device();
assert(fd > 0); assert(fd > 0);
fm_radio_slsi::bootctrl(fd); fm_radio_slsi::bootctrl(fd);
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::getValue(GetType type, int *_aidl_return) { ::ndk::ScopedAStatus FMDevControl::getValue(GetType type, int *_aidl_return) {
LOG_D("%s: type %d", __func__, type); LOG_D("%s: type %d", __func__, type);
if (type != GetType::GET_TYPE_FM_MUTEX_LOCKED) { if (type != GetType::GET_TYPE_FM_MUTEX_LOCKED) {
RETURN_IF_FAILED_LOCK; RETURN_IF_FAILED_LOCK;
} }
assert(fd > 0); assert(fd > 0);
switch (type) { switch (type) {
case GetType::GET_TYPE_FM_FREQ: case GetType::GET_TYPE_FM_FREQ:
fm_radio_slsi::get_frequency(fd, _aidl_return); fm_radio_slsi::get_frequency(fd, _aidl_return);
break; break;
case GetType::GET_TYPE_FM_UPPER_LIMIT: case GetType::GET_TYPE_FM_UPPER_LIMIT:
*_aidl_return = fm_radio_slsi::get_upperband_limit(fd); *_aidl_return = fm_radio_slsi::get_upperband_limit(fd);
break; break;
case GetType::GET_TYPE_FM_LOWER_LIMIT: case GetType::GET_TYPE_FM_LOWER_LIMIT:
*_aidl_return = fm_radio_slsi::get_lowerband_limit(fd); *_aidl_return = fm_radio_slsi::get_lowerband_limit(fd);
break; break;
case GetType::GET_TYPE_FM_RMSSI: case GetType::GET_TYPE_FM_RMSSI:
*_aidl_return = fm_radio_slsi::get_rmssi(fd); *_aidl_return = fm_radio_slsi::get_rmssi(fd);
break; break;
case GetType::GET_TYPE_FM_BEFORE_CHANNEL: case GetType::GET_TYPE_FM_BEFORE_CHANNEL:
if (index > 0) index -= 1; if (index > 0)
if (kMiddleState != nullptr) { index -= 1;
index = kMiddleState->first; if (kMiddleState != nullptr) {
delete kMiddleState; index = kMiddleState->first;
kMiddleState = nullptr; delete kMiddleState;
} kMiddleState = nullptr;
fm_radio_slsi::set_frequency(fd, freqs_list[index]); }
*_aidl_return = freqs_list[index]; fm_radio_slsi::set_frequency(fd, freqs_list[index]);
break; *_aidl_return = freqs_list[index];
case GetType::GET_TYPE_FM_NEXT_CHANNEL: break;
if (index < freqs_list.size() - 1) index += 1; case GetType::GET_TYPE_FM_NEXT_CHANNEL:
if (kMiddleState != nullptr) { if (index < freqs_list.size() - 1)
index = kMiddleState->second; index += 1;
delete kMiddleState; if (kMiddleState != nullptr) {
kMiddleState = nullptr; index = kMiddleState->second;
} delete kMiddleState;
fm_radio_slsi::set_frequency(fd, freqs_list[index]); kMiddleState = nullptr;
*_aidl_return = freqs_list[index]; }
break; fm_radio_slsi::set_frequency(fd, freqs_list[index]);
case GetType::GET_TYPE_FM_SYSFS_IF: *_aidl_return = freqs_list[index];
NOT_SUPPORTED; break;
case GetType::GET_TYPE_FM_MUTEX_LOCKED: case GetType::GET_TYPE_FM_SYSFS_IF:
if (lock.try_lock()){ NOT_SUPPORTED;
*_aidl_return = false; case GetType::GET_TYPE_FM_MUTEX_LOCKED:
lock.unlock(); if (lock.try_lock()) {
} else { *_aidl_return = false;
*_aidl_return = true; lock.unlock();
} } else {
break; *_aidl_return = true;
default: }
break; break;
}; default:
if (type != GetType::GET_TYPE_FM_MUTEX_LOCKED) { break;
lock.unlock(); };
} if (type != GetType::GET_TYPE_FM_MUTEX_LOCKED) {
LOG_D("%s: returning %d", __func__, *_aidl_return); lock.unlock();
}
LOG_D("%s: returning %d", __func__, *_aidl_return);
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::setValue(SetType type, int value) { ::ndk::ScopedAStatus FMDevControl::setValue(SetType type, int value) {
using audio_route::IAudioRoute; using audio_route::IAudioRoute;
LOG_D("%s: type %d, value %d", __func__, type, value); LOG_D("%s: type %d, value %d", __func__, type, value);
RETURN_IF_FAILED_LOCK; RETURN_IF_FAILED_LOCK;
assert(fd > 0); assert(fd > 0);
switch (type) { switch (type) {
case SetType::SET_TYPE_FM_FREQ: case SetType::SET_TYPE_FM_FREQ:
fm_radio_slsi::set_frequency(fd, value); fm_radio_slsi::set_frequency(fd, value);
if (std::find(freqs_list.begin(), freqs_list.end(), value) == freqs_list.end()) if (std::find(freqs_list.begin(), freqs_list.end(), value) ==
kMiddleState = saveMiddleState(value, freqs_list); freqs_list.end())
break; kMiddleState = saveMiddleState(value, freqs_list);
case SetType::SET_TYPE_FM_MUTE: break;
fm_radio_slsi::set_mute(fd, value); case SetType::SET_TYPE_FM_MUTE:
break; fm_radio_slsi::set_mute(fd, value);
case SetType::SET_TYPE_FM_VOLUME: break;
fm_radio_slsi::set_volume(fd, value); case SetType::SET_TYPE_FM_VOLUME:
break; fm_radio_slsi::set_volume(fd, value);
case SetType::SET_TYPE_FM_THREAD: break;
fm_radio_slsi::fm_thread_set(fd, value); case SetType::SET_TYPE_FM_THREAD:
break; fm_radio_slsi::fm_thread_set(fd, value);
case SetType::SET_TYPE_FM_RMSSI: break;
fm_radio_slsi::set_rssi(fd, value); case SetType::SET_TYPE_FM_RMSSI:
break; fm_radio_slsi::set_rssi(fd, value);
case SetType::SET_TYPE_FM_SEARCH_CANCEL: break;
fm_radio_slsi::stop_search(fd); case SetType::SET_TYPE_FM_SEARCH_CANCEL:
break; fm_radio_slsi::stop_search(fd);
case SetType::SET_TYPE_FM_SPEAKER_ROUTE: { break;
auto svc = IAudioRoute::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService("vendor.eureka.hardware.audio_route.IAudioRoute/default"))); case SetType::SET_TYPE_FM_SPEAKER_ROUTE: {
svc->setParam(value ? "routing=2": "routing=8"); auto svc =
break; IAudioRoute::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(
} "vendor.eureka.hardware.audio_route.IAudioRoute/default")));
case SetType::SET_TYPE_FM_SEARCH_START: svc->setParam(value ? "routing=2" : "routing=8");
lock.unlock(); break;
search_thread = new std::thread([this] { }
const std::lock_guard<std::timed_mutex> guard(lock); case SetType::SET_TYPE_FM_SEARCH_START:
freqs_list = fm_radio_slsi::get_freqs(fd); lock.unlock();
std::sort(freqs_list.begin(), freqs_list.end(), std::less<int>()); search_thread = new std::thread([this] {
}); const std::lock_guard<std::timed_mutex> guard(lock);
break; freqs_list = fm_radio_slsi::get_freqs(fd);
case SetType::SET_TYPE_FM_APP_PID: { std::sort(freqs_list.begin(), freqs_list.end(), std::less<int>());
client_observe_thread = new std::thread([=] { });
pid_t pid = value; break;
case SetType::SET_TYPE_FM_APP_PID: {
client_observe_thread = new std::thread([=] {
pid_t pid = value;
LOG_D("FM_APP_PID: received value %d", pid); LOG_D("FM_APP_PID: received value %d", pid);
while (true) { while (true) {
if (kill(pid, 0) < 0 && errno == ESRCH) break; if (kill(pid, 0) < 0 && errno == ESRCH)
std::this_thread::sleep_for(std::chrono::seconds(2)); break;
} std::this_thread::sleep_for(std::chrono::seconds(2));
LOG_W("FM_APP_PID: Starting client death receiver"); }
fm_radio_slsi::fm_thread_set(fd, 0); LOG_W("FM_APP_PID: Starting client death receiver");
close(); fm_radio_slsi::fm_thread_set(fd, 0);
}); close();
break; });
} break;
default: }
break; default:
}; break;
if (type != SetType::SET_TYPE_FM_SEARCH_START) lock.unlock(); };
return ::ndk::ScopedAStatus::ok(); if (type != SetType::SET_TYPE_FM_SEARCH_START)
lock.unlock();
return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::getFreqsList(std::vector<int> *_aidl_return){ ::ndk::ScopedAStatus
RETURN_IF_FAILED_LOCK; FMDevControl::getFreqsList(std::vector<int> *_aidl_return) {
*_aidl_return = freqs_list; RETURN_IF_FAILED_LOCK;
lock.unlock(); *_aidl_return = freqs_list;
return ::ndk::ScopedAStatus::ok(); lock.unlock();
return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::close() { ::ndk::ScopedAStatus FMDevControl::close() {
if (fd > 0) ::close(fd); if (fd > 0)
fd = -1; ::close(fd);
return ::ndk::ScopedAStatus::ok(); fd = -1;
return ::ndk::ScopedAStatus::ok();
} }
} // namespace aidl::vendor::eureka::hardware::fmradio } // namespace aidl::vendor::eureka::hardware::fmradio

View file

@ -24,17 +24,16 @@
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
struct FMDevControl : public BnFMDevControl { struct FMDevControl : public BnFMDevControl {
public: public:
FMDevControl(void) { FMDevControl(void) { index = 0; }
index = 0;
}
// Methods from aidl::vendor::eureka::hardware::fmradio::IFMRadio follow. // Methods from aidl::vendor::eureka::hardware::fmradio::IFMRadio follow.
::ndk::ScopedAStatus open(void) override; ::ndk::ScopedAStatus open(void) override;
::ndk::ScopedAStatus getValue(GetType type, int *_aidl_return) override; ::ndk::ScopedAStatus getValue(GetType type, int *_aidl_return) override;
::ndk::ScopedAStatus setValue(SetType type, int value) override; ::ndk::ScopedAStatus setValue(SetType type, int value) override;
::ndk::ScopedAStatus getFreqsList(std::vector<int> *_aidl_return) override; ::ndk::ScopedAStatus getFreqsList(std::vector<int> *_aidl_return) override;
::ndk::ScopedAStatus close(void) override; ::ndk::ScopedAStatus close(void) override;
private:
private:
int fd; int fd;
unsigned int index; unsigned int index;
std::timed_mutex lock; std::timed_mutex lock;

View file

@ -45,21 +45,23 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
case GetType::GET_TYPE_FM_RMSSI: case GetType::GET_TYPE_FM_RMSSI:
NOT_SUPPORTED; NOT_SUPPORTED;
case GetType::GET_TYPE_FM_BEFORE_CHANNEL: case GetType::GET_TYPE_FM_BEFORE_CHANNEL:
if (index > 0) index -= 1; if (index > 0)
index -= 1;
if (kMiddleState != nullptr) { if (kMiddleState != nullptr) {
index = kMiddleState->first; index = kMiddleState->first;
delete kMiddleState; delete kMiddleState;
kMiddleState = nullptr; kMiddleState = nullptr;
} }
FileIO::writeline(FM_FREQ_CTL, freqs_list[index]); FileIO::writeline(FM_FREQ_CTL, freqs_list[index]);
*_aidl_return = freqs_list[index]; *_aidl_return = freqs_list[index];
break; break;
case GetType::GET_TYPE_FM_NEXT_CHANNEL: case GetType::GET_TYPE_FM_NEXT_CHANNEL:
if (index < freqs_list.size() - 1) index += 1; if (index < freqs_list.size() - 1)
index += 1;
if (kMiddleState != nullptr) { if (kMiddleState != nullptr) {
index = kMiddleState->second; index = kMiddleState->second;
delete kMiddleState; delete kMiddleState;
kMiddleState = nullptr; kMiddleState = nullptr;
} }
FileIO::writeline(FM_FREQ_CTL, freqs_list[index]); FileIO::writeline(FM_FREQ_CTL, freqs_list[index]);
*_aidl_return = freqs_list[index]; *_aidl_return = freqs_list[index];
@ -69,10 +71,10 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
break; break;
case GetType::GET_TYPE_FM_MUTEX_LOCKED: case GetType::GET_TYPE_FM_MUTEX_LOCKED:
if (lock.try_lock()) { if (lock.try_lock()) {
*_aidl_return = false; *_aidl_return = false;
lock.unlock(); lock.unlock();
} else { } else {
*_aidl_return = true; *_aidl_return = true;
} }
break; break;
default: default:
@ -88,8 +90,9 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
switch (type) { switch (type) {
case SetType::SET_TYPE_FM_FREQ: case SetType::SET_TYPE_FM_FREQ:
FileIO::writeline(FM_FREQ_CTL, value); FileIO::writeline(FM_FREQ_CTL, value);
if (std::find(freqs_list.begin(), freqs_list.end(), value) == freqs_list.end()) if (std::find(freqs_list.begin(), freqs_list.end(), value) ==
kMiddleState = saveMiddleState(value, freqs_list); freqs_list.end())
kMiddleState = saveMiddleState(value, freqs_list);
break; break;
case SetType::SET_TYPE_FM_MUTE: case SetType::SET_TYPE_FM_MUTE:
case SetType::SET_TYPE_FM_VOLUME: case SetType::SET_TYPE_FM_VOLUME:
@ -102,20 +105,24 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
case SetType::SET_TYPE_FM_SEARCH_START: case SetType::SET_TYPE_FM_SEARCH_START:
lock.unlock(); lock.unlock();
search_thread = new std::thread([this] { search_thread = new std::thread([this] {
const std::lock_guard<std::timed_mutex> guard(lock); const std::lock_guard<std::timed_mutex> guard(lock);
for (int i = 0; i < TRACK_SIZE; i++) { for (int i = 0; i < TRACK_SIZE; i++) {
FileIO::writeline(FM_FREQ_SEEK, "1 " + std::to_string(SYSFS_SPACING * 10)); FileIO::writeline(FM_FREQ_SEEK,
int freq = FileIO::readline(FM_FREQ_CTL); "1 " + std::to_string(SYSFS_SPACING * 10));
if (std::find(freqs_list.begin(), freqs_list.end(), freq) != freqs_list.end()) continue; int freq = FileIO::readline(FM_FREQ_CTL);
freqs_list.push_back(freq); if (std::find(freqs_list.begin(), freqs_list.end(), freq) !=
} freqs_list.end())
std::sort(freqs_list.begin(), freqs_list.end(), std::less<int>()); continue;
freqs_list.push_back(freq);
}
std::sort(freqs_list.begin(), freqs_list.end(), std::less<int>());
}); });
break; break;
default: default:
break; break;
}; };
if (type != SetType::SET_TYPE_FM_SEARCH_START) lock.unlock(); if (type != SetType::SET_TYPE_FM_SEARCH_START)
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }

View file

@ -16,24 +16,23 @@
#include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h> #include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h>
#include "MiddleState.h"
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include "MiddleState.h"
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
struct FMSupport : public BnFMDevControl { struct FMSupport : public BnFMDevControl {
public: public:
FMSupport(void) { FMSupport(void) { index = 0; };
index = 0;
};
// Methods from aidl::vendor::eureka::hardware::fmradio::IFMRadio follow. // Methods from aidl::vendor::eureka::hardware::fmradio::IFMRadio follow.
::ndk::ScopedAStatus open(void) override; ::ndk::ScopedAStatus open(void) override;
::ndk::ScopedAStatus getValue(GetType type, int *_aidl_return) override; ::ndk::ScopedAStatus getValue(GetType type, int *_aidl_return) override;
::ndk::ScopedAStatus setValue(SetType type, int value) override; ::ndk::ScopedAStatus setValue(SetType type, int value) override;
::ndk::ScopedAStatus getFreqsList(std::vector<int> *_aidl_return) override; ::ndk::ScopedAStatus getFreqsList(std::vector<int> *_aidl_return) override;
::ndk::ScopedAStatus close(void) override; ::ndk::ScopedAStatus close(void) override;
private:
private:
std::timed_mutex lock; std::timed_mutex lock;
std::thread *search_thread; std::thread *search_thread;
std::vector<int> freqs_list; std::vector<int> freqs_list;

View file

@ -1,20 +1,20 @@
#include "MiddleState.h" #include "MiddleState.h"
middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec) middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec) {
{ unsigned int i;
unsigned int i;
// Bounds checking, we need at least 2 elements on the vector. // Bounds checking, we need at least 2 elements on the vector.
// If it doesn't meet the requirements, return nullptr to keep it // If it doesn't meet the requirements, return nullptr to keep it
// as-is. // as-is.
if (vec.size() <= 1) return nullptr; if (vec.size() <= 1)
return nullptr;
for (i = 0; i < vec.size() - 1; i++) { for (i = 0; i < vec.size() - 1; i++) {
const int first = vec[i] - value; const int first = vec[i] - value;
const int second = vec[i + 1] - value; const int second = vec[i + 1] - value;
if (first * second < 0) if (first * second < 0)
break; break;
} }
return new middlestate_t {i, i + 1}; return new middlestate_t{i, i + 1};
} }

View file

@ -3,8 +3,8 @@
#pragma once #pragma once
struct pair { struct pair {
unsigned int first; unsigned int first;
unsigned int second; unsigned int second;
}; };
using middlestate_t = struct pair; using middlestate_t = struct pair;

View file

@ -16,13 +16,14 @@
#include <android/binder_manager.h> #include <android/binder_manager.h>
#include <android/binder_process.h> #include <android/binder_process.h>
#include "FMSupport.h"
#include "FMDevControl.h" #include "FMDevControl.h"
#include "FMSupport.h"
using ::aidl::vendor::eureka::hardware::fmradio::FMDevControl; using ::aidl::vendor::eureka::hardware::fmradio::FMDevControl;
using ::aidl::vendor::eureka::hardware::fmradio::FMSupport; using ::aidl::vendor::eureka::hardware::fmradio::FMSupport;
template <class C> static void registerAsService(std::shared_ptr<C> service, const char *inst) { template <class C>
static void registerAsService(std::shared_ptr<C> service, const char *inst) {
const std::string instance = std::string() + C::descriptor + "/" + inst; const std::string instance = std::string() + C::descriptor + "/" + inst;
binder_status_t status = binder_status_t status =
AServiceManager_addService(service->asBinder().get(), instance.c_str()); AServiceManager_addService(service->asBinder().get(), instance.c_str());