1/*
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
17#ifndef INTERFACE_H_
18
19#define INTERFACE_H_
20
21#include <vector>
22
23#include <hidl-hash/Hash.h>
24
25#include "Reference.h"
26#include "Scope.h"
27
28namespace android {
29
30struct Method;
31struct InterfaceAndMethod;
32
33struct Interface : public Scope {
34    enum {
35        /////////////////// Flag(s) - DO NOT CHANGE
36        FLAG_ONEWAY = 0x00000001,
37    };
38
39    Interface(const char* localName, const FQName& fullName, const Location& location,
40              Scope* parent, const Reference<Type>& superType, const Hash* fileHash);
41
42    const Hash* getFileHash() const;
43
44    bool addMethod(Method *method);
45    bool addAllReservedMethods();
46
47    bool isElidableType() const override;
48    bool isInterface() const override;
49    bool isBinder() const override;
50    bool isIBase() const { return fqName() == gIBaseFqName; }
51    std::string typeName() const override;
52
53    const Interface* superType() const;
54
55    // Super type chain to root type.
56    // First element is superType().
57    std::vector<const Interface *> superTypeChain() const;
58    // Super type chain to root type, including myself.
59    // First element is this.
60    std::vector<const Interface *> typeChain() const;
61
62    // user defined methods (explicit definition in HAL files)
63    const std::vector<Method *> &userDefinedMethods() const;
64    // HIDL reserved methods (every interface has these implicitly defined)
65    const std::vector<Method *> &hidlReservedMethods() const;
66    // the sum of userDefinedMethods() and hidlReservedMethods().
67    std::vector<Method *> methods() const;
68
69    // userDefinedMethods() for all super type + methods()
70    // The order will be as follows (in the transaction code order):
71    // great-great-...-great-grand parent->userDefinedMethods()
72    // ...
73    // parent->userDefinedMethods()
74    // this->userDefinedMethods()
75    // this->hidlReservedMethods()
76    std::vector<InterfaceAndMethod> allMethodsFromRoot() const;
77
78    // allMethodsFromRoot for parent
79    std::vector<InterfaceAndMethod> allSuperMethodsFromRoot() const;
80
81    // aliases for corresponding methods in this->fqName()
82    std::string getBaseName() const;
83    std::string getAdapterName() const;
84    std::string getProxyName() const;
85    std::string getStubName() const;
86    std::string getPassthroughName() const;
87    std::string getHwName() const;
88    FQName getProxyFqName() const;
89    FQName getStubFqName() const;
90    FQName getPassthroughFqName() const;
91
92    std::string getCppType(
93            StorageMode mode,
94            bool specifyNamespaces) const override;
95
96    std::string getJavaType(bool forInitializer) const override;
97    std::string getVtsType() const override;
98
99    std::vector<const Reference<Type>*> getReferences() const override;
100    std::vector<const Reference<Type>*> getStrongReferences() const override;
101
102    std::vector<const ConstantExpression*> getConstantExpressions() const override;
103
104    status_t resolveInheritance() override;
105    status_t validate() const override;
106    status_t validateUniqueNames() const;
107    status_t validateAnnotations() const;
108
109    void emitReaderWriter(
110            Formatter &out,
111            const std::string &name,
112            const std::string &parcelObj,
113            bool parcelObjIsPointer,
114            bool isReader,
115            ErrorMode mode) const override;
116
117    void emitPackageTypeDeclarations(Formatter& out) const override;
118    void emitTypeDefinitions(Formatter& out, const std::string& prefix) const override;
119
120    void getAlignmentAndSize(size_t* align, size_t* size) const override;
121    void emitJavaReaderWriter(
122            Formatter &out,
123            const std::string &parcelObj,
124            const std::string &argName,
125            bool isReader) const override;
126
127    void emitVtsAttributeType(Formatter& out) const override;
128
129    void emitVtsAttributeDeclaration(Formatter& out) const;
130    void emitVtsMethodDeclaration(Formatter& out) const;
131
132    bool hasOnewayMethods() const;
133
134    bool deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const override;
135
136    bool isNeverStrongReference() const override;
137
138   private:
139    Reference<Type> mSuperType;
140
141    std::vector<Method*> mUserMethods;
142    std::vector<Method*> mReservedMethods;
143
144    const Hash* mFileHash;
145
146    bool fillPingMethod(Method* method) const;
147    bool fillDescriptorChainMethod(Method* method) const;
148    bool fillGetDescriptorMethod(Method* method) const;
149    bool fillHashChainMethod(Method* method) const;
150    bool fillSyspropsChangedMethod(Method* method) const;
151    bool fillLinkToDeathMethod(Method* method) const;
152    bool fillUnlinkToDeathMethod(Method* method) const;
153    bool fillSetHALInstrumentationMethod(Method* method) const;
154    bool fillGetDebugInfoMethod(Method* method) const;
155    bool fillDebugMethod(Method* method) const;
156
157    void emitDigestChain(
158        Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
159        std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const;
160
161    DISALLOW_COPY_AND_ASSIGN(Interface);
162};
163
164// An interface / method tuple.
165struct InterfaceAndMethod {
166    InterfaceAndMethod(const Interface *iface, Method *method)
167        : mInterface(iface),
168          mMethod(method) {}
169    Method *method() const { return mMethod; }
170    const Interface *interface() const { return mInterface; }
171
172   private:
173    // do not own these objects.
174    const Interface *mInterface;
175    Method *mMethod;
176};
177
178}  // namespace android
179
180#endif  // INTERFACE_H_
181
182