• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020, 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 //! Test Rust service for the AIDL compiler.
18 
19 use aidl_test_fixedsizearray::aidl::android::aidl::fixedsizearray::FixedSizeArrayExample::{
20     IRepeatFixedSizeArray, IntParcelable::IntParcelable,
21 };
22 use aidl_test_interface::aidl::android::aidl::tests::nested::{
23     INestedService, ParcelableWithNested,
24 };
25 use aidl_test_interface::aidl::android::aidl::tests::ITestService::{
26     self, BnTestService, BpTestService, Empty::Empty,
27 };
28 use aidl_test_interface::aidl::android::aidl::tests::{
29     extension::ExtendableParcelable::ExtendableParcelable, extension::MyExt::MyExt,
30     BackendType::BackendType, ByteEnum::ByteEnum, CircularParcelable::CircularParcelable,
31     ConstantExpressionEnum::ConstantExpressionEnum, ICircular, INamedCallback, INewName, IOldName,
32     IntEnum::IntEnum, LongEnum::LongEnum, RecursiveList::RecursiveList, StructuredParcelable,
33     Union,
34 };
35 use aidl_test_interface::binder::{
36     self, BinderFeatures, Interface, ParcelFileDescriptor, SpIBinder,
37 };
38 use aidl_test_versioned_interface::aidl::android::aidl::versioned::tests::{
39     BazUnion::BazUnion, Foo::Foo, IFooInterface, IFooInterface::BnFooInterface,
40     IFooInterface::BpFooInterface,
41 };
42 use std::collections::HashMap;
43 use std::sync::Mutex;
44 
dup_fd(fd: &ParcelFileDescriptor) -> ParcelFileDescriptor45 fn dup_fd(fd: &ParcelFileDescriptor) -> ParcelFileDescriptor {
46     ParcelFileDescriptor::new(fd.as_ref().try_clone().unwrap())
47 }
48 
49 struct NamedCallback(String);
50 
51 impl Interface for NamedCallback {}
52 
53 impl INamedCallback::INamedCallback for NamedCallback {
GetName(&self) -> binder::Result<String>54     fn GetName(&self) -> binder::Result<String> {
55         Ok(self.0.clone())
56     }
57 }
58 
59 struct OldName;
60 
61 impl Interface for OldName {}
62 
63 impl IOldName::IOldName for OldName {
RealName(&self) -> binder::Result<String>64     fn RealName(&self) -> binder::Result<String> {
65         Ok("OldName".into())
66     }
67 }
68 
69 #[derive(Debug, Default)]
70 struct NewName;
71 
72 impl Interface for NewName {}
73 
74 impl INewName::INewName for NewName {
RealName(&self) -> binder::Result<String>75     fn RealName(&self) -> binder::Result<String> {
76         Ok("NewName".into())
77     }
78 }
79 
80 #[derive(Debug, Default)]
81 struct Circular;
82 
83 impl Interface for Circular {}
84 
85 impl ICircular::ICircular for Circular {
GetTestService( &self, ) -> binder::Result<Option<binder::Strong<dyn ITestService::ITestService>>>86     fn GetTestService(
87         &self,
88     ) -> binder::Result<Option<binder::Strong<dyn ITestService::ITestService>>> {
89         Ok(None)
90     }
91 }
92 
93 #[derive(Default)]
94 struct TestService {
95     service_map: Mutex<HashMap<String, binder::Strong<dyn INamedCallback::INamedCallback>>>,
96 }
97 
98 impl Interface for TestService {}
99 
100 macro_rules! impl_repeat {
101     ($repeat_name:ident, $type:ty) => {
102         fn $repeat_name(&self, token: $type) -> binder::Result<$type> {
103             Ok(token)
104         }
105     };
106 }
107 
108 macro_rules! impl_reverse {
109     ($reverse_name:ident, $type:ty) => {
110         fn $reverse_name(
111             &self,
112             input: &[$type],
113             repeated: &mut Vec<$type>,
114         ) -> binder::Result<Vec<$type>> {
115             repeated.clear();
116             repeated.extend_from_slice(input);
117             Ok(input.iter().rev().cloned().collect())
118         }
119     };
120 }
121 
122 macro_rules! impl_repeat_reverse {
123     ($repeat_name:ident, $reverse_name:ident, $type:ty) => {
124         impl_repeat! {$repeat_name, $type}
125         impl_reverse! {$reverse_name, $type}
126     };
127 }
128 
129 macro_rules! impl_repeat_nullable {
130     ($repeat_nullable_name:ident, $type:ty) => {
131         fn $repeat_nullable_name(
132             &self,
133             input: Option<&[$type]>,
134         ) -> binder::Result<Option<Vec<$type>>> {
135             Ok(input.map(<[$type]>::to_vec))
136         }
137     };
138 }
139 
140 impl ITestService::ITestService for TestService {
141     impl_repeat! {RepeatByte, i8}
142     impl_reverse! {ReverseByte, u8}
143 
UnimplementedMethod(&self, _: i32) -> binder::Result<i32>144     fn UnimplementedMethod(&self, _: i32) -> binder::Result<i32> {
145         // Pretend this method hasn't been implemented
146         Err(binder::StatusCode::UNKNOWN_TRANSACTION.into())
147     }
148 
TestOneway(&self) -> binder::Result<()>149     fn TestOneway(&self) -> binder::Result<()> {
150         Err(binder::StatusCode::UNKNOWN_ERROR.into())
151     }
152 
Deprecated(&self) -> binder::Result<()>153     fn Deprecated(&self) -> binder::Result<()> {
154         Ok(())
155     }
156 
157     impl_repeat_reverse! {RepeatBoolean, ReverseBoolean, bool}
158     impl_repeat_reverse! {RepeatChar, ReverseChar, u16}
159     impl_repeat_reverse! {RepeatInt, ReverseInt, i32}
160     impl_repeat_reverse! {RepeatLong, ReverseLong, i64}
161     impl_repeat_reverse! {RepeatFloat, ReverseFloat, f32}
162     impl_repeat_reverse! {RepeatDouble, ReverseDouble, f64}
163     impl_repeat_reverse! {RepeatByteEnum, ReverseByteEnum, ByteEnum}
164     impl_repeat_reverse! {RepeatIntEnum, ReverseIntEnum, IntEnum}
165     impl_repeat_reverse! {RepeatLongEnum, ReverseLongEnum, LongEnum}
166     impl_reverse! {ReverseString, String}
167     impl_reverse! {ReverseStringList, String}
168     impl_reverse! {ReverseUtf8CppString, String}
169 
RepeatString(&self, input: &str) -> binder::Result<String>170     fn RepeatString(&self, input: &str) -> binder::Result<String> {
171         Ok(input.into())
172     }
173 
RepeatUtf8CppString(&self, input: &str) -> binder::Result<String>174     fn RepeatUtf8CppString(&self, input: &str) -> binder::Result<String> {
175         Ok(input.into())
176     }
177 
GetOtherTestService( &self, name: &str, ) -> binder::Result<binder::Strong<dyn INamedCallback::INamedCallback>>178     fn GetOtherTestService(
179         &self,
180         name: &str,
181     ) -> binder::Result<binder::Strong<dyn INamedCallback::INamedCallback>> {
182         let mut service_map = self.service_map.lock().unwrap();
183         let other_service = service_map.entry(name.into()).or_insert_with(|| {
184             let named_callback = NamedCallback(name.into());
185             INamedCallback::BnNamedCallback::new_binder(named_callback, BinderFeatures::default())
186         });
187         Ok(other_service.to_owned())
188     }
189 
SetOtherTestService( &self, name: &str, service: &binder::Strong<dyn INamedCallback::INamedCallback>, ) -> binder::Result<bool>190     fn SetOtherTestService(
191         &self,
192         name: &str,
193         service: &binder::Strong<dyn INamedCallback::INamedCallback>,
194     ) -> binder::Result<bool> {
195         let mut service_map = self.service_map.lock().unwrap();
196         if let Some(existing_service) = service_map.get(name) {
197             if existing_service == service {
198                 return Ok(true);
199             }
200         }
201         service_map.insert(name.into(), service.clone());
202         Ok(false)
203     }
204 
VerifyName( &self, service: &binder::Strong<dyn INamedCallback::INamedCallback>, name: &str, ) -> binder::Result<bool>205     fn VerifyName(
206         &self,
207         service: &binder::Strong<dyn INamedCallback::INamedCallback>,
208         name: &str,
209     ) -> binder::Result<bool> {
210         service.GetName().map(|found_name| found_name == name)
211     }
212 
GetInterfaceArray( &self, names: &[String], ) -> binder::Result<Vec<binder::Strong<dyn INamedCallback::INamedCallback>>>213     fn GetInterfaceArray(
214         &self,
215         names: &[String],
216     ) -> binder::Result<Vec<binder::Strong<dyn INamedCallback::INamedCallback>>> {
217         names.iter().map(|name| self.GetOtherTestService(name)).collect()
218     }
219 
VerifyNamesWithInterfaceArray( &self, services: &[binder::Strong<dyn INamedCallback::INamedCallback>], names: &[String], ) -> binder::Result<bool>220     fn VerifyNamesWithInterfaceArray(
221         &self,
222         services: &[binder::Strong<dyn INamedCallback::INamedCallback>],
223         names: &[String],
224     ) -> binder::Result<bool> {
225         if services.len() == names.len() {
226             for (s, n) in services.iter().zip(names) {
227                 if !self.VerifyName(s, n)? {
228                     return Ok(false);
229                 }
230             }
231             Ok(true)
232         } else {
233             Ok(false)
234         }
235     }
236 
GetNullableInterfaceArray( &self, names: Option<&[Option<String>]>, ) -> binder::Result<Option<Vec<Option<binder::Strong<dyn INamedCallback::INamedCallback>>>>>237     fn GetNullableInterfaceArray(
238         &self,
239         names: Option<&[Option<String>]>,
240     ) -> binder::Result<Option<Vec<Option<binder::Strong<dyn INamedCallback::INamedCallback>>>>>
241     {
242         if let Some(names) = names {
243             let mut services = vec![];
244             for name in names {
245                 if let Some(name) = name {
246                     services.push(Some(self.GetOtherTestService(name)?));
247                 } else {
248                     services.push(None);
249                 }
250             }
251             Ok(Some(services))
252         } else {
253             Ok(None)
254         }
255     }
256 
VerifyNamesWithNullableInterfaceArray( &self, services: Option<&[Option<binder::Strong<dyn INamedCallback::INamedCallback>>]>, names: Option<&[Option<String>]>, ) -> binder::Result<bool>257     fn VerifyNamesWithNullableInterfaceArray(
258         &self,
259         services: Option<&[Option<binder::Strong<dyn INamedCallback::INamedCallback>>]>,
260         names: Option<&[Option<String>]>,
261     ) -> binder::Result<bool> {
262         if let (Some(services), Some(names)) = (services, names) {
263             for (s, n) in services.iter().zip(names) {
264                 if let (Some(s), Some(n)) = (s, n) {
265                     if !self.VerifyName(s, n)? {
266                         return Ok(false);
267                     }
268                 } else if s.is_some() || n.is_some() {
269                     return Ok(false);
270                 }
271             }
272             Ok(true)
273         } else {
274             Ok(services.is_none() && names.is_none())
275         }
276     }
277 
GetInterfaceList( &self, names: Option<&[Option<String>]>, ) -> binder::Result<Option<Vec<Option<binder::Strong<dyn INamedCallback::INamedCallback>>>>>278     fn GetInterfaceList(
279         &self,
280         names: Option<&[Option<String>]>,
281     ) -> binder::Result<Option<Vec<Option<binder::Strong<dyn INamedCallback::INamedCallback>>>>>
282     {
283         self.GetNullableInterfaceArray(names)
284     }
285 
VerifyNamesWithInterfaceList( &self, services: Option<&[Option<binder::Strong<dyn INamedCallback::INamedCallback>>]>, names: Option<&[Option<String>]>, ) -> binder::Result<bool>286     fn VerifyNamesWithInterfaceList(
287         &self,
288         services: Option<&[Option<binder::Strong<dyn INamedCallback::INamedCallback>>]>,
289         names: Option<&[Option<String>]>,
290     ) -> binder::Result<bool> {
291         self.VerifyNamesWithNullableInterfaceArray(services, names)
292     }
293 
RepeatParcelFileDescriptor( &self, read: &ParcelFileDescriptor, ) -> binder::Result<ParcelFileDescriptor>294     fn RepeatParcelFileDescriptor(
295         &self,
296         read: &ParcelFileDescriptor,
297     ) -> binder::Result<ParcelFileDescriptor> {
298         Ok(dup_fd(read))
299     }
300 
ReverseParcelFileDescriptorArray( &self, input: &[ParcelFileDescriptor], repeated: &mut Vec<Option<ParcelFileDescriptor>>, ) -> binder::Result<Vec<ParcelFileDescriptor>>301     fn ReverseParcelFileDescriptorArray(
302         &self,
303         input: &[ParcelFileDescriptor],
304         repeated: &mut Vec<Option<ParcelFileDescriptor>>,
305     ) -> binder::Result<Vec<ParcelFileDescriptor>> {
306         repeated.clear();
307         repeated.extend(input.iter().map(dup_fd).map(Some));
308         Ok(input.iter().rev().map(dup_fd).collect())
309     }
310 
ThrowServiceException(&self, code: i32) -> binder::Result<()>311     fn ThrowServiceException(&self, code: i32) -> binder::Result<()> {
312         Err(binder::Status::new_service_specific_error(code, None))
313     }
314 
315     impl_repeat_nullable! {RepeatNullableIntArray, i32}
316     impl_repeat_nullable! {RepeatNullableByteEnumArray, ByteEnum}
317     impl_repeat_nullable! {RepeatNullableIntEnumArray, IntEnum}
318     impl_repeat_nullable! {RepeatNullableLongEnumArray, LongEnum}
319     impl_repeat_nullable! {RepeatNullableStringList, Option<String>}
320 
RepeatNullableString(&self, input: Option<&str>) -> binder::Result<Option<String>>321     fn RepeatNullableString(&self, input: Option<&str>) -> binder::Result<Option<String>> {
322         Ok(input.map(String::from))
323     }
324 
RepeatNullableUtf8CppString(&self, input: Option<&str>) -> binder::Result<Option<String>>325     fn RepeatNullableUtf8CppString(&self, input: Option<&str>) -> binder::Result<Option<String>> {
326         Ok(input.map(String::from))
327     }
328 
RepeatNullableParcelable(&self, input: Option<&Empty>) -> binder::Result<Option<Empty>>329     fn RepeatNullableParcelable(&self, input: Option<&Empty>) -> binder::Result<Option<Empty>> {
330         Ok(input.cloned())
331     }
332 
333     impl_repeat_nullable! {RepeatNullableParcelableArray, Option<Empty>}
334     impl_repeat_nullable! {RepeatNullableParcelableList, Option<Empty>}
335 
TakesAnIBinder(&self, _: &SpIBinder) -> binder::Result<()>336     fn TakesAnIBinder(&self, _: &SpIBinder) -> binder::Result<()> {
337         Ok(())
338     }
339 
TakesANullableIBinder(&self, _: Option<&SpIBinder>) -> binder::Result<()>340     fn TakesANullableIBinder(&self, _: Option<&SpIBinder>) -> binder::Result<()> {
341         Ok(())
342     }
343 
TakesAnIBinderList(&self, _: &[SpIBinder]) -> binder::Result<()>344     fn TakesAnIBinderList(&self, _: &[SpIBinder]) -> binder::Result<()> {
345         Ok(())
346     }
347 
TakesANullableIBinderList(&self, _: Option<&[Option<SpIBinder>]>) -> binder::Result<()>348     fn TakesANullableIBinderList(&self, _: Option<&[Option<SpIBinder>]>) -> binder::Result<()> {
349         Ok(())
350     }
351 
ReverseNullableUtf8CppString( &self, input: Option<&[Option<String>]>, repeated: &mut Option<Vec<Option<String>>>, ) -> binder::Result<Option<Vec<Option<String>>>>352     fn ReverseNullableUtf8CppString(
353         &self,
354         input: Option<&[Option<String>]>,
355         repeated: &mut Option<Vec<Option<String>>>,
356     ) -> binder::Result<Option<Vec<Option<String>>>> {
357         if let Some(input) = input {
358             *repeated = Some(input.to_vec());
359             Ok(Some(input.iter().rev().cloned().collect()))
360         } else {
361             // We don't touch `repeated` here, since
362             // the C++ test service doesn't either
363             Ok(None)
364         }
365     }
366 
ReverseUtf8CppStringList( &self, input: Option<&[Option<String>]>, repeated: &mut Option<Vec<Option<String>>>, ) -> binder::Result<Option<Vec<Option<String>>>>367     fn ReverseUtf8CppStringList(
368         &self,
369         input: Option<&[Option<String>]>,
370         repeated: &mut Option<Vec<Option<String>>>,
371     ) -> binder::Result<Option<Vec<Option<String>>>> {
372         self.ReverseNullableUtf8CppString(input, repeated)
373     }
374 
GetCallback( &self, return_null: bool, ) -> binder::Result<Option<binder::Strong<dyn INamedCallback::INamedCallback>>>375     fn GetCallback(
376         &self,
377         return_null: bool,
378     ) -> binder::Result<Option<binder::Strong<dyn INamedCallback::INamedCallback>>> {
379         if return_null {
380             Ok(None)
381         } else {
382             self.GetOtherTestService("ABT: always be testing").map(Some)
383         }
384     }
385 
FillOutStructuredParcelable( &self, parcelable: &mut StructuredParcelable::StructuredParcelable, ) -> binder::Result<()>386     fn FillOutStructuredParcelable(
387         &self,
388         parcelable: &mut StructuredParcelable::StructuredParcelable,
389     ) -> binder::Result<()> {
390         parcelable.shouldBeJerry = "Jerry".into();
391         parcelable.shouldContainThreeFs = vec![parcelable.f, parcelable.f, parcelable.f];
392         parcelable.shouldBeByteBar = ByteEnum::BAR;
393         parcelable.shouldBeIntBar = IntEnum::BAR;
394         parcelable.shouldBeLongBar = LongEnum::BAR;
395         parcelable.shouldContainTwoByteFoos = vec![ByteEnum::FOO, ByteEnum::FOO];
396         parcelable.shouldContainTwoIntFoos = vec![IntEnum::FOO, IntEnum::FOO];
397         parcelable.shouldContainTwoLongFoos = vec![LongEnum::FOO, LongEnum::FOO];
398 
399         parcelable.const_exprs_1 = ConstantExpressionEnum::decInt32_1;
400         parcelable.const_exprs_2 = ConstantExpressionEnum::decInt32_2;
401         parcelable.const_exprs_3 = ConstantExpressionEnum::decInt64_1;
402         parcelable.const_exprs_4 = ConstantExpressionEnum::decInt64_2;
403         parcelable.const_exprs_5 = ConstantExpressionEnum::decInt64_3;
404         parcelable.const_exprs_6 = ConstantExpressionEnum::decInt64_4;
405         parcelable.const_exprs_7 = ConstantExpressionEnum::hexInt32_1;
406         parcelable.const_exprs_8 = ConstantExpressionEnum::hexInt32_2;
407         parcelable.const_exprs_9 = ConstantExpressionEnum::hexInt32_3;
408         parcelable.const_exprs_10 = ConstantExpressionEnum::hexInt64_1;
409 
410         parcelable.shouldSetBit0AndBit2 = StructuredParcelable::BIT0 | StructuredParcelable::BIT2;
411 
412         parcelable.u = Some(Union::Union::Ns(vec![1, 2, 3]));
413         parcelable.shouldBeConstS1 = Some(Union::Union::S(Union::S1.to_string()));
414         Ok(())
415     }
416 
RepeatExtendableParcelable( &self, ep: &ExtendableParcelable, ep2: &mut ExtendableParcelable, ) -> binder::Result<()>417     fn RepeatExtendableParcelable(
418         &self,
419         ep: &ExtendableParcelable,
420         ep2: &mut ExtendableParcelable,
421     ) -> binder::Result<()> {
422         ep2.a = ep.a;
423         ep2.b = ep.b.clone();
424 
425         let my_ext = ep.ext.get_parcelable::<MyExt>()?;
426         if let Some(my_ext) = my_ext {
427             ep2.ext.set_parcelable(my_ext)?;
428         } else {
429             ep2.ext.reset();
430         }
431 
432         Ok(())
433     }
434 
ReverseList(&self, list: &RecursiveList) -> binder::Result<RecursiveList>435     fn ReverseList(&self, list: &RecursiveList) -> binder::Result<RecursiveList> {
436         let mut reversed: Option<RecursiveList> = None;
437         let mut cur: Option<&RecursiveList> = Some(list);
438         while let Some(node) = cur {
439             reversed = Some(RecursiveList { value: node.value, next: reversed.map(Box::new) });
440             cur = node.next.as_ref().map(|n| n.as_ref());
441         }
442         // `list` is always not empty, so is `reversed`.
443         Ok(reversed.unwrap())
444     }
445 
ReverseIBinderArray( &self, input: &[SpIBinder], repeated: &mut Vec<Option<SpIBinder>>, ) -> binder::Result<Vec<SpIBinder>>446     fn ReverseIBinderArray(
447         &self,
448         input: &[SpIBinder],
449         repeated: &mut Vec<Option<SpIBinder>>,
450     ) -> binder::Result<Vec<SpIBinder>> {
451         *repeated = input.iter().cloned().map(Some).collect();
452         Ok(input.iter().rev().cloned().collect())
453     }
454 
ReverseNullableIBinderArray( &self, input: Option<&[Option<SpIBinder>]>, repeated: &mut Option<Vec<Option<SpIBinder>>>, ) -> binder::Result<Option<Vec<Option<SpIBinder>>>>455     fn ReverseNullableIBinderArray(
456         &self,
457         input: Option<&[Option<SpIBinder>]>,
458         repeated: &mut Option<Vec<Option<SpIBinder>>>,
459     ) -> binder::Result<Option<Vec<Option<SpIBinder>>>> {
460         let input = input.expect("input is null");
461         *repeated = Some(input.to_vec());
462         Ok(Some(input.iter().rev().cloned().collect()))
463     }
464 
GetOldNameInterface(&self) -> binder::Result<binder::Strong<dyn IOldName::IOldName>>465     fn GetOldNameInterface(&self) -> binder::Result<binder::Strong<dyn IOldName::IOldName>> {
466         Ok(IOldName::BnOldName::new_binder(OldName, BinderFeatures::default()))
467     }
468 
GetNewNameInterface(&self) -> binder::Result<binder::Strong<dyn INewName::INewName>>469     fn GetNewNameInterface(&self) -> binder::Result<binder::Strong<dyn INewName::INewName>> {
470         Ok(INewName::BnNewName::new_binder(NewName, BinderFeatures::default()))
471     }
472 
GetUnionTags(&self, input: &[Union::Union]) -> binder::Result<Vec<Union::Tag::Tag>>473     fn GetUnionTags(&self, input: &[Union::Union]) -> binder::Result<Vec<Union::Tag::Tag>> {
474         Ok(input
475             .iter()
476             .map(|u| match u {
477                 Union::Union::Ns(_) => Union::Tag::Tag::ns,
478                 Union::Union::N(_) => Union::Tag::Tag::n,
479                 Union::Union::M(_) => Union::Tag::Tag::m,
480                 Union::Union::S(_) => Union::Tag::Tag::s,
481                 Union::Union::Ibinder(_) => Union::Tag::Tag::ibinder,
482                 Union::Union::Ss(_) => Union::Tag::Tag::ss,
483                 Union::Union::Be(_) => Union::Tag::Tag::be,
484             })
485             .collect::<Vec<_>>())
486     }
487 
GetCppJavaTests(&self) -> binder::Result<Option<SpIBinder>>488     fn GetCppJavaTests(&self) -> binder::Result<Option<SpIBinder>> {
489         Ok(None)
490     }
491 
getBackendType(&self) -> binder::Result<BackendType>492     fn getBackendType(&self) -> binder::Result<BackendType> {
493         Ok(BackendType::RUST)
494     }
495 
GetCircular( &self, _: &mut CircularParcelable, ) -> binder::Result<binder::Strong<dyn ICircular::ICircular>>496     fn GetCircular(
497         &self,
498         _: &mut CircularParcelable,
499     ) -> binder::Result<binder::Strong<dyn ICircular::ICircular>> {
500         Ok(ICircular::BnCircular::new_binder(Circular, BinderFeatures::default()))
501     }
502 }
503 
504 struct FooInterface;
505 
506 impl Interface for FooInterface {}
507 
508 impl IFooInterface::IFooInterface for FooInterface {
originalApi(&self) -> binder::Result<()>509     fn originalApi(&self) -> binder::Result<()> {
510         Ok(())
511     }
acceptUnionAndReturnString(&self, u: &BazUnion) -> binder::Result<String>512     fn acceptUnionAndReturnString(&self, u: &BazUnion) -> binder::Result<String> {
513         match u {
514             BazUnion::IntNum(n) => Ok(n.to_string()),
515         }
516     }
returnsLengthOfFooArray(&self, foos: &[Foo]) -> binder::Result<i32>517     fn returnsLengthOfFooArray(&self, foos: &[Foo]) -> binder::Result<i32> {
518         Ok(foos.len() as i32)
519     }
ignoreParcelablesAndRepeatInt( &self, _in_foo: &Foo, _inout_foo: &mut Foo, _out_foo: &mut Foo, value: i32, ) -> binder::Result<i32>520     fn ignoreParcelablesAndRepeatInt(
521         &self,
522         _in_foo: &Foo,
523         _inout_foo: &mut Foo,
524         _out_foo: &mut Foo,
525         value: i32,
526     ) -> binder::Result<i32> {
527         Ok(value)
528     }
529 }
530 
531 struct NestedService;
532 
533 impl Interface for NestedService {}
534 
535 impl INestedService::INestedService for NestedService {
flipStatus( &self, p: &ParcelableWithNested::ParcelableWithNested, ) -> binder::Result<INestedService::Result::Result>536     fn flipStatus(
537         &self,
538         p: &ParcelableWithNested::ParcelableWithNested,
539     ) -> binder::Result<INestedService::Result::Result> {
540         if p.status == ParcelableWithNested::Status::Status::OK {
541             Ok(INestedService::Result::Result {
542                 status: ParcelableWithNested::Status::Status::NOT_OK,
543             })
544         } else {
545             Ok(INestedService::Result::Result { status: ParcelableWithNested::Status::Status::OK })
546         }
547     }
flipStatusWithCallback( &self, st: ParcelableWithNested::Status::Status, cb: &binder::Strong<dyn INestedService::ICallback::ICallback>, ) -> binder::Result<()>548     fn flipStatusWithCallback(
549         &self,
550         st: ParcelableWithNested::Status::Status,
551         cb: &binder::Strong<dyn INestedService::ICallback::ICallback>,
552     ) -> binder::Result<()> {
553         if st == ParcelableWithNested::Status::Status::OK {
554             cb.done(ParcelableWithNested::Status::Status::NOT_OK)
555         } else {
556             cb.done(ParcelableWithNested::Status::Status::OK)
557         }
558     }
559 }
560 
561 struct FixedSizeArrayService;
562 
563 impl Interface for FixedSizeArrayService {}
564 
565 impl IRepeatFixedSizeArray::IRepeatFixedSizeArray for FixedSizeArrayService {
RepeatBytes(&self, input: &[u8; 3], repeated: &mut [u8; 3]) -> binder::Result<[u8; 3]>566     fn RepeatBytes(&self, input: &[u8; 3], repeated: &mut [u8; 3]) -> binder::Result<[u8; 3]> {
567         *repeated = *input;
568         Ok(*input)
569     }
RepeatInts(&self, input: &[i32; 3], repeated: &mut [i32; 3]) -> binder::Result<[i32; 3]>570     fn RepeatInts(&self, input: &[i32; 3], repeated: &mut [i32; 3]) -> binder::Result<[i32; 3]> {
571         *repeated = *input;
572         Ok(*input)
573     }
RepeatBinders( &self, input: &[SpIBinder; 3], repeated: &mut [Option<SpIBinder>; 3], ) -> binder::Result<[SpIBinder; 3]>574     fn RepeatBinders(
575         &self,
576         input: &[SpIBinder; 3],
577         repeated: &mut [Option<SpIBinder>; 3],
578     ) -> binder::Result<[SpIBinder; 3]> {
579         *repeated = input.clone().map(Some);
580         Ok(input.clone())
581     }
RepeatParcelables( &self, input: &[IntParcelable; 3], repeated: &mut [IntParcelable; 3], ) -> binder::Result<[IntParcelable; 3]>582     fn RepeatParcelables(
583         &self,
584         input: &[IntParcelable; 3],
585         repeated: &mut [IntParcelable; 3],
586     ) -> binder::Result<[IntParcelable; 3]> {
587         *repeated = *input;
588         Ok(*input)
589     }
Repeat2dBytes( &self, input: &[[u8; 3]; 2], repeated: &mut [[u8; 3]; 2], ) -> binder::Result<[[u8; 3]; 2]>590     fn Repeat2dBytes(
591         &self,
592         input: &[[u8; 3]; 2],
593         repeated: &mut [[u8; 3]; 2],
594     ) -> binder::Result<[[u8; 3]; 2]> {
595         *repeated = *input;
596         Ok(*input)
597     }
Repeat2dInts( &self, input: &[[i32; 3]; 2], repeated: &mut [[i32; 3]; 2], ) -> binder::Result<[[i32; 3]; 2]>598     fn Repeat2dInts(
599         &self,
600         input: &[[i32; 3]; 2],
601         repeated: &mut [[i32; 3]; 2],
602     ) -> binder::Result<[[i32; 3]; 2]> {
603         *repeated = *input;
604         Ok(*input)
605     }
Repeat2dBinders( &self, input: &[[SpIBinder; 3]; 2], repeated: &mut [[Option<SpIBinder>; 3]; 2], ) -> binder::Result<[[SpIBinder; 3]; 2]>606     fn Repeat2dBinders(
607         &self,
608         input: &[[SpIBinder; 3]; 2],
609         repeated: &mut [[Option<SpIBinder>; 3]; 2],
610     ) -> binder::Result<[[SpIBinder; 3]; 2]> {
611         *repeated = input.clone().map(|nested| nested.map(Some));
612         Ok(input.clone())
613     }
Repeat2dParcelables( &self, input: &[[IntParcelable; 3]; 2], repeated: &mut [[IntParcelable; 3]; 2], ) -> binder::Result<[[IntParcelable; 3]; 2]>614     fn Repeat2dParcelables(
615         &self,
616         input: &[[IntParcelable; 3]; 2],
617         repeated: &mut [[IntParcelable; 3]; 2],
618     ) -> binder::Result<[[IntParcelable; 3]; 2]> {
619         *repeated = *input;
620         Ok(*input)
621     }
622 }
623 
main()624 fn main() {
625     binder::ProcessState::set_thread_pool_max_thread_count(0);
626     binder::ProcessState::start_thread_pool();
627 
628     let service_name = <BpTestService as ITestService::ITestService>::get_descriptor();
629     let service = BnTestService::new_binder(TestService::default(), BinderFeatures::default());
630     binder::add_service(service_name, service.as_binder()).expect("Could not register service");
631 
632     let versioned_service_name = <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor();
633     let versioned_service = BnFooInterface::new_binder(FooInterface, BinderFeatures::default());
634     binder::add_service(versioned_service_name, versioned_service.as_binder())
635         .expect("Could not register service");
636 
637     let nested_service_name =
638         <INestedService::BpNestedService as INestedService::INestedService>::get_descriptor();
639     let nested_service =
640         INestedService::BnNestedService::new_binder(NestedService, BinderFeatures::default());
641     binder::add_service(nested_service_name, nested_service.as_binder())
642         .expect("Could not register service");
643 
644     let fixed_size_array_service_name =
645         <IRepeatFixedSizeArray::BpRepeatFixedSizeArray as IRepeatFixedSizeArray::IRepeatFixedSizeArray>::get_descriptor();
646     let fixed_size_array_service = IRepeatFixedSizeArray::BnRepeatFixedSizeArray::new_binder(
647         FixedSizeArrayService,
648         BinderFeatures::default(),
649     );
650     binder::add_service(fixed_size_array_service_name, fixed_size_array_service.as_binder())
651         .expect("Could not register service");
652 
653     binder::ProcessState::join_thread_pool();
654 }
655