vibrator: common: Check whether file is opened correctly

- Add compile-time type checking inside the has() function to ensure it
  receives the correct type.
- Add is_open() inside the has() function.

Bug: 350911314
Test: alarm, ringtone vibrations
Test: keyboard vibrations
Test: idlcli commands
Test: atest PTS, CTS, VTS
Flag: EXEMPT bugfix
Change-Id: Ifa8f197f6f4b25d51b2edaf6fec27a086bd4f73c
This commit is contained in:
leonardian 2024-07-08 17:00:50 +08:00
parent 0dcee74707
commit 1d29f93863
3 changed files with 14 additions and 6 deletions

View file

@ -40,10 +40,6 @@ void HwApiBase::saveName(const std::string &name, const std::ios *stream) {
mNames[stream] = name; mNames[stream] = name;
} }
bool HwApiBase::has(const std::ios &stream) {
return !!stream;
}
void HwApiBase::debug(int fd) { void HwApiBase::debug(int fd) {
dprintf(fd, "Kernel:\n"); dprintf(fd, "Kernel:\n");

View file

@ -24,6 +24,7 @@
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <type_traits>
#include "utils.h" #include "utils.h"
@ -67,7 +68,8 @@ class HwApiBase {
void saveName(const std::string &name, const std::ios *stream); void saveName(const std::string &name, const std::ios *stream);
template <typename T> template <typename T>
void open(const std::string &name, T *stream); void open(const std::string &name, T *stream);
bool has(const std::ios &stream); template <typename T>
bool has(const T &stream);
template <typename T> template <typename T>
bool get(T *value, std::istream *stream); bool get(T *value, std::istream *stream);
template <typename T> template <typename T>
@ -93,6 +95,16 @@ void HwApiBase::open(const std::string &name, T *stream) {
utils::openNoCreate(mPathPrefix + name, stream); utils::openNoCreate(mPathPrefix + name, stream);
} }
template <typename T>
bool HwApiBase::has(const T &stream) {
if constexpr (std::is_same<T, std::fstream>::value || std::is_same<T, std::ofstream>::value ||
std::is_same<T, std::ifstream>::value)
return stream.is_open() && !stream.fail();
ALOGE("File stream is not of the correct type");
return false;
}
template <typename T> template <typename T>
bool HwApiBase::get(T *value, std::istream *stream) { bool HwApiBase::get(T *value, std::istream *stream) {
ATRACE_NAME("HwApi::get"); ATRACE_NAME("HwApi::get");

View file

@ -92,7 +92,7 @@ class HwApi : public Vibrator::HwApi, private HwApiBase {
bool setRedc(std::string value) override { return set(value, &mRedc); } bool setRedc(std::string value) override { return set(value, &mRedc); }
bool setQ(std::string value) override { return set(value, &mQ); } bool setQ(std::string value) override { return set(value, &mQ); }
bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); } bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
bool hasEffectBrakingTimeBank() override { return mEffectBrakingTimeBank.is_open(); } bool hasEffectBrakingTimeBank() override { return has(mEffectBrakingTimeBank); }
bool setEffectBrakingTimeBank(uint32_t value) override { bool setEffectBrakingTimeBank(uint32_t value) override {
return set(value, &mEffectBrakingTimeBank); return set(value, &mEffectBrakingTimeBank);
} }