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