1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "hwservicemanager"
18 //#define LOG_NDEBUG 0
19 
20 #include "Vintf.h"
21 
22 #include <android-base/logging.h>
23 #include <hidl-util/FQName.h>
24 #include <vintf/VintfObject.h>
25 #include <vintf/parse_string.h>
26 
27 namespace android {
28 namespace hardware {
29 
getTransportFromManifest(const FQName & fqName,const std::string & instanceName,const std::shared_ptr<const vintf::HalManifest> & vm)30 vintf::Transport getTransportFromManifest(
31         const FQName &fqName, const std::string &instanceName,
32         const std::shared_ptr<const vintf::HalManifest>& vm) {
33     if (vm == nullptr) {
34         return vintf::Transport::EMPTY;
35     }
36     return vm->getHidlTransport(fqName.package(), fqName.getVersion(),
37                                 fqName.name(), instanceName);
38 }
39 
getTransport(const std::string & interfaceName,const std::string & instanceName)40 vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) {
41     FQName fqName;
42 
43     if (!FQName::parse(interfaceName, &fqName)) {
44         LOG(ERROR) << __FUNCTION__ << ": " << interfaceName
45                    << " is not a valid fully-qualified name.";
46         return vintf::Transport::EMPTY;
47     }
48     if (!fqName.hasVersion()) {
49         LOG(ERROR) << __FUNCTION__ << ": " << fqName.string()
50                    << " does not specify a version.";
51         return vintf::Transport::EMPTY;
52     }
53     if (fqName.name().empty()) {
54         LOG(ERROR) << __FUNCTION__ << ": " << fqName.string()
55                    << " does not specify an interface name.";
56         return vintf::Transport::EMPTY;
57     }
58 
59     vintf::Transport tr = getTransportFromManifest(fqName, instanceName,
60             vintf::VintfObject::GetFrameworkHalManifest());
61     if (tr != vintf::Transport::EMPTY) {
62         return tr;
63     }
64     tr = getTransportFromManifest(fqName, instanceName,
65             vintf::VintfObject::GetDeviceHalManifest());
66     if (tr != vintf::Transport::EMPTY) {
67         return tr;
68     }
69 
70     LOG(INFO) << __FUNCTION__ << ": Cannot find entry " << fqName.string() << "/" << instanceName
71               << " in either framework or device VINTF manifest.";
72     return vintf::Transport::EMPTY;
73 }
74 
insertManifestInstances(const FQName & fqName,const std::shared_ptr<const vintf::HalManifest> & manifest,const std::string & manifestType,std::set<std::string> * toSet)75 static void insertManifestInstances(const FQName& fqName,
76                                     const std::shared_ptr<const vintf::HalManifest>& manifest,
77                                     const std::string& manifestType,
78                                     std::set<std::string>* toSet) {
79     if (manifest == nullptr) {
80         LOG(ERROR) << "Device is missing " << manifestType << " manifest.";
81         return;
82     }
83 
84     std::set<std::string> manifestSet = manifest->getHidlInstances(
85         fqName.package(), fqName.getVersion(), fqName.name());
86 
87     toSet->insert(manifestSet.begin(), manifestSet.end());
88 }
89 
getInstances(const std::string & interfaceName)90 std::set<std::string> getInstances(const std::string& interfaceName) {
91     FQName fqName;
92     if (!FQName::parse(interfaceName, &fqName) || !fqName.isFullyQualified() ||
93             fqName.isValidValueName() || !fqName.isInterfaceName()) {
94         LOG(ERROR) << __FUNCTION__ << ": " << interfaceName
95                    << " is not a valid fully-qualified name.";
96         return {};
97     }
98 
99     std::set<std::string> ret;
100 
101     insertManifestInstances(
102         fqName, vintf::VintfObject::GetDeviceHalManifest(), "device", &ret);
103     insertManifestInstances(
104         fqName, vintf::VintfObject::GetFrameworkHalManifest(), "framework", &ret);
105 
106     return ret;
107 }
108 
109 
110 }  // hardware
111 }  // android
112