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