Hao Chen | 5a97c38 | 2019-12-05 15:53:05 -0800 | [diff] [blame] | 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 <android-base/logging.h> |
| 18 | #include <cutils/properties.h> |
| 19 | #include <hidl/HidlTransportSupport.h> |
| 20 | |
| 21 | #include <vhal_v2_0/EmulatedVehicleConnector.h> |
| 22 | #include <vhal_v2_0/EmulatedVehicleHal.h> |
| 23 | #include <vhal_v2_0/VehicleHalManager.h> |
| 24 | #include <vhal_v2_0/virtualization/GrpcVehicleClient.h> |
| 25 | #include <vhal_v2_0/virtualization/Utils.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::hardware; |
| 29 | using namespace android::hardware::automotive::vehicle::V2_0; |
| 30 | |
| 31 | int main(int argc, char* argv[]) { |
| 32 | constexpr const char* VHAL_SERVER_CID_PROPERTY_KEY = "ro.vendor.vehiclehal.server.cid"; |
| 33 | constexpr const char* VHAL_SERVER_PORT_PROPERTY_KEY = "ro.vendor.vehiclehal.server.port"; |
| 34 | |
| 35 | auto property_get_uint = [](const char* key, unsigned int default_value) { |
| 36 | auto value = property_get_int64(key, default_value); |
| 37 | if (value < 0 || value > UINT_MAX) { |
| 38 | LOG(DEBUG) << key << ": " << value << " is out of bound, using default value '" |
| 39 | << default_value << "' instead"; |
| 40 | return default_value; |
| 41 | } |
| 42 | return static_cast<unsigned int>(value); |
| 43 | }; |
| 44 | |
| 45 | impl::VsockServerInfo serverInfo{property_get_uint(VHAL_SERVER_CID_PROPERTY_KEY, 0), |
| 46 | property_get_uint(VHAL_SERVER_PORT_PROPERTY_KEY, 0)}; |
| 47 | |
| 48 | if (serverInfo.serverCid == 0 || serverInfo.serverPort == 0) { |
| 49 | LOG(FATAL) << "Invalid server information, CID: " << serverInfo.serverCid |
| 50 | << "; port: " << serverInfo.serverPort; |
| 51 | // Will abort after logging |
| 52 | } |
| 53 | |
| 54 | auto store = std::make_unique<VehiclePropertyStore>(); |
| 55 | auto connector = impl::makeGrpcVehicleClient(impl::getVsockUri(serverInfo)); |
| 56 | auto hal = std::make_unique<impl::EmulatedVehicleHal>(store.get(), connector.get()); |
| 57 | auto emulator = std::make_unique<impl::VehicleEmulator>(hal.get()); |
| 58 | auto service = std::make_unique<VehicleHalManager>(hal.get()); |
| 59 | |
| 60 | configureRpcThreadpool(4, true /* callerWillJoin */); |
| 61 | |
| 62 | LOG(INFO) << "Registering as service..."; |
| 63 | status_t status = service->registerAsService(); |
| 64 | |
| 65 | if (status != OK) { |
| 66 | LOG(ERROR) << "Unable to register vehicle service (" << status << ")"; |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | LOG(INFO) << "Ready"; |
| 71 | joinRpcThreadpool(); |
| 72 | |
| 73 | return 1; |
| 74 | } |