Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 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 | |
| 17 | #include "hidl-utils.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/binder_manager.h> |
| 21 | #include <android/binder_process.h> |
Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 22 | #include <libradiocompat/CallbackManager.h> |
Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 23 | #include <libradiocompat/RadioConfig.h> |
Tomasz Wasilczyk | d2e7459 | 2021-11-02 12:14:52 -0700 | [diff] [blame] | 24 | #include <libradiocompat/RadioData.h> |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 25 | #include <libradiocompat/RadioMessaging.h> |
Tomasz Wasilczyk | 535f30c | 2021-11-08 16:10:28 -0800 | [diff] [blame] | 26 | #include <libradiocompat/RadioModem.h> |
Tomasz Wasilczyk | 61213fe | 2021-11-03 15:15:48 -0700 | [diff] [blame] | 27 | #include <libradiocompat/RadioNetwork.h> |
Tomasz Wasilczyk | fb10e00 | 2021-10-28 13:22:47 -0700 | [diff] [blame] | 28 | #include <libradiocompat/RadioSim.h> |
Tomasz Wasilczyk | 9375f3a | 2021-11-05 10:53:55 -0700 | [diff] [blame] | 29 | #include <libradiocompat/RadioVoice.h> |
Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android::hardware::radio::service { |
| 32 | |
| 33 | using namespace std::string_literals; |
| 34 | |
| 35 | static std::vector<std::shared_ptr<ndk::ICInterface>> gPublishedHals; |
| 36 | |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 37 | template <typename T> |
Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 38 | static void publishRadioHal(std::shared_ptr<compat::DriverContext> ctx, sp<V1_5::IRadio> hidlHal, |
| 39 | std::shared_ptr<compat::CallbackManager> cm, const std::string& slot) { |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 40 | const auto instance = T::descriptor + "/"s + slot; |
| 41 | if (!AServiceManager_isDeclared(instance.c_str())) { |
| 42 | LOG(INFO) << instance << " is not declared in VINTF (this may be intentional)"; |
| 43 | return; |
| 44 | } |
| 45 | LOG(DEBUG) << "Publishing " << instance; |
| 46 | |
Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 47 | auto aidlHal = ndk::SharedRefBase::make<T>(ctx, hidlHal, cm); |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 48 | gPublishedHals.push_back(aidlHal); |
| 49 | const auto status = AServiceManager_addService(aidlHal->asBinder().get(), instance.c_str()); |
| 50 | CHECK_EQ(status, STATUS_OK); |
| 51 | } |
| 52 | |
| 53 | static void publishRadio(std::string slot) { |
| 54 | auto radioHidl = V1_5::IRadio::getService(slot); |
| 55 | CHECK(radioHidl) << "HIDL IRadio not present in VINTF"; |
| 56 | |
| 57 | hidl_utils::linkDeathToDeath(radioHidl); |
| 58 | |
Sarah Chin | ba42735 | 2021-12-09 00:33:37 -0800 | [diff] [blame] | 59 | auto context = std::make_shared<compat::DriverContext>(); |
Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 60 | auto callbackMgr = std::make_shared<compat::CallbackManager>(context, radioHidl); |
Sarah Chin | ba42735 | 2021-12-09 00:33:37 -0800 | [diff] [blame] | 61 | |
Tomasz Wasilczyk | 8579f1d | 2021-12-16 12:19:09 -0800 | [diff] [blame] | 62 | publishRadioHal<compat::RadioData>(context, radioHidl, callbackMgr, slot); |
| 63 | publishRadioHal<compat::RadioMessaging>(context, radioHidl, callbackMgr, slot); |
| 64 | publishRadioHal<compat::RadioModem>(context, radioHidl, callbackMgr, slot); |
| 65 | publishRadioHal<compat::RadioNetwork>(context, radioHidl, callbackMgr, slot); |
| 66 | publishRadioHal<compat::RadioSim>(context, radioHidl, callbackMgr, slot); |
| 67 | publishRadioHal<compat::RadioVoice>(context, radioHidl, callbackMgr, slot); |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 70 | static void publishRadioConfig() { |
| 71 | auto hidlHal = config::V1_1::IRadioConfig::getService(); |
| 72 | CHECK(hidlHal) << "HIDL IRadioConfig not present in VINTF"; |
| 73 | |
| 74 | hidl_utils::linkDeathToDeath(hidlHal); |
| 75 | |
| 76 | auto aidlHal = ndk::SharedRefBase::make<compat::RadioConfig>(hidlHal); |
| 77 | gPublishedHals.push_back(aidlHal); |
| 78 | const auto instance = compat::RadioConfig::descriptor + "/default"s; |
| 79 | const auto status = AServiceManager_addService(aidlHal->asBinder().get(), instance.c_str()); |
| 80 | CHECK_EQ(status, STATUS_OK); |
| 81 | } |
| 82 | |
| 83 | static void main() { |
Tomasz Wasilczyk | d1d52fa | 2021-12-28 20:30:54 -0800 | [diff] [blame] | 84 | base::InitLogging(nullptr, base::LogdLogger(base::RADIO)); |
Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 85 | base::SetDefaultTag("radiocompat"); |
| 86 | base::SetMinimumLogSeverity(base::VERBOSE); |
| 87 | LOG(DEBUG) << "Radio HAL compat service starting..."; |
| 88 | |
| 89 | publishRadioConfig(); |
| 90 | |
Tomasz Wasilczyk | 1f16d3a | 2021-10-25 20:20:49 -0700 | [diff] [blame] | 91 | const auto slots = hidl_utils::listManifestByInterface(V1_0::IRadio::descriptor); |
| 92 | LOG(INFO) << "Found " << slots.size() << " slot(s)"; |
| 93 | for (const auto& slot : slots) { |
| 94 | publishRadio(slot); |
| 95 | } |
| 96 | |
Tomasz Wasilczyk | 6301e8f | 2021-10-18 16:53:40 -0700 | [diff] [blame] | 97 | LOG(DEBUG) << "Radio HAL compat service is operational"; |
| 98 | ABinderProcess_joinThreadPool(); |
| 99 | LOG(FATAL) << "Radio HAL compat service has stopped"; |
| 100 | } |
| 101 | |
| 102 | } // namespace android::hardware::radio::service |
| 103 | |
| 104 | int main() { |
| 105 | android::hardware::radio::service::main(); |
| 106 | return EXIT_FAILURE; // should not reach |
| 107 | } |