1717#define LOG_TAG " VibratorHalWrapper"
1818
1919#include < aidl/android/hardware/vibrator/IVibrator.h>
20- #include < android/hardware/vibrator/1.3/IVibrator.h>
2120#include < hardware/vibrator.h>
2221#include < cmath>
2322
@@ -33,32 +32,18 @@ using aidl::android::hardware::vibrator::CompositePwleV2;
3332using aidl::android::hardware::vibrator::Effect;
3433using aidl::android::hardware::vibrator::EffectStrength;
3534using aidl::android::hardware::vibrator::FrequencyAccelerationMapEntry;
35+ using aidl::android::hardware::vibrator::IVibrator;
3636using aidl::android::hardware::vibrator::PrimitivePwle;
3737using aidl::android::hardware::vibrator::VendorEffect;
3838
3939using std::chrono::milliseconds;
4040
41- namespace V1_0 = android::hardware::vibrator::V1_0;
42- namespace V1_1 = android::hardware::vibrator::V1_1;
43- namespace V1_2 = android::hardware::vibrator::V1_2;
44- namespace V1_3 = android::hardware::vibrator::V1_3;
45- namespace Aidl = aidl::android::hardware::vibrator;
46-
4741namespace android {
4842
4943namespace vibrator {
5044
5145// -------------------------------------------------------------------------------------------------
5246
53- template <class T >
54- bool isStaticCastValid (Effect effect) {
55- T castEffect = static_cast <T>(effect);
56- auto iter = hardware::hidl_enum_range<T>();
57- return castEffect >= *iter.begin () && castEffect <= *std::prev (iter.end ());
58- }
59-
60- // -------------------------------------------------------------------------------------------------
61-
6247Info HalWrapper::getInfo () {
6348 getCapabilities ();
6449 getPrimitiveDurations ();
@@ -261,7 +246,7 @@ void AidlHalWrapper::tryReconnect() {
261246 if (!result.isOk ()) {
262247 return ;
263248 }
264- std::shared_ptr<Aidl:: IVibrator> newHandle = result.value ();
249+ std::shared_ptr<IVibrator> newHandle = result.value ();
265250 if (newHandle) {
266251 std::lock_guard<std::mutex> lock (mHandleMutex );
267252 mHandle = std::move (newHandle);
@@ -514,219 +499,13 @@ AidlHalWrapper::getFrequencyToOutputAccelerationMapInternal() {
514499 frequencyToOutputAccelerationMap);
515500}
516501
517- std::shared_ptr<Aidl::IVibrator> AidlHalWrapper::getHal () {
518- std::lock_guard<std::mutex> lock (mHandleMutex );
519- return mHandle ;
520- }
521-
522- // -------------------------------------------------------------------------------------------------
523-
524- template <typename I>
525- HalResult<void > HidlHalWrapper<I>::ping() {
526- return HalResultFactory::fromReturn (getHal ()->ping ());
527- }
528-
529- template <typename I>
530- void HidlHalWrapper<I>::tryReconnect() {
531- sp<I> newHandle = I::tryGetService ();
532- if (newHandle) {
533- std::lock_guard<std::mutex> lock (mHandleMutex );
534- mHandle = std::move (newHandle);
535- }
536- }
537-
538- template <typename I>
539- HalResult<void > HidlHalWrapper<I>::on(milliseconds timeout,
540- const std::function<void ()>& completionCallback) {
541- auto status = getHal ()->on (timeout.count ());
542- auto ret = HalResultFactory::fromStatus (status.withDefault (V1_0::Status::UNKNOWN_ERROR));
543- if (ret.isOk ()) {
544- mCallbackScheduler ->schedule (completionCallback, timeout);
545- }
546- return ret;
547- }
548-
549- template <typename I>
550- HalResult<void > HidlHalWrapper<I>::off() {
551- auto status = getHal ()->off ();
552- return HalResultFactory::fromStatus (status.withDefault (V1_0::Status::UNKNOWN_ERROR));
553- }
554-
555- template <typename I>
556- HalResult<void > HidlHalWrapper<I>::setAmplitude(float amplitude) {
557- uint8_t amp = static_cast <uint8_t >(amplitude * std::numeric_limits<uint8_t >::max ());
558- auto status = getHal ()->setAmplitude (amp);
559- return HalResultFactory::fromStatus (status.withDefault (V1_0::Status::UNKNOWN_ERROR));
560- }
561-
562- template <typename I>
563- HalResult<void > HidlHalWrapper<I>::setExternalControl(bool ) {
564- ALOGV (" Skipped setExternalControl because Vibrator HAL does not support it" );
565- return HalResult<void >::unsupported ();
566- }
567-
568- template <typename I>
569- HalResult<void > HidlHalWrapper<I>::alwaysOnEnable(int32_t , Effect, EffectStrength) {
570- ALOGV (" Skipped alwaysOnEnable because Vibrator HAL AIDL is not available" );
571- return HalResult<void >::unsupported ();
572- }
573-
574- template <typename I>
575- HalResult<void > HidlHalWrapper<I>::alwaysOnDisable(int32_t ) {
576- ALOGV (" Skipped alwaysOnDisable because Vibrator HAL AIDL is not available" );
577- return HalResult<void >::unsupported ();
578- }
579-
580- template <typename I>
581- HalResult<Capabilities> HidlHalWrapper<I>::getCapabilitiesInternal() {
582- hardware::Return<bool > result = getHal ()->supportsAmplitudeControl ();
583- Capabilities capabilities =
584- result.withDefault (false ) ? Capabilities::AMPLITUDE_CONTROL : Capabilities::NONE;
585- return HalResultFactory::fromReturn<Capabilities>(std::move (result), capabilities);
586- }
587-
588- template <typename I>
589- template <typename T>
590- HalResult<milliseconds> HidlHalWrapper<I>::performInternal(
591- perform_fn<T> performFn, sp<I> handle, T effect, EffectStrength strength,
592- const std::function<void ()>& completionCallback) {
593- V1_0::Status status;
594- int32_t lengthMs;
595- auto effectCallback = [&status, &lengthMs](V1_0::Status retStatus, uint32_t retLengthMs) {
596- status = retStatus;
597- lengthMs = retLengthMs;
598- };
599-
600- V1_0::EffectStrength effectStrength = static_cast <V1_0::EffectStrength>(strength);
601- auto result = std::invoke (performFn, handle, effect, effectStrength, effectCallback);
602- milliseconds length = milliseconds (lengthMs);
603-
604- auto ret = HalResultFactory::fromReturn<milliseconds>(std::move (result), status, length);
605- if (ret.isOk ()) {
606- mCallbackScheduler ->schedule (completionCallback, length);
607- }
608-
609- return ret;
610- }
611-
612- template <typename I>
613- sp<I> HidlHalWrapper<I>::getHal() {
502+ std::shared_ptr<IVibrator> AidlHalWrapper::getHal () {
614503 std::lock_guard<std::mutex> lock (mHandleMutex );
615504 return mHandle ;
616505}
617506
618507// -------------------------------------------------------------------------------------------------
619508
620- HalResult<milliseconds> HidlHalWrapperV1_0::performEffect (
621- Effect effect, EffectStrength strength, const std::function<void ()>& completionCallback) {
622- if (isStaticCastValid<V1_0::Effect>(effect)) {
623- return performInternal (&V1_0::IVibrator::perform, getHal (),
624- static_cast <V1_0::Effect>(effect), strength, completionCallback);
625- }
626-
627- ALOGV (" Skipped performEffect because Vibrator HAL does not support effect %s" ,
628- Aidl::toString (effect).c_str ());
629- return HalResult<milliseconds>::unsupported ();
630- }
631-
632- // -------------------------------------------------------------------------------------------------
633-
634- HalResult<milliseconds> HidlHalWrapperV1_1::performEffect (
635- Effect effect, EffectStrength strength, const std::function<void ()>& completionCallback) {
636- if (isStaticCastValid<V1_0::Effect>(effect)) {
637- return performInternal (&V1_1::IVibrator::perform, getHal (),
638- static_cast <V1_0::Effect>(effect), strength, completionCallback);
639- }
640- if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
641- return performInternal (&V1_1::IVibrator::perform_1_1, getHal (),
642- static_cast <V1_1::Effect_1_1>(effect), strength, completionCallback);
643- }
644-
645- ALOGV (" Skipped performEffect because Vibrator HAL does not support effect %s" ,
646- Aidl::toString (effect).c_str ());
647- return HalResult<milliseconds>::unsupported ();
648- }
649-
650- // -------------------------------------------------------------------------------------------------
651-
652- HalResult<milliseconds> HidlHalWrapperV1_2::performEffect (
653- Effect effect, EffectStrength strength, const std::function<void ()>& completionCallback) {
654- if (isStaticCastValid<V1_0::Effect>(effect)) {
655- return performInternal (&V1_2::IVibrator::perform, getHal (),
656- static_cast <V1_0::Effect>(effect), strength, completionCallback);
657- }
658- if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
659- return performInternal (&V1_2::IVibrator::perform_1_1, getHal (),
660- static_cast <V1_1::Effect_1_1>(effect), strength, completionCallback);
661- }
662- if (isStaticCastValid<V1_2::Effect>(effect)) {
663- return performInternal (&V1_2::IVibrator::perform_1_2, getHal (),
664- static_cast <V1_2::Effect>(effect), strength, completionCallback);
665- }
666-
667- ALOGV (" Skipped performEffect because Vibrator HAL does not support effect %s" ,
668- Aidl::toString (effect).c_str ());
669- return HalResult<milliseconds>::unsupported ();
670- }
671-
672- // -------------------------------------------------------------------------------------------------
673-
674- HalResult<void > HidlHalWrapperV1_3::setExternalControl (bool enabled) {
675- auto result = getHal ()->setExternalControl (static_cast <uint32_t >(enabled));
676- return HalResultFactory::fromStatus (result.withDefault (V1_0::Status::UNKNOWN_ERROR));
677- }
678-
679- HalResult<milliseconds> HidlHalWrapperV1_3::performEffect (
680- Effect effect, EffectStrength strength, const std::function<void ()>& completionCallback) {
681- if (isStaticCastValid<V1_0::Effect>(effect)) {
682- return performInternal (&V1_3::IVibrator::perform, getHal (),
683- static_cast <V1_0::Effect>(effect), strength, completionCallback);
684- }
685- if (isStaticCastValid<V1_1::Effect_1_1>(effect)) {
686- return performInternal (&V1_3::IVibrator::perform_1_1, getHal (),
687- static_cast <V1_1::Effect_1_1>(effect), strength, completionCallback);
688- }
689- if (isStaticCastValid<V1_2::Effect>(effect)) {
690- return performInternal (&V1_3::IVibrator::perform_1_2, getHal (),
691- static_cast <V1_2::Effect>(effect), strength, completionCallback);
692- }
693- if (isStaticCastValid<V1_3::Effect>(effect)) {
694- return performInternal (&V1_3::IVibrator::perform_1_3, getHal (),
695- static_cast <V1_3::Effect>(effect), strength, completionCallback);
696- }
697-
698- ALOGV (" Skipped performEffect because Vibrator HAL does not support effect %s" ,
699- Aidl::toString (effect).c_str ());
700- return HalResult<milliseconds>::unsupported ();
701- }
702-
703- HalResult<Capabilities> HidlHalWrapperV1_3::getCapabilitiesInternal () {
704- Capabilities capabilities = Capabilities::NONE;
705-
706- sp<V1_3::IVibrator> hal = getHal ();
707- auto amplitudeResult = hal->supportsAmplitudeControl ();
708- if (!amplitudeResult.isOk ()) {
709- return HalResultFactory::fromReturn<Capabilities>(std::move (amplitudeResult), capabilities);
710- }
711-
712- auto externalControlResult = hal->supportsExternalControl ();
713- if (amplitudeResult.withDefault (false )) {
714- capabilities |= Capabilities::AMPLITUDE_CONTROL;
715- }
716- if (externalControlResult.withDefault (false )) {
717- capabilities |= Capabilities::EXTERNAL_CONTROL;
718-
719- if (amplitudeResult.withDefault (false )) {
720- capabilities |= Capabilities::EXTERNAL_AMPLITUDE_CONTROL;
721- }
722- }
723-
724- return HalResultFactory::fromReturn<Capabilities>(std::move (externalControlResult),
725- capabilities);
726- }
727-
728- // -------------------------------------------------------------------------------------------------
729-
730509}; // namespace vibrator
731510
732511}; // namespace android
0 commit comments