• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include <aidl/test_package/BnTest.h>
18 
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <condition_variable>
22 #include <mutex>
23 
24 #include "utilities.h"
25 
26 using IEmpty = ::aidl::test_package::IEmpty;
27 using RegularPolygon = ::aidl::test_package::RegularPolygon;
28 using Foo = ::aidl::test_package::Foo;
29 using Bar = ::aidl::test_package::Bar;
30 
31 class MyTest : public ::aidl::test_package::BnTest,
32                public ThisShouldBeDestroyed {
33  public:
dump(int fd,const char ** args,uint32_t numArgs)34   binder_status_t dump(int fd, const char** args, uint32_t numArgs) override {
35     for (uint32_t i = 0; i < numArgs; i++) {
36       dprintf(fd, "%s", args[i]);
37     }
38     fsync(fd);
39     return STATUS_OK;
40   }
41 
GetName(std::string * _aidl_return)42   ::ndk::ScopedAStatus GetName(std::string* _aidl_return) override {
43     *_aidl_return = "CPP";
44     return ::ndk::ScopedAStatus(AStatus_newOk());
45   }
TestVoidReturn()46   ::ndk::ScopedAStatus TestVoidReturn() override {
47     return ::ndk::ScopedAStatus(AStatus_newOk());
48   }
TestOneway()49   ::ndk::ScopedAStatus TestOneway() override {
50     // This return code should be ignored since it is oneway.
51     return ::ndk::ScopedAStatus(AStatus_fromStatus(STATUS_UNKNOWN_ERROR));
52   }
53 
GiveMeMyCallingPid(int32_t * _aidl_return)54   ::ndk::ScopedAStatus GiveMeMyCallingPid(int32_t* _aidl_return) override {
55     *_aidl_return = AIBinder_getCallingPid();
56     return ::ndk::ScopedAStatus(AStatus_newOk());
57   }
GiveMeMyCallingUid(int32_t * _aidl_return)58   ::ndk::ScopedAStatus GiveMeMyCallingUid(int32_t* _aidl_return) override {
59     *_aidl_return = AIBinder_getCallingUid();
60     return ::ndk::ScopedAStatus(AStatus_newOk());
61   }
62 
63  private:
64   bool mCached = false;
65   std::mutex mCachedMutex;
66   std::condition_variable mCachedCondition;
67   int mCachedPid = -1;
68   int mCachedUid = -1;
69 
70  public:
CacheCallingInfoFromOneway()71   ::ndk::ScopedAStatus CacheCallingInfoFromOneway() override {
72     std::unique_lock<std::mutex> l(mCachedMutex);
73     mCached = true;
74     mCachedPid = AIBinder_getCallingPid();
75     mCachedUid = AIBinder_getCallingUid();
76     mCachedCondition.notify_all();
77 
78     return ::ndk::ScopedAStatus(AStatus_newOk());
79   }
GiveMeMyCallingPidFromOneway(int32_t * _aidl_return)80   ::ndk::ScopedAStatus GiveMeMyCallingPidFromOneway(
81       int32_t* _aidl_return) override {
82     std::unique_lock<std::mutex> l(mCachedMutex);
83     mCachedCondition.wait(l, [&] { return mCached; });
84 
85     *_aidl_return = mCachedPid;
86     return ::ndk::ScopedAStatus(AStatus_newOk());
87   }
GiveMeMyCallingUidFromOneway(int32_t * _aidl_return)88   ::ndk::ScopedAStatus GiveMeMyCallingUidFromOneway(
89       int32_t* _aidl_return) override {
90     std::unique_lock<std::mutex> l(mCachedMutex);
91     mCachedCondition.wait(l, [&] { return mCached; });
92 
93     *_aidl_return = mCachedUid;
94     return ::ndk::ScopedAStatus(AStatus_newOk());
95   }
RepeatInt(int32_t in_value,int32_t * _aidl_return)96   ::ndk::ScopedAStatus RepeatInt(int32_t in_value,
97                                  int32_t* _aidl_return) override {
98     *_aidl_return = in_value;
99     return ::ndk::ScopedAStatus(AStatus_newOk());
100   }
RepeatLong(int64_t in_value,int64_t * _aidl_return)101   ::ndk::ScopedAStatus RepeatLong(int64_t in_value,
102                                   int64_t* _aidl_return) override {
103     *_aidl_return = in_value;
104     return ::ndk::ScopedAStatus(AStatus_newOk());
105   }
RepeatFloat(float in_value,float * _aidl_return)106   ::ndk::ScopedAStatus RepeatFloat(float in_value,
107                                    float* _aidl_return) override {
108     *_aidl_return = in_value;
109     return ::ndk::ScopedAStatus(AStatus_newOk());
110   }
RepeatDouble(double in_value,double * _aidl_return)111   ::ndk::ScopedAStatus RepeatDouble(double in_value,
112                                     double* _aidl_return) override {
113     *_aidl_return = in_value;
114     return ::ndk::ScopedAStatus(AStatus_newOk());
115   }
RepeatBoolean(bool in_value,bool * _aidl_return)116   ::ndk::ScopedAStatus RepeatBoolean(bool in_value,
117                                      bool* _aidl_return) override {
118     *_aidl_return = in_value;
119     return ::ndk::ScopedAStatus(AStatus_newOk());
120   }
RepeatChar(char16_t in_value,char16_t * _aidl_return)121   ::ndk::ScopedAStatus RepeatChar(char16_t in_value,
122                                   char16_t* _aidl_return) override {
123     *_aidl_return = in_value;
124     return ::ndk::ScopedAStatus(AStatus_newOk());
125   }
RepeatByte(int8_t in_value,int8_t * _aidl_return)126   ::ndk::ScopedAStatus RepeatByte(int8_t in_value,
127                                   int8_t* _aidl_return) override {
128     *_aidl_return = in_value;
129     return ::ndk::ScopedAStatus(AStatus_newOk());
130   }
RepeatBinder(const::ndk::SpAIBinder & in_value,::ndk::SpAIBinder * _aidl_return)131   ::ndk::ScopedAStatus RepeatBinder(const ::ndk::SpAIBinder& in_value,
132                                     ::ndk::SpAIBinder* _aidl_return) override {
133     *_aidl_return = in_value;
134     return ::ndk::ScopedAStatus(AStatus_newOk());
135   }
RepeatNullableBinder(const::ndk::SpAIBinder & in_value,::ndk::SpAIBinder * _aidl_return)136   ::ndk::ScopedAStatus RepeatNullableBinder(
137       const ::ndk::SpAIBinder& in_value,
138       ::ndk::SpAIBinder* _aidl_return) override {
139     *_aidl_return = in_value;
140     return ::ndk::ScopedAStatus(AStatus_newOk());
141   }
RepeatInterface(const std::shared_ptr<IEmpty> & in_value,std::shared_ptr<IEmpty> * _aidl_return)142   ::ndk::ScopedAStatus RepeatInterface(
143       const std::shared_ptr<IEmpty>& in_value,
144       std::shared_ptr<IEmpty>* _aidl_return) override {
145     *_aidl_return = in_value;
146     return ::ndk::ScopedAStatus(AStatus_newOk());
147   }
RepeatNullableInterface(const std::shared_ptr<IEmpty> & in_value,std::shared_ptr<IEmpty> * _aidl_return)148   ::ndk::ScopedAStatus RepeatNullableInterface(
149       const std::shared_ptr<IEmpty>& in_value,
150       std::shared_ptr<IEmpty>* _aidl_return) override {
151     *_aidl_return = in_value;
152     return ::ndk::ScopedAStatus(AStatus_newOk());
153   }
RepeatFd(const::ndk::ScopedFileDescriptor & in_value,::ndk::ScopedFileDescriptor * _aidl_return)154   ::ndk::ScopedAStatus RepeatFd(
155       const ::ndk::ScopedFileDescriptor& in_value,
156       ::ndk::ScopedFileDescriptor* _aidl_return) override {
157     _aidl_return->set(dup(in_value.get()));
158     return ::ndk::ScopedAStatus(AStatus_newOk());
159   }
RepeatNullableFd(const::ndk::ScopedFileDescriptor & in_value,::ndk::ScopedFileDescriptor * _aidl_return)160   ::ndk::ScopedAStatus RepeatNullableFd(
161       const ::ndk::ScopedFileDescriptor& in_value,
162       ::ndk::ScopedFileDescriptor* _aidl_return) override {
163     _aidl_return->set(dup(in_value.get()));
164     return ::ndk::ScopedAStatus(AStatus_newOk());
165   }
RepeatString(const std::string & in_value,std::string * _aidl_return)166   ::ndk::ScopedAStatus RepeatString(const std::string& in_value,
167                                     std::string* _aidl_return) override {
168     *_aidl_return = in_value;
169     return ::ndk::ScopedAStatus(AStatus_newOk());
170   }
RepeatNullableString(const std::optional<std::string> & in_value,std::optional<std::string> * _aidl_return)171   ::ndk::ScopedAStatus RepeatNullableString(
172       const std::optional<std::string>& in_value,
173       std::optional<std::string>* _aidl_return) override {
174     *_aidl_return = in_value;
175     return ::ndk::ScopedAStatus(AStatus_newOk());
176   }
RepeatPolygon(const RegularPolygon & in_value,RegularPolygon * _aidl_return)177   ::ndk::ScopedAStatus RepeatPolygon(const RegularPolygon& in_value,
178                                      RegularPolygon* _aidl_return) override {
179     *_aidl_return = in_value;
180     return ::ndk::ScopedAStatus(AStatus_newOk());
181   }
RenamePolygon(RegularPolygon * value,const std::string & newName)182   ::ndk::ScopedAStatus RenamePolygon(RegularPolygon* value,
183                                      const std::string& newName) override {
184     value->name = newName;
185     return ::ndk::ScopedAStatus(AStatus_newOk());
186   }
RepeatBooleanArray(const std::vector<bool> & in_value,std::vector<bool> * out_repeated,std::vector<bool> * _aidl_return)187   ::ndk::ScopedAStatus RepeatBooleanArray(
188       const std::vector<bool>& in_value, std::vector<bool>* out_repeated,
189       std::vector<bool>* _aidl_return) override {
190     *out_repeated = in_value;
191     *_aidl_return = in_value;
192     return ::ndk::ScopedAStatus(AStatus_newOk());
193   }
RepeatByteArray(const std::vector<int8_t> & in_value,std::vector<int8_t> * out_repeated,std::vector<int8_t> * _aidl_return)194   ::ndk::ScopedAStatus RepeatByteArray(
195       const std::vector<int8_t>& in_value, std::vector<int8_t>* out_repeated,
196       std::vector<int8_t>* _aidl_return) override {
197     *out_repeated = in_value;
198     *_aidl_return = in_value;
199     return ::ndk::ScopedAStatus(AStatus_newOk());
200   }
RepeatCharArray(const std::vector<char16_t> & in_value,std::vector<char16_t> * out_repeated,std::vector<char16_t> * _aidl_return)201   ::ndk::ScopedAStatus RepeatCharArray(
202       const std::vector<char16_t>& in_value,
203       std::vector<char16_t>* out_repeated,
204       std::vector<char16_t>* _aidl_return) override {
205     *out_repeated = in_value;
206     *_aidl_return = in_value;
207     return ::ndk::ScopedAStatus(AStatus_newOk());
208   }
RepeatIntArray(const std::vector<int32_t> & in_value,std::vector<int32_t> * out_repeated,std::vector<int32_t> * _aidl_return)209   ::ndk::ScopedAStatus RepeatIntArray(
210       const std::vector<int32_t>& in_value, std::vector<int32_t>* out_repeated,
211       std::vector<int32_t>* _aidl_return) override {
212     *out_repeated = in_value;
213     *_aidl_return = in_value;
214     return ::ndk::ScopedAStatus(AStatus_newOk());
215   }
RepeatLongArray(const std::vector<int64_t> & in_value,std::vector<int64_t> * out_repeated,std::vector<int64_t> * _aidl_return)216   ::ndk::ScopedAStatus RepeatLongArray(
217       const std::vector<int64_t>& in_value, std::vector<int64_t>* out_repeated,
218       std::vector<int64_t>* _aidl_return) override {
219     *out_repeated = in_value;
220     *_aidl_return = in_value;
221     return ::ndk::ScopedAStatus(AStatus_newOk());
222   }
RepeatFloatArray(const std::vector<float> & in_value,std::vector<float> * out_repeated,std::vector<float> * _aidl_return)223   ::ndk::ScopedAStatus RepeatFloatArray(
224       const std::vector<float>& in_value, std::vector<float>* out_repeated,
225       std::vector<float>* _aidl_return) override {
226     *out_repeated = in_value;
227     *_aidl_return = in_value;
228     return ::ndk::ScopedAStatus(AStatus_newOk());
229   }
RepeatDoubleArray(const std::vector<double> & in_value,std::vector<double> * out_repeated,std::vector<double> * _aidl_return)230   ::ndk::ScopedAStatus RepeatDoubleArray(
231       const std::vector<double>& in_value, std::vector<double>* out_repeated,
232       std::vector<double>* _aidl_return) override {
233     *out_repeated = in_value;
234     *_aidl_return = in_value;
235     return ::ndk::ScopedAStatus(AStatus_newOk());
236   }
RepeatStringArray(const std::vector<std::string> & in_value,std::vector<std::string> * out_repeated,std::vector<std::string> * _aidl_return)237   ::ndk::ScopedAStatus RepeatStringArray(
238       const std::vector<std::string>& in_value,
239       std::vector<std::string>* out_repeated,
240       std::vector<std::string>* _aidl_return) override {
241     *out_repeated = in_value;
242     *_aidl_return = in_value;
243     return ::ndk::ScopedAStatus(AStatus_newOk());
244   }
RepeatRegularPolygonArray(const std::vector<RegularPolygon> & in_value,std::vector<RegularPolygon> * out_repeated,std::vector<RegularPolygon> * _aidl_return)245   ::ndk::ScopedAStatus RepeatRegularPolygonArray(
246       const std::vector<RegularPolygon>& in_value, std::vector<RegularPolygon>* out_repeated,
247       std::vector<RegularPolygon>* _aidl_return) override {
248     *out_repeated = in_value;
249     *_aidl_return = in_value;
250     return ::ndk::ScopedAStatus(AStatus_newOk());
251   }
RepeatNullableBooleanArray(const std::optional<std::vector<bool>> & in_value,std::optional<std::vector<bool>> * _aidl_return)252   ::ndk::ScopedAStatus RepeatNullableBooleanArray(
253       const std::optional<std::vector<bool>>& in_value,
254       std::optional<std::vector<bool>>* _aidl_return) override {
255     *_aidl_return = in_value;
256     return ::ndk::ScopedAStatus(AStatus_newOk());
257   }
RepeatNullableByteArray(const std::optional<std::vector<int8_t>> & in_value,std::optional<std::vector<int8_t>> * _aidl_return)258   ::ndk::ScopedAStatus RepeatNullableByteArray(
259       const std::optional<std::vector<int8_t>>& in_value,
260       std::optional<std::vector<int8_t>>* _aidl_return) override {
261     *_aidl_return = in_value;
262     return ::ndk::ScopedAStatus(AStatus_newOk());
263   }
RepeatNullableCharArray(const std::optional<std::vector<char16_t>> & in_value,std::optional<std::vector<char16_t>> * _aidl_return)264   ::ndk::ScopedAStatus RepeatNullableCharArray(
265       const std::optional<std::vector<char16_t>>& in_value,
266       std::optional<std::vector<char16_t>>* _aidl_return) override {
267     *_aidl_return = in_value;
268     return ::ndk::ScopedAStatus(AStatus_newOk());
269   }
RepeatNullableIntArray(const std::optional<std::vector<int32_t>> & in_value,std::optional<std::vector<int32_t>> * _aidl_return)270   ::ndk::ScopedAStatus RepeatNullableIntArray(
271       const std::optional<std::vector<int32_t>>& in_value,
272       std::optional<std::vector<int32_t>>* _aidl_return) override {
273     *_aidl_return = in_value;
274     return ::ndk::ScopedAStatus(AStatus_newOk());
275   }
RepeatNullableLongArray(const std::optional<std::vector<int64_t>> & in_value,std::optional<std::vector<int64_t>> * _aidl_return)276   ::ndk::ScopedAStatus RepeatNullableLongArray(
277       const std::optional<std::vector<int64_t>>& in_value,
278       std::optional<std::vector<int64_t>>* _aidl_return) override {
279     *_aidl_return = in_value;
280     return ::ndk::ScopedAStatus(AStatus_newOk());
281   }
RepeatNullableFloatArray(const std::optional<std::vector<float>> & in_value,std::optional<std::vector<float>> * _aidl_return)282   ::ndk::ScopedAStatus RepeatNullableFloatArray(
283       const std::optional<std::vector<float>>& in_value,
284       std::optional<std::vector<float>>* _aidl_return) override {
285     *_aidl_return = in_value;
286     return ::ndk::ScopedAStatus(AStatus_newOk());
287   }
RepeatNullableDoubleArray(const std::optional<std::vector<double>> & in_value,std::optional<std::vector<double>> * _aidl_return)288   ::ndk::ScopedAStatus RepeatNullableDoubleArray(
289       const std::optional<std::vector<double>>& in_value,
290       std::optional<std::vector<double>>* _aidl_return) override {
291     *_aidl_return = in_value;
292     return ::ndk::ScopedAStatus(AStatus_newOk());
293   }
RepeatNullableStringArray(const std::optional<std::vector<std::optional<std::string>>> & in_value,std::optional<std::vector<std::optional<std::string>>> * _aidl_return)294   ::ndk::ScopedAStatus RepeatNullableStringArray(
295       const std::optional<std::vector<std::optional<std::string>>>& in_value,
296       std::optional<std::vector<std::optional<std::string>>>* _aidl_return) {
297     *_aidl_return = in_value;
298     return ::ndk::ScopedAStatus(AStatus_newOk());
299   }
DoubleRepeatNullableStringArray(const std::optional<std::vector<std::optional<std::string>>> & in_value,std::optional<std::vector<std::optional<std::string>>> * out_repeated,std::optional<std::vector<std::optional<std::string>>> * _aidl_return)300   ::ndk::ScopedAStatus DoubleRepeatNullableStringArray(
301       const std::optional<std::vector<std::optional<std::string>>>& in_value,
302       std::optional<std::vector<std::optional<std::string>>>* out_repeated,
303       std::optional<std::vector<std::optional<std::string>>>* _aidl_return)
304       override {
305     *out_repeated = in_value;
306     *_aidl_return = in_value;
307     return ::ndk::ScopedAStatus(AStatus_newOk());
308   }
309 #ifndef USING_VERSION_1
310   // All methods added from now on should be within this macro
NewMethodThatReturns10(int32_t * _aidl_return)311   ::ndk::ScopedAStatus NewMethodThatReturns10(int32_t* _aidl_return) override {
312     *_aidl_return = 10;
313     return ::ndk::ScopedAStatus(AStatus_newOk());
314   }
315 #endif
316 
repeatFoo(const Foo & in_inFoo,Foo * _aidl_return)317   ::ndk::ScopedAStatus repeatFoo(const Foo& in_inFoo, Foo* _aidl_return) {
318     *_aidl_return = in_inFoo;
319     return ::ndk::ScopedAStatus(AStatus_newOk());
320   }
321 
renameFoo(Foo * in_foo,const std::string & in_name)322   ::ndk::ScopedAStatus renameFoo(Foo* in_foo, const std::string& in_name) {
323     in_foo->a = in_name;
324     return ::ndk::ScopedAStatus(AStatus_newOk());
325   }
326 
renameBar(Foo * in_foo,const std::string & in_name)327   ::ndk::ScopedAStatus renameBar(Foo* in_foo, const std::string& in_name) {
328     in_foo->d.a = in_name;
329     return ::ndk::ScopedAStatus(AStatus_newOk());
330   }
331 
getF(const Foo & foo,int32_t * _aidl_return)332   ::ndk::ScopedAStatus getF(const Foo& foo, int32_t* _aidl_return) {
333     *_aidl_return = foo.f;
334     return ::ndk::ScopedAStatus(AStatus_newOk());
335   }
336 };
337