universal7885: fm-aidl: Use std::timed_mutex to provide thread safe operation

- Also sort out common macro to new header
This commit is contained in:
roynatech2544 2022-10-17 18:59:42 +09:00
commit 30f8177b3d
6 changed files with 49 additions and 13 deletions

View file

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

View file

@ -19,21 +19,25 @@
#include <android/binder_manager.h> #include <android/binder_manager.h>
#include <cassert> #include <cassert>
#include <cerrno>
#include <fcntl.h> #include <fcntl.h>
#include "CommonMacro.h"
#include <aidl/vendor/eureka/hardware/audio_route/BnAudioRoute.h> #include <aidl/vendor/eureka/hardware/audio_route/BnAudioRoute.h>
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
static int fd = -1;
::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) {
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:
@ -57,14 +61,16 @@ static int fd = -1;
*_aidl_return = fm_radio_slsi::next_channel(fd); *_aidl_return = fm_radio_slsi::next_channel(fd);
break; break;
case GetType::GET_TYPE_FM_SYSFS_IF: case GetType::GET_TYPE_FM_SYSFS_IF:
[[fallthrough]]; NOT_SUPPORTED;
default: default:
break; break;
}; };
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::setValue(SetType type, int value) { ::ndk::ScopedAStatus FMDevControl::setValue(SetType type, int value) {
RETURN_IF_FAILED_LOCK;
assert(fd > 0); assert(fd > 0);
std::shared_ptr<audio_route::IAudioRoute> svc; std::shared_ptr<audio_route::IAudioRoute> svc;
switch (type) { switch (type) {
@ -93,13 +99,17 @@ static int fd = -1;
default: default:
break; break;
}; };
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMDevControl::getFreqsList(std::vector<int> *_aidl_return){ ::ndk::ScopedAStatus FMDevControl::getFreqsList(std::vector<int> *_aidl_return){
RETURN_IF_FAILED_LOCK;
auto vec = fm_radio_slsi::get_freqs(fd); auto vec = fm_radio_slsi::get_freqs(fd);
for (auto i : vec) for (auto i : vec)
_aidl_return->push_back(i); _aidl_return->push_back(i);
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }

View file

@ -16,10 +16,12 @@
#include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h> #include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h>
#include <mutex>
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
struct FMDevControl : public BnFMDevControl { struct FMDevControl : public BnFMDevControl {
public: public:
FMDevControl() = default; FMDevControl() = default;
// 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;
@ -27,5 +29,8 @@ public:
::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:
int fd;
std::timed_mutex lock;
}; };
} // namespace aidl::vendor::eureka::hardware::fmradio } // namespace aidl::vendor::eureka::hardware::fmradio

View file

@ -22,13 +22,7 @@
#include <FileIO.h> #include <FileIO.h>
#include <android-base/logging.h> #include "CommonMacro.h"
#define NOT_SUPPORTED \
({ \
LOG(ERROR) << __func__ << ": Attempted to invoke unsupported operation"; \
return ::ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); \
})
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
@ -40,6 +34,7 @@ constexpr const char *FM_FREQ_SEEK =
::ndk::ScopedAStatus FMSupport::open(void) { NOT_SUPPORTED; } ::ndk::ScopedAStatus FMSupport::open(void) { NOT_SUPPORTED; }
::ndk::ScopedAStatus FMSupport::getValue(GetType type, int *_aidl_return) { ::ndk::ScopedAStatus FMSupport::getValue(GetType type, int *_aidl_return) {
RETURN_IF_FAILED_LOCK;
switch (type) { switch (type) {
case GetType::GET_TYPE_FM_FREQ: case GetType::GET_TYPE_FM_FREQ:
*_aidl_return = FileIO::readline(FM_FREQ_CTL); *_aidl_return = FileIO::readline(FM_FREQ_CTL);
@ -62,9 +57,11 @@ constexpr const char *FM_FREQ_SEEK =
default: default:
break; break;
}; };
lock.unlock();
return ndk::ScopedAStatus::ok(); return ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMSupport::setValue(SetType type, int value) { ::ndk::ScopedAStatus FMSupport::setValue(SetType type, int value) {
RETURN_IF_FAILED_LOCK;
switch (type) { switch (type) {
case SetType::SET_TYPE_FM_FREQ: case SetType::SET_TYPE_FM_FREQ:
FileIO::writeline(FM_FREQ_CTL, value * 1000); FileIO::writeline(FM_FREQ_CTL, value * 1000);
@ -79,6 +76,7 @@ constexpr const char *FM_FREQ_SEEK =
default: default:
break; break;
}; };
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
@ -92,6 +90,7 @@ static inline bool vector_contains(const std::vector<int> vec,
} }
::ndk::ScopedAStatus FMSupport::getFreqsList(std::vector<int> *_aidl_return) { ::ndk::ScopedAStatus FMSupport::getFreqsList(std::vector<int> *_aidl_return) {
RETURN_IF_FAILED_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(3 * 10)); FileIO::writeline(FM_FREQ_SEEK, "1 " + std::to_string(3 * 10));
int freq = FileIO::readline(FM_FREQ_CTL); int freq = FileIO::readline(FM_FREQ_CTL);
@ -99,6 +98,7 @@ static inline bool vector_contains(const std::vector<int> vec,
continue; continue;
_aidl_return->push_back(freq); _aidl_return->push_back(freq);
} }
lock.unlock();
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }
::ndk::ScopedAStatus FMSupport::close() { NOT_SUPPORTED; } ::ndk::ScopedAStatus FMSupport::close() { NOT_SUPPORTED; }

View file

@ -16,6 +16,8 @@
#include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h> #include <aidl/vendor/eureka/hardware/fmradio/BnFMDevControl.h>
#include <mutex>
namespace aidl::vendor::eureka::hardware::fmradio { namespace aidl::vendor::eureka::hardware::fmradio {
struct FMSupport : public BnFMDevControl { struct FMSupport : public BnFMDevControl {
@ -27,5 +29,7 @@ public:
::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:
std::timed_mutex lock;
}; };
} // namespace aidl::vendor::eureka::hardware::fmradio } // namespace aidl::vendor::eureka::hardware::fmradio

View file

@ -16,6 +16,8 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <FileIO.h>
#include "BatteryConstants.h"
namespace aidl::vendor::eureka::hardware::parts { namespace aidl::vendor::eureka::hardware::parts {
@ -25,7 +27,7 @@ static int restart = 0;
static int limit_stat = 0; static int limit_stat = 0;
static int restart_stat = 0; static int restart_stat = 0;
static std::thread monitor_th = nullptr; static std::thread *monitor_th = nullptr;
static void battery_monitor(void) { static void battery_monitor(void) {
while (true) { while (true) {
@ -46,7 +48,7 @@ static void battery_monitor(void) {
return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage( return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage(
EX_ILLEGAL_ARGUMENT, "Start called without configuring."); EX_ILLEGAL_ARGUMENT, "Start called without configuring.");
monitor_th = std::thread(battery_monitor); monitor_th = new std::thread(battery_monitor);
return ::ndk::ScopedAStatus::ok(); return ::ndk::ScopedAStatus::ok();
} }