1 /* 2 * Copyright (C) 2019 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 17 #include "device_config.h" 18 19 #include <chrono> 20 #include <thread> 21 22 #include <android-base/logging.h> 23 #include <cutils/properties.h> 24 25 #include "common/libs/fs/shared_fd_stream.h" 26 27 namespace cuttlefish { 28 29 namespace { 30 31 static constexpr int kRetries = 5; 32 static constexpr int kRetryDelaySeconds = 5; 33 GetRawFromServer(DeviceConfig * data)34bool GetRawFromServer(DeviceConfig* data) { 35 auto port_property = "ro.boot.cuttlefish_config_server_port"; 36 auto port = property_get_int64(port_property, -1); 37 if (port < 0) { 38 LOG(ERROR) << "Unable to get config server port from property: " << 39 port_property; 40 return false; 41 } 42 auto config_server = 43 SharedFD::VsockClient(2 /*host cid*/, 44 static_cast<unsigned int>(port), SOCK_STREAM); 45 if (!config_server->IsOpen()) { 46 LOG(ERROR) << "Unable to connect to config server: " 47 << config_server->StrError(); 48 return false; 49 } 50 51 SharedFDIstream stream(config_server); 52 if (!data->ParseFromIstream(&stream)) { 53 LOG(ERROR) << "Error reading from config server: " 54 << config_server->StrError(); 55 } 56 return true; 57 } 58 59 } // namespace 60 Get()61std::unique_ptr<DeviceConfigHelper> DeviceConfigHelper::Get() { 62 DeviceConfig device_config; 63 64 int attempts_remaining = 1 + kRetries; 65 while (attempts_remaining > 0) { 66 if (GetRawFromServer(&device_config)) { 67 return std::unique_ptr<DeviceConfigHelper>( 68 new DeviceConfigHelper(device_config)); 69 } 70 71 std::this_thread::sleep_for(std::chrono::seconds(kRetryDelaySeconds)); 72 73 --attempts_remaining; 74 } 75 return nullptr; 76 } 77 78 } // namespace cuttlefish 79