Caleb Connolly | c445fab | 2021-08-11 14:42:57 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H |
| 17 | #define ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H |
| 18 | |
| 19 | #include <android/hardware/vibrator/1.1/IVibrator.h> |
| 20 | #include <hidl/Status.h> |
| 21 | |
| 22 | #include <linux/input.h> |
| 23 | |
| 24 | #include <fstream> |
| 25 | |
| 26 | // Borrowed from linuxconsole/utils/bitmaskros.h |
| 27 | /* Number of bits for 1 unsigned char */ |
| 28 | #define nBitsPerUchar (sizeof(unsigned char) * 8) |
| 29 | /* Index=Offset of given bit in 1 unsigned char */ |
| 30 | #define bitOffsetInUchar(bit) ((bit)%nBitsPerUchar) |
| 31 | /* Index=Offset of the unsigned char associated to the bit |
| 32 | * at the given index=offset |
| 33 | */ |
| 34 | #define ucharIndexForBit(bit) ((bit)/nBitsPerUchar) |
| 35 | /* Test the bit with given index=offset in an unsigned char array */ |
| 36 | #define testBit(bit, array) ((array[ucharIndexForBit(bit)] >> bitOffsetInUchar(bit)) & 1) |
| 37 | |
| 38 | namespace android { |
| 39 | namespace hardware { |
| 40 | namespace vibrator { |
| 41 | namespace V1_1 { |
| 42 | namespace implementation { |
| 43 | |
| 44 | class Vibrator : public IVibrator { |
| 45 | public: |
| 46 | Vibrator(std::string mInputDevPath); |
| 47 | |
| 48 | // Methods from ::android::hardware::vibrator::V1_0::IVibrator follow. |
| 49 | using Status = ::android::hardware::vibrator::V1_0::Status; |
| 50 | Return<Status> on(uint32_t timeoutMs) override; |
| 51 | Return<Status> off() override; |
| 52 | Return<bool> supportsAmplitudeControl() override; |
| 53 | Return<Status> setAmplitude(uint8_t amplitude) override; |
| 54 | |
| 55 | using EffectStrength = ::android::hardware::vibrator::V1_0::EffectStrength; |
| 56 | Return<void> perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) |
| 57 | override; |
| 58 | Return<void> perform_1_1(Effect_1_1 effect, EffectStrength strength, perform_cb _hidl_cb) |
| 59 | override; |
| 60 | |
| 61 | private: |
| 62 | ~Vibrator(); |
| 63 | Return<Status> on(uint32_t timeoutMs, uint32_t playCount); |
| 64 | int openInputDev(); |
| 65 | template <typename T> |
| 66 | Return<void> performWrapper(T effect, EffectStrength strength, perform_cb _hidl_cb); |
| 67 | Return<void> performEffect(Effect_1_1 effect, EffectStrength strength, perform_cb _hidl_cb); |
| 68 | std::string mInputDevPath; |
| 69 | int mfd; |
| 70 | struct ff_effect mEffect; |
| 71 | |
| 72 | }; |
| 73 | |
| 74 | } // namespace implementation |
| 75 | } // namespace V1_1 |
| 76 | } // namespace vibrator |
| 77 | } // namespace hardware |
| 78 | } // namespace android |
| 79 | |
| 80 | class FileDescGuard { |
| 81 | public: |
| 82 | FileDescGuard(int fd) : m_fd(fd) {} |
| 83 | |
| 84 | ~FileDescGuard() { |
| 85 | if (close(m_fd) != 0) |
| 86 | { |
| 87 | ALOGE("CA:: Failed to close fd %d, errno = %d", m_fd, errno); |
| 88 | } |
| 89 | } |
| 90 | private: |
| 91 | int m_fd = -1; |
| 92 | }; |
| 93 | |
| 94 | #endif // ANDROID_HARDWARE_VIBRATOR_V1_1_VIBRATOR_H |