Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #ifndef METHOD_H_ |
| 18 | |
| 19 | #define METHOD_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 22 | #include <hidl-util/Formatter.h> |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <functional> |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 25 | #include <map> |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 26 | #include <set> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 28 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 29 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 30 | #include "Location.h" |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 31 | #include "Reference.h" |
| 32 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 35 | struct Annotation; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | struct Formatter; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 37 | struct ScalarType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 38 | struct Type; |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 39 | struct TypedVarVector; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 40 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 41 | enum MethodImplType { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 42 | IMPL_INTERFACE, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 43 | IMPL_PROXY, |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 44 | IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored |
| 45 | IMPL_STUB_IMPL, // use this->method() instead of mImpl->method() |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 46 | IMPL_PASSTHROUGH, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 50 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 51 | struct Method { |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 52 | Method(const char* name, std::vector<NamedReference<Type>*>* args, |
| 53 | std::vector<NamedReference<Type>*>* results, bool oneway, |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 54 | std::vector<Annotation*>* annotations, const Location& location); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 55 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 56 | std::string name() const; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 57 | const std::vector<NamedReference<Type>*>& args() const; |
| 58 | const std::vector<NamedReference<Type>*>& results() const; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 59 | bool isOneway() const { return mOneway; } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 60 | bool overridesCppImpl(MethodImplType type) const; |
| 61 | bool overridesJavaImpl(MethodImplType type) const; |
| 62 | void cppImpl(MethodImplType type, Formatter &out) const; |
| 63 | void javaImpl(MethodImplType type, Formatter &out) const; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 64 | bool isHidlReserved() const { return mIsHidlReserved; } |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 65 | bool isHiddenFromJava() const; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 66 | const std::vector<Annotation *> &annotations() const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 67 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 68 | std::vector<Reference<Type>> getReferences() const; |
| 69 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 70 | status_t evaluate(); |
| 71 | status_t validate() const; |
| 72 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 73 | // Make a copy with the same name, args, results, oneway, annotations. |
| 74 | // Implementations, serial are not copied. |
| 75 | Method *copySignature() const; |
| 76 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 77 | void setSerialId(size_t serial); |
| 78 | size_t getSerialId() const; |
| 79 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 80 | // Fill implementation for HIDL reserved methods. mIsHidlReserved will be |
| 81 | // set to true. |
| 82 | void fillImplementation( |
| 83 | size_t serial, |
| 84 | MethodImpl cppImpl, |
| 85 | MethodImpl javaImpl); |
| 86 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 87 | void generateCppReturnType(Formatter &out, bool specifyNamespaces = true) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 88 | void generateCppSignature(Formatter &out, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 89 | const std::string &className = "", |
| 90 | bool specifyNamespaces = true) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 91 | |
Steven Moreland | d8b10ee | 2017-07-31 15:06:20 -0700 | [diff] [blame] | 92 | bool hasEmptyCppArgSignature() const; |
| 93 | void emitCppArgSignature(Formatter &out, bool specifyNamespaces = true) const; |
| 94 | void emitCppResultSignature(Formatter &out, bool specifyNamespaces = true) const; |
| 95 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 96 | void emitJavaArgSignature(Formatter &out) const; |
| 97 | void emitJavaResultSignature(Formatter &out) const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 98 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 99 | const NamedReference<Type>* canElideCallback() const; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 100 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 101 | void dumpAnnotations(Formatter &out) const; |
| 102 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 103 | bool isJavaCompatible() const; |
| 104 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 105 | const Location& location() const; |
| 106 | |
| 107 | private: |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 108 | std::string mName; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 109 | size_t mSerial = 0; |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 110 | std::vector<NamedReference<Type>*>* mArgs; |
| 111 | std::vector<NamedReference<Type>*>* mResults; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 112 | bool mOneway; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 113 | std::vector<Annotation *> *mAnnotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 114 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 115 | bool mIsHidlReserved = false; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 116 | // The following fields have no meaning if mIsHidlReserved is false. |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 117 | // hard-coded implementation for HIDL reserved methods. |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 118 | MethodImpl mCppImpl; |
| 119 | MethodImpl mJavaImpl; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 120 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 121 | const Location& mLocation; |
| 122 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(Method); |
| 124 | }; |
| 125 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 126 | struct TypedVarVector : public std::vector<NamedReference<Type>*> { |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 127 | TypedVarVector() = default; |
| 128 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 129 | bool add(NamedReference<Type>* v); |
| 130 | |
| 131 | private: |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 132 | std::set<std::string> mNames; |
| 133 | }; |
| 134 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 135 | } // namespace android |
| 136 | |
| 137 | #endif // METHOD_H_ |
| 138 | |