blob: ddf5dfa19367273c1b3363ace00de4ee20c8adb1 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huber5345ec22016-07-29 13:33:27 -070017#ifndef COORDINATOR_H_
18
19#define COORDINATOR_H_
20
21#include <android-base/macros.h>
Andreas Hubere61e3f72016-08-03 10:22:03 -070022#include <functional>
Steven Morelandd537ab02016-09-12 10:32:01 -070023#include <map>
Andreas Huber39fa7182016-08-19 14:27:33 -070024#include <set>
Andreas Huber5345ec22016-07-29 13:33:27 -070025#include <string>
Steven Morelandd537ab02016-09-12 10:32:01 -070026#include <utils/Errors.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <vector>
Andreas Huber5345ec22016-07-29 13:33:27 -070028
29namespace android {
30
31struct AST;
32struct FQName;
Andreas Huberfd4afab2016-08-03 13:02:57 -070033struct Type;
Andreas Huber5345ec22016-07-29 13:33:27 -070034
35struct Coordinator {
Andreas Huberdca261f2016-08-04 13:47:51 -070036 Coordinator(
37 const std::vector<std::string> &packageRootPaths,
38 const std::vector<std::string> &packageRoots);
39
Andreas Huber5345ec22016-07-29 13:33:27 -070040 ~Coordinator();
41
Andreas Huber39fa7182016-08-19 14:27:33 -070042 // Attempts to parse the interface/types referred to by fqName.
43 // Parsing an interface also parses the associated package's types.hal
44 // file if it exists.
45 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
46 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070047 // If !enforce, enforceRestrictionsOnPackage won't be run.
48 AST *parse(const FQName &fqName, std::set<AST *> *parsedASTs = nullptr,
49 bool enforce = true);
Andreas Huber5345ec22016-07-29 13:33:27 -070050
Andreas Huberdca261f2016-08-04 13:47:51 -070051 // Given package-root paths of ["hardware/interfaces",
52 // "vendor/<something>/interfaces"], package roots of
53 // ["android.hardware", "vendor.<something>.hardware"], and a
54 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -080055 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
56 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Andreas Huberdca261f2016-08-04 13:47:51 -070057
Andreas Huber881227d2016-08-02 14:20:21 -070058 std::string getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -080059 const FQName &fqName, bool relative = false,
60 bool sanitized = false) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070061
Andreas Huberdca261f2016-08-04 13:47:51 -070062 // Given package roots of ["android.hardware",
63 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -070064 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -070065 // return "android.hardware".
66
67 std::string getPackageRoot(const FQName &fqName) const;
68
Iliyan Malchev5bb14022016-08-09 15:04:39 -070069 // Given package-root paths of ["hardware/interfaces",
70 // "vendor/<something>/interfaces"], package roots of
71 // ["android.hardware", "vendor.<something>.hardware"], and a
72 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
73 // will return "hardware/interfaces".
74
75 std::string getPackageRootPath(const FQName &fqName) const;
76
Yifan Hongc8934042016-11-17 17:10:52 -080077 // return getPackageRoot + ":" + getPackageRootPath
78 std::string getPackageRootOption(const FQName &fqName) const;
79
Andreas Huberd2943e12016-08-05 11:59:31 -070080 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
81 // "android/hardware/".
82 std::string convertPackageRootToPath(const FQName &fqName) const;
83
84 status_t getPackageInterfaceFiles(
85 const FQName &package,
86 std::vector<std::string> *fileNames) const;
87
Steven Morelandaa186832016-09-26 13:51:43 -070088 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -070089 const FQName &package,
90 std::vector<FQName> *packageInterfaces) const;
91
Yifan Hong78b38d12017-02-13 18:14:46 +000092 // Enforce a set of restrictions on a set of packages. These include:
93 // - minor version upgrades
94 // "packages" contains names like "android.hardware.nfc@1.1".
95 status_t enforceRestrictionsOnPackage(const FQName &fqName);
96
Andreas Huberd2943e12016-08-05 11:59:31 -070097 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -070098
Andreas Huber5345ec22016-07-29 13:33:27 -070099private:
Andreas Huberdca261f2016-08-04 13:47:51 -0700100 // A list of top-level directories (mPackageRootPaths)
101 // corresponding to a list of package roots (mPackageRoots). For
102 // example, if mPackageRootPaths[0] == "hardware/interfaces" and
103 // mPackageRoots[0] == "android.hardware" this means that all
104 // packages starting with "android.hardware" will be looked up in
105 // "hardware/interfaces".
106 std::vector<std::string> mPackageRootPaths;
107 std::vector<std::string> mPackageRoots;
Yifan Hong78b38d12017-02-13 18:14:46 +0000108
109 // cache to parse().
Steven Morelandd537ab02016-09-12 10:32:01 -0700110 std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700111
Yifan Hong78b38d12017-02-13 18:14:46 +0000112 // cache to enforceRestrictionsOnPackage().
113 std::set<FQName> mPackagesEnforced;
114
Andreas Huberdca261f2016-08-04 13:47:51 -0700115 std::vector<std::string>::const_iterator findPackageRoot(
116 const FQName &fqName) const;
117
Yifan Hong78b38d12017-02-13 18:14:46 +0000118 // Rules of enforceRestrictionsOnPackage are listed below.
119 status_t enforceMinorVersionUprevs(const FQName &fqName);
120
Andreas Huber5345ec22016-07-29 13:33:27 -0700121 DISALLOW_COPY_AND_ASSIGN(Coordinator);
122};
123
124} // namespace android
125
126#endif // COORDINATOR_H_