1 /*
2  * Copyright (C) 2020 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 "libcanhaltools/libcanhaltools.h"
18 
19 #include <android-base/logging.h>
20 #include <android/hardware/automotive/can/1.0/ICanController.h>
21 #include <android/hidl/manager/1.2/IServiceManager.h>
22 #include <hidl-utils/hidl-utils.h>
23 
24 #include <iostream>
25 #include <string>
26 
27 namespace android::hardware::automotive::can::libcanhaltools {
28 
29 using ICanBus = V1_0::ICanBus;
30 using ICanController = V1_0::ICanController;
31 using IfIdDisc = ICanController::BusConfig::InterfaceId::hidl_discriminator;
32 
getControlServices()33 hidl_vec<hidl_string> getControlServices() {
34     auto manager = hidl::manager::V1_2::IServiceManager::getService();
35     hidl_vec<hidl_string> services;
36     manager->listManifestByInterface(ICanController::descriptor, hidl_utils::fill(&services));
37     CHECK(services.size() > 0) << "No ICanController services registered (missing privileges?)"
38                                << std::endl;
39     return services;
40 }
41 
isSupported(sp<ICanController> ctrl,ICanController::InterfaceType iftype)42 bool isSupported(sp<ICanController> ctrl, ICanController::InterfaceType iftype) {
43     hidl_vec<ICanController::InterfaceType> supported;
44     if (!ctrl->getSupportedInterfaceTypes(hidl_utils::fill(&supported)).isOk()) return false;
45     return supported.contains(iftype);
46 }
47 
getIftype(ICanController::BusConfig can_config)48 ICanController::InterfaceType getIftype(ICanController::BusConfig can_config) {
49     switch (can_config.interfaceId.getDiscriminator()) {
50         case IfIdDisc::socketcan:
51             return ICanController::InterfaceType::SOCKETCAN;
52         case IfIdDisc::slcan:
53             return ICanController::InterfaceType::SLCAN;
54         case IfIdDisc::virtualif:
55             return ICanController::InterfaceType::VIRTUAL;
56         case IfIdDisc::indexed:
57             return ICanController::InterfaceType::INDEXED;
58         default:
59             CHECK(false) << "HAL returned unexpected interface type!";
60     }
61 }
62 
configureIface(ICanController::BusConfig can_config)63 ICanController::Result configureIface(ICanController::BusConfig can_config) {
64     auto iftype = getIftype(can_config);
65     auto can_controller_list = getControlServices();
66     for (auto const& service : can_controller_list) {
67         auto ctrl = ICanController::getService(service);
68         if (ctrl == nullptr) {
69             LOG(ERROR) << "Couldn't open ICanController/" << service;
70             continue;
71         }
72 
73         if (!libcanhaltools::isSupported(ctrl, iftype)) continue;
74 
75         const auto up_result = ctrl->upInterface(can_config);
76         if (up_result != ICanController::Result::OK) {
77             LOG(ERROR) << "Failed to bring " << can_config.name << " up: " << toString(up_result)
78                        << std::endl;
79         }
80         return up_result;
81     }
82     return ICanController::Result::NOT_SUPPORTED;
83 }
84 
85 }  // namespace android::hardware::automotive::can::libcanhaltools
86