1 /* 2 * Copyright (C) 2021 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 #include "host/libs/config/adb/adb.h" 17 18 #include <android-base/logging.h> 19 #include <fruit/fruit.h> 20 #include <json/json.h> 21 22 #include "host/libs/config/config_fragment.h" 23 24 namespace cuttlefish { 25 namespace { 26 27 class AdbConfigFragmentImpl : public AdbConfigFragment { 28 public: INJECT(AdbConfigFragmentImpl (AdbConfig & config))29 INJECT(AdbConfigFragmentImpl(AdbConfig& config)) : config_(config) {} 30 Name() const31 std::string Name() const override { return "AdbConfigFragmentImpl"; } 32 Serialize() const33 Json::Value Serialize() const override { 34 Json::Value json; 35 json[kMode] = Json::Value(Json::arrayValue); 36 for (const auto& mode : config_.Modes()) { 37 json[kMode].append(AdbModeToString(mode)); 38 } 39 json[kConnectorEnabled] = config_.RunConnector(); 40 return json; 41 } Deserialize(const Json::Value & json)42 bool Deserialize(const Json::Value& json) override { 43 if (!json.isMember(kMode) || json[kMode].type() != Json::arrayValue) { 44 LOG(ERROR) << "Invalid value for " << kMode; 45 return false; 46 } 47 std::set<AdbMode> modes; 48 for (auto& mode : json[kMode]) { 49 if (mode.type() != Json::stringValue) { 50 LOG(ERROR) << "Invalid mode type" << mode; 51 return false; 52 } 53 modes.insert(StringToAdbMode(mode.asString())); 54 } 55 if (!config_.SetModes(std::move(modes))) { 56 LOG(ERROR) << "Failed to set adb modes"; 57 return false; 58 } 59 60 if (!json.isMember(kConnectorEnabled) || 61 json[kConnectorEnabled].type() != Json::booleanValue) { 62 LOG(ERROR) << "Invalid value for " << kConnectorEnabled; 63 return false; 64 } 65 if (!config_.SetRunConnector(json[kConnectorEnabled].asBool())) { 66 LOG(ERROR) << "Failed to set whether to run the adb connector"; 67 } 68 return true; 69 } 70 71 private: 72 static constexpr char kMode[] = "mode"; 73 static constexpr char kConnectorEnabled[] = "connector_enabled"; 74 AdbConfig& config_; 75 }; 76 77 } // namespace 78 79 fruit::Component<fruit::Required<AdbConfig>, AdbConfigFragment> AdbConfigFragmentComponent()80AdbConfigFragmentComponent() { 81 return fruit::createComponent() 82 .bind<AdbConfigFragment, AdbConfigFragmentImpl>() 83 .addMultibinding<ConfigFragment, AdbConfigFragment>(); 84 } 85 86 } // namespace cuttlefish 87