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