| Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project * | 
|  | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | * you may not use this file except in compliance with the License. | 
|  | 5 | * You may obtain a copy of the License at | 
|  | 6 | * | 
|  | 7 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | * | 
|  | 9 | * Unless required by applicable law or agreed to in writing, software | 
|  | 10 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | * See the License for the specific language governing permissions and | 
|  | 13 | * limitations under the License. | 
|  | 14 | */ | 
|  | 15 |  | 
|  | 16 | #include "utils.h" | 
|  | 17 | #include "vibrator.h" | 
|  | 18 |  | 
|  | 19 | namespace android { | 
|  | 20 | namespace idlcli { | 
|  | 21 |  | 
|  | 22 | class CommandVibrator; | 
|  | 23 |  | 
|  | 24 | namespace vibrator { | 
|  | 25 |  | 
|  | 26 | class CommandSetAmplitude : public Command { | 
|  | 27 | std::string getDescription() const override { return "Set vibration amplitude."; } | 
|  | 28 |  | 
|  | 29 | std::string getUsageSummary() const override { return "<amplitude>"; } | 
|  | 30 |  | 
|  | 31 | UsageDetails getUsageDetails() const override { | 
|  | 32 | UsageDetails details{ | 
|  | 33 | {"<amplitude>", {"1-255."}}, | 
|  | 34 | }; | 
|  | 35 | return details; | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | Status doArgs(Args &args) override { | 
|  | 39 | if (auto amplitude = args.pop<decltype(mAmplitude)>()) { | 
|  | 40 | mAmplitude = *amplitude; | 
|  | 41 | } else { | 
|  | 42 | std::cerr << "Missing or Invalid Amplitude!" << std::endl; | 
|  | 43 | return USAGE; | 
|  | 44 | } | 
|  | 45 | if (!args.empty()) { | 
|  | 46 | std::cerr << "Unexpected Arguments!" << std::endl; | 
|  | 47 | return USAGE; | 
|  | 48 | } | 
|  | 49 | return OK; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | Status doMain(Args && /*args*/) override { | 
| Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 53 | std::string statusStr; | 
|  | 54 | Status ret; | 
| Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 55 |  | 
| Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 56 | if (auto hal = getHal<aidl::IVibrator>()) { | 
| Harpreet \"Eli\" Sangha | 3a8400f | 2019-10-24 09:20:02 +0900 | [diff] [blame] | 57 | auto status = hal->call(&aidl::IVibrator::setAmplitude, | 
|  | 58 | static_cast<float>(mAmplitude) / UINT8_MAX); | 
| Harpreet \"Eli\" Sangha | 6ce6f73 | 2019-11-28 16:53:41 +0900 | [diff] [blame] | 59 | statusStr = status.getDescription(); | 
| Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 60 | ret = status.isOk() ? OK : ERROR; | 
|  | 61 | } else if (auto hal = getHal<V1_0::IVibrator>()) { | 
|  | 62 | auto status = hal->call(&V1_0::IVibrator::setAmplitude, mAmplitude); | 
|  | 63 | statusStr = toString(status); | 
|  | 64 | ret = status.isOk() && status == V1_0::Status::OK ? OK : ERROR; | 
|  | 65 | } else { | 
| Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 66 | return UNAVAILABLE; | 
|  | 67 | } | 
|  | 68 |  | 
| Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 69 | std::cout << "Status: " << statusStr << std::endl; | 
| Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 70 |  | 
| Harpreet \"Eli\" Sangha | cb350b8 | 2019-11-12 15:15:25 +0900 | [diff] [blame] | 71 | return ret; | 
| Harpreet "Eli" Sangha | e47ae68 | 2019-06-05 16:52:47 +0900 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | uint8_t mAmplitude; | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | static const auto Command = | 
|  | 78 | CommandRegistry<CommandVibrator>::Register<CommandSetAmplitude>("setAmplitude"); | 
|  | 79 |  | 
|  | 80 | } // namespace vibrator | 
|  | 81 | } // namespace idlcli | 
|  | 82 | } // namespace android |