1use crate::{
2 client::{Client, DoCanClient},
3 error::DoCanError,
4 SecurityAlgo,
5};
6use iso14229_1::{
7 request::{self, Request},
8 response,
9 utils::U24,
10 *,
11};
12use iso15765_2::{Address, AddressType};
13use rs_can::{CanDevice, CanFrame, CanResult};
14use std::{fmt::Display, hash::Hash, time::Duration};
15use tokio::time::sleep;
16
17#[async_trait::async_trait]
18impl<D, C, F> Client for DoCanClient<D, C, F>
19where
20 D: CanDevice<Channel = C, Frame = F> + Clone + Send + Sync + 'static,
21 C: Display + Clone + Hash + Eq + Send + Sync + 'static,
22 F: CanFrame<Channel = C> + Clone + Display + Send + Sync + 'static,
23{
24 type Channel = C;
25 type Error = DoCanError;
26
27 async fn update_address(
28 &mut self,
29 channel: Self::Channel,
30 address: Address,
31 ) -> CanResult<(), Self::Error> {
32 self.with_context(channel, |_, iso_tp, _| async move {
33 iso_tp.update_address(address).await;
34
35 Ok(())
36 })
37 .await
38 }
39
40 async fn update_security_algo(
41 &mut self,
42 channel: Self::Channel,
43 algo: SecurityAlgo,
44 ) -> CanResult<(), Self::Error> {
45 self.context_util(channel, |ctx| async move {
46 let mut guard = ctx.security_algo.lock().await;
47 *guard = Some(algo);
48
49 Ok(())
50 })
51 .await
52 }
53
54 async fn add_data_identifier(
55 &mut self,
56 channel: Self::Channel,
57 did: DataIdentifier,
58 length: usize,
59 ) -> CanResult<(), Self::Error> {
60 self.context_util(channel, |ctx| async move {
61 ctx.did.lock().await.insert(did, length);
62
63 Ok(())
64 })
65 .await
66 }
67
68 async fn remove_data_identifier(
69 &mut self,
70 channel: Self::Channel,
71 did: DataIdentifier,
72 ) -> CanResult<(), Self::Error> {
73 self.context_util(channel, |ctx| async move {
74 ctx.did.lock().await.remove(&did);
75
76 Ok(())
77 })
78 .await
79 }
80
81 async fn session_ctrl(
82 &mut self,
83 channel: Self::Channel,
84 r#type: SessionType,
85 suppress_positive: bool,
86 addr_type: AddressType,
87 ) -> CanResult<(), Self::Error> {
88 self.with_context(channel, |listener, iso_tp, did| async move {
89 let service = Service::SessionCtrl;
90 let mut sub_func: u8 = r#type.into();
91 if suppress_positive {
92 sub_func |= SUPPRESS_POSITIVE;
93 }
94 let request = Request::new(service, Some(sub_func), vec![], &did)
95 .map_err(DoCanError::ISO14229Error)?;
96
97 if let Some(response) = Self::suppress_positive_sr(
98 &listener,
99 &iso_tp,
100 &did,
101 addr_type,
102 request,
103 suppress_positive,
104 )
105 .await?
106 {
107 Self::sub_func_check(&response, r#type.into(), service)?;
108
109 let timing = response
110 .data::<response::SessionCtrl>(&did)
111 .map_err(DoCanError::ISO14229Error)?
112 .0;
113 listener
114 .update_p2_ctx(timing.p2, timing.p2_star as u32)
115 .await;
116 }
117
118 Ok(())
119 })
120 .await
121 }
122
123 async fn ecu_reset(
124 &mut self,
125 channel: Self::Channel,
126 r#type: ECUResetType,
127 suppress_positive: bool,
128 addr_type: AddressType,
129 ) -> CanResult<(), Self::Error> {
130 self.with_context(channel, |listener, iso_tp, did| async move {
131 let service = Service::ECUReset;
132 let mut sub_func: u8 = r#type.into();
133 if suppress_positive {
134 sub_func |= SUPPRESS_POSITIVE;
135 }
136 let request = Request::new(service, Some(sub_func), vec![], &did)
137 .map_err(DoCanError::ISO14229Error)?;
138
139 if let Some(response) = Self::suppress_positive_sr(
140 &listener,
141 &iso_tp,
142 &did,
143 addr_type,
144 request,
145 suppress_positive,
146 )
147 .await?
148 {
149 Self::sub_func_check(&response, r#type.into(), service)?;
150
151 let resp = response
152 .data::<response::ECUReset>(&did)
153 .map_err(DoCanError::ISO14229Error)?;
154 if let Some(seconds) = resp.second {
155 sleep(Duration::from_secs(seconds as u64)).await;
156 }
157 }
158
159 Ok(())
160 })
161 .await
162 }
163
164 async fn security_access(
165 &mut self,
166 channel: Self::Channel,
167 level: u8,
168 params: Vec<u8>,
169 ) -> CanResult<Vec<u8>, Self::Error> {
170 self.with_context(channel, |listener, iso_tp, did| {
171 let params = params.clone();
172 async move {
173 let service = Service::SecurityAccess;
174 let request = Request::new(service, Some(level), params, &did)
175 .map_err(DoCanError::ISO14229Error)?;
176
177 let response = Self::send_and_response(
178 &listener,
179 &iso_tp,
180 &did,
181 AddressType::Physical,
182 request,
183 )
184 .await?;
185
186 Self::sub_func_check(&response, level, service)?;
187
188 Ok(response.raw_data().to_vec())
189 }
190 })
191 .await
192 }
193
194 async fn unlock_security_access(
195 &mut self,
196 channel: Self::Channel,
197 level: u8,
198 params: Vec<u8>,
199 salt: Vec<u8>,
200 ) -> CanResult<(), Self::Error> {
201 self.with_security_algo_ctx(channel, |listener, iso_tp, did, algo| {
202 let params = params.clone();
203 let salt = salt.clone();
204 async move {
205 let service = Service::SecurityAccess;
206 let req = Request::new(service, Some(level), params, &did)
207 .map_err(DoCanError::ISO14229Error)?;
208
209 let resp =
210 Self::send_and_response(&listener, &iso_tp, &did, AddressType::Physical, req)
211 .await?;
212 Self::sub_func_check(&resp, level, service)?;
213
214 let seed = resp.raw_data().to_vec();
215 match algo(level, seed, salt)? {
216 Some(data) => {
217 let request = Request::new(service, Some(level + 1), data, &did)
218 .map_err(DoCanError::ISO14229Error)?;
219 let response = Self::send_and_response(
220 &listener,
221 &iso_tp,
222 &did,
223 AddressType::Physical,
224 request,
225 )
226 .await?;
227
228 Self::sub_func_check(&response, level + 1, service)
229 }
230 None => Ok(()),
231 }
232 }
233 })
234 .await
235 }
236
237 async fn communication_control(
238 &mut self,
239 channel: Self::Channel,
240 ctrl_type: CommunicationCtrlType,
241 comm_type: CommunicationType,
242 node_id: Option<request::NodeId>,
243 suppress_positive: bool,
244 addr_type: AddressType,
245 ) -> CanResult<(), Self::Error> {
246 self.with_context(channel, |listener, iso_tp, did| async move {
247 let service = Service::CommunicationCtrl;
248 let mut sub_func = ctrl_type.into();
249 if suppress_positive {
250 sub_func |= SUPPRESS_POSITIVE;
251 }
252 let data = request::CommunicationCtrl::new(ctrl_type, comm_type, node_id)
253 .map_err(DoCanError::ISO14229Error)?;
254 let req = Request::new(service, Some(sub_func), data.into(), &did)
255 .map_err(DoCanError::ISO14229Error)?;
256
257 let resp = Self::suppress_positive_sr(
258 &listener,
259 &iso_tp,
260 &did,
261 addr_type,
262 req,
263 suppress_positive,
264 )
265 .await?;
266
267 if let Some(response) = resp {
268 Self::sub_func_check(&response, ctrl_type.into(), service)?;
269 }
270
271 Ok(())
272 })
273 .await
274 }
275
276 #[cfg(feature = "std2020")]
277 async fn authentication(
278 &mut self,
279 channel: Self::Channel,
280 auth_task: AuthenticationTask,
281 data: request::Authentication,
282 ) -> CanResult<response::Authentication, Self::Error> {
283 self.with_context(channel, |listener, iso_tp, did| {
284 let data = data.clone();
285 async move {
286 let service = Service::Authentication;
287 let request = Request::new(service, Some(auth_task.into()), data.into(), &did)
288 .map_err(DoCanError::ISO14229Error)?;
289
290 let response = Self::send_and_response(
291 &listener,
292 &iso_tp,
293 &did,
294 AddressType::Physical,
295 request,
296 )
297 .await?;
298 Self::sub_func_check(&response, auth_task.into(), service)?;
299
300 response
301 .data::<response::Authentication>(&did)
302 .map_err(DoCanError::ISO14229Error)
303 }
304 })
305 .await
306 }
307
308 async fn tester_present(
309 &mut self,
310 channel: Self::Channel,
311 r#type: TesterPresentType,
312 suppress_positive: bool,
313 addr_type: AddressType,
314 ) -> CanResult<(), Self::Error> {
315 self.with_context(channel, |listener, iso_tp, did| async move {
316 let (service, request) =
317 Self::tester_present_request(&did, r#type, suppress_positive).await?;
318
319 let response = Self::suppress_positive_sr(
320 &listener,
321 &iso_tp,
322 &did,
323 addr_type,
324 request,
325 suppress_positive,
326 )
327 .await?;
328
329 if let Some(response) = response {
330 Self::sub_func_check(&response, r#type.into(), service)?;
331 }
332
333 Ok(())
334 })
335 .await
336 }
337
338 #[cfg(any(feature = "std2006", feature = "std2013"))]
339 async fn access_timing_parameter(
340 &mut self,
341 channel: Self::Channel,
342 r#type: request::TimingParameterAccessType,
343 parameter: Vec<u8>,
344 suppress_positive: bool,
345 ) -> CanResult<Option<response::AccessTimingParameter>, Self::Error> {
346 self.with_context(channel, |listener, iso_tp, did| async move {
347 let service = Service::AccessTimingParam;
348 let mut sub_func = r#type.into();
349 if suppress_positive {
350 sub_func |= SUPPRESS_POSITIVE;
351 }
352 let request = Request::new(service, Some(sub_func), parameter, &did)
353 .map_err(DoCanError::ISO14229Error)?;
354
355 let response = Self::suppress_positive_sr(
356 &listener,
357 &iso_tp,
358 &did,
359 AddressType::Physical,
360 request,
361 suppress_positive,
362 )
363 .await?;
364
365 match response {
366 Some(v) => {
367 Self::sub_func_check(&v, r#type.into(), service)?;
368 Ok(Some(v.data().map_err(DoCanError::ISO14229Error)?))
369 }
370 None => Ok(None),
371 }
372 })
373 .await
374 }
375
376 async fn secured_data_transmit(
377 &mut self,
378 channel: Self::Channel,
379 apar: AdministrativeParameter,
380 signature: SignatureEncryptionCalculation,
381 anti_replay_cnt: u16,
382 service: u8,
383 service_data: Vec<u8>,
384 signature_data: Vec<u8>,
385 ) -> CanResult<response::SecuredDataTrans, Self::Error> {
386 self.with_context(channel, |listener, iso_tp, did| {
387 let service_data = service_data.clone();
388 let signature_data = signature_data.clone();
389 async move {
390 let data = request::SecuredDataTrans::new(
391 apar,
392 signature,
393 anti_replay_cnt,
394 service,
395 service_data,
396 signature_data,
397 )
398 .map_err(DoCanError::ISO14229Error)?;
399 let request = Request::new(Service::SecuredDataTrans, None, data.into(), &did)
400 .map_err(DoCanError::ISO14229Error)?;
401
402 let response = Self::send_and_response(
403 &listener,
404 &iso_tp,
405 &did,
406 AddressType::Physical,
407 request,
408 )
409 .await?;
410
411 response
412 .data::<response::SecuredDataTrans>(&did)
413 .map_err(DoCanError::ISO14229Error)
414 }
415 })
416 .await
417 }
418
419 async fn control_dtc_setting(
420 &mut self,
421 channel: Self::Channel,
422 r#type: DTCSettingType,
423 parameter: Vec<u8>,
424 suppress_positive: bool,
425 ) -> CanResult<(), Self::Error> {
426 self.with_context(channel, |listener, iso_tp, did| {
427 let parameter = parameter.clone();
428 async move {
429 let service = Service::CtrlDTCSetting;
430 let mut sub_func = r#type.into();
431 if suppress_positive {
432 sub_func |= SUPPRESS_POSITIVE;
433 }
434 let request = Request::new(service, Some(sub_func), parameter, &did)
435 .map_err(DoCanError::ISO14229Error)?;
436
437 let response = Self::suppress_positive_sr(
438 &listener,
439 &iso_tp,
440 &did,
441 AddressType::Physical,
442 request,
443 suppress_positive,
444 )
445 .await?;
446
447 if let Some(response) = response {
448 Self::sub_func_check(&response, r#type.into(), service)?;
449 }
450
451 Ok(())
452 }
453 })
454 .await
455 }
456
457 async fn response_on_event(&mut self, channel: Self::Channel) -> CanResult<(), Self::Error> {
458 self.with_context(channel, |_, _, _| async move {
459 Err(DoCanError::NotImplement(Service::ResponseOnEvent))
460 })
461 .await
462 }
463
464 async fn link_control(
465 &mut self,
466 channel: Self::Channel,
467 r#type: LinkCtrlType,
468 data: request::LinkCtrl,
469 suppress_positive: bool,
470 ) -> CanResult<(), Self::Error> {
471 self.with_context(channel, |listener, iso_tp, did| {
472 let data = data.clone();
473 async move {
474 let service = Service::LinkCtrl;
475 let mut sub_func = r#type.into();
476 if suppress_positive {
477 sub_func |= SUPPRESS_POSITIVE;
478 }
479 let request = Request::new(service, Some(sub_func), data.into(), &did)
480 .map_err(DoCanError::ISO14229Error)?;
481
482 let response = Self::suppress_positive_sr(
483 &listener,
484 &iso_tp,
485 &did,
486 AddressType::Physical,
487 request,
488 suppress_positive,
489 )
490 .await?;
491
492 if let Some(response) = response {
493 Self::sub_func_check(&response, r#type.into(), service)?;
494 }
495
496 Ok(())
497 }
498 })
499 .await
500 }
501
502 async fn read_data_by_identifier(
503 &mut self,
504 channel: Self::Channel,
505 did: DataIdentifier,
506 others: Vec<DataIdentifier>,
507 ) -> CanResult<response::ReadDID, Self::Error> {
508 self.with_context(channel, |listener, iso_tp, cfg| {
509 let others = others.clone();
510 async move {
511 let data = request::ReadDID::new(did, others);
512 let request = Request::new(Service::ReadDID, None, data.into(), &cfg)
513 .map_err(DoCanError::ISO14229Error)?;
514
515 let response = Self::send_and_response(
516 &listener,
517 &iso_tp,
518 &cfg,
519 AddressType::Physical,
520 request,
521 )
522 .await?;
523
524 response
525 .data::<response::ReadDID>(&cfg)
526 .map_err(DoCanError::ISO14229Error)
527 }
528 })
529 .await
530 }
531
532 async fn read_memory_by_address(
533 &mut self,
534 channel: Self::Channel,
535 mem_loc: MemoryLocation,
536 ) -> CanResult<Vec<u8>, Self::Error> {
537 self.with_context(channel, |listener, iso_tp, did| async move {
538 let data = request::ReadMemByAddr(mem_loc);
539 let request = Request::new(Service::ReadMemByAddr, None, data.into(), &did)
540 .map_err(DoCanError::ISO14229Error)?;
541
542 let response =
543 Self::send_and_response(&listener, &iso_tp, &did, AddressType::Physical, request)
544 .await?;
545
546 Ok(response.raw_data().to_vec())
547 })
548 .await
549 }
550
551 async fn read_scaling_data_by_identifier(
552 &mut self,
553 channel: Self::Channel,
554 did: DataIdentifier,
555 ) -> CanResult<response::ReadScalingDID, Self::Error> {
556 self.with_context(channel, |listener, iso_tp, cfg| async move {
557 let data = request::ReadScalingDID(did);
558 let request = Request::new(Service::ReadScalingDID, None, data.into(), &cfg)
559 .map_err(DoCanError::ISO14229Error)?;
560
561 let response =
562 Self::send_and_response(&listener, &iso_tp, &cfg, AddressType::Physical, request)
563 .await?;
564
565 response
566 .data::<response::ReadScalingDID>(&cfg)
567 .map_err(DoCanError::ISO14229Error)
568 })
569 .await
570 }
571
572 async fn read_data_by_period_identifier(
573 &mut self,
574 channel: Self::Channel,
575 mode: request::TransmissionMode,
576 did: Vec<u8>,
577 ) -> CanResult<response::ReadDataByPeriodId, Self::Error> {
578 self.with_context(channel, |listener, iso_tp, cfg| {
579 let did = did.clone();
580 async move {
581 let data = request::ReadDataByPeriodId::new(mode, did)
582 .map_err(DoCanError::ISO14229Error)?;
583 let request = Request::new(Service::ReadDataByPeriodId, None, data.into(), &cfg)
584 .map_err(DoCanError::ISO14229Error)?;
585
586 let response = Self::send_and_response(
587 &listener,
588 &iso_tp,
589 &cfg,
590 AddressType::Physical,
591 request,
592 )
593 .await?;
594
595 response
596 .data::<response::ReadDataByPeriodId>(&cfg)
597 .map_err(DoCanError::ISO14229Error)
598 }
599 })
600 .await
601 }
602
603 async fn dynamically_define_data_by_identifier(
604 &mut self,
605 channel: Self::Channel,
606 r#type: DefinitionType,
607 data: request::DynamicallyDefineDID,
608 suppress_positive: bool,
609 ) -> CanResult<Option<response::DynamicallyDefineDID>, Self::Error> {
610 self.with_context(channel, |listener, iso_tp, did| {
611 let data = data.clone();
612 async move {
613 let service = Service::DynamicalDefineDID;
614 let mut sub_func = r#type.into();
615 if suppress_positive {
616 sub_func |= SUPPRESS_POSITIVE;
617 }
618 let request = Request::new(service, Some(sub_func), data.into(), &did)
619 .map_err(DoCanError::ISO14229Error)?;
620
621 let response = Self::suppress_positive_sr(
622 &listener,
623 &iso_tp,
624 &did,
625 AddressType::Physical,
626 request,
627 suppress_positive,
628 )
629 .await?;
630
631 match response {
632 Some(v) => {
633 Self::sub_func_check(&v, r#type.into(), service)?;
634 Ok(Some(v.data(&did).map_err(DoCanError::ISO14229Error)?))
635 }
636 None => Ok(None),
637 }
638 }
639 })
640 .await
641 }
642
643 async fn write_data_by_identifier(
644 &mut self,
645 channel: Self::Channel,
646 did: DataIdentifier,
647 data: Vec<u8>,
648 ) -> CanResult<(), Self::Error> {
649 self.with_context(channel, |listener, iso_tp, cfg| {
650 let data = data.clone();
651 async move {
652 let data = request::WriteDID(DIDData { did, data });
653 let request = Request::new(Service::WriteDID, None, data.into(), &cfg)
654 .map_err(DoCanError::ISO14229Error)?;
655
656 let _ = Self::send_and_response(
657 &listener,
658 &iso_tp,
659 &cfg,
660 AddressType::Physical,
661 request,
662 )
663 .await?;
664
665 Ok(())
666 }
667 })
668 .await
669 }
670
671 async fn write_memory_by_address(
672 &mut self,
673 channel: Self::Channel,
674 alfi: AddressAndLengthFormatIdentifier,
675 mem_addr: u128,
676 mem_size: u128,
677 record: Vec<u8>,
678 ) -> CanResult<response::WriteMemByAddr, Self::Error> {
679 self.with_context(channel, |listener, iso_tp, did| {
680 let record = record.clone();
681 async move {
682 let data = request::WriteMemByAddr::new(alfi, mem_addr, mem_size, record)
683 .map_err(DoCanError::ISO14229Error)?;
684 let request = Request::new(Service::WriteMemByAddr, None, data.into(), &did)
685 .map_err(DoCanError::ISO14229Error)?;
686
687 let response = Self::send_and_response(
688 &listener,
689 &iso_tp,
690 &did,
691 AddressType::Physical,
692 request,
693 )
694 .await?;
695
696 response
697 .data::<response::WriteMemByAddr>(&did)
698 .map_err(DoCanError::ISO14229Error)
699 }
700 })
701 .await
702 }
703
704 async fn clear_dtc_info(
705 &mut self,
706 channel: Self::Channel,
707 group: U24,
708 mem_sel: Option<u8>,
709 addr_type: AddressType,
710 ) -> CanResult<(), Self::Error> {
711 self.with_context(channel, |listener, iso_tp, did| async move {
712 let group = group.clone();
713 async move {
714 #[cfg(any(feature = "std2020"))]
715 let data = request::ClearDiagnosticInfo::new(group, mem_sel);
716 #[cfg(any(feature = "std2006", feature = "std2013"))]
717 let data = request::ClearDiagnosticInfo::new(group);
718 let request = Request::new(Service::ClearDiagnosticInfo, None, data.into(), &did)
719 .map_err(DoCanError::ISO14229Error)?;
720
721 let _ =
722 Self::send_and_response(&listener, &iso_tp, &did, addr_type, request).await?;
723
724 Ok(())
725 }
726 .await
727 })
728 .await
729 }
730
731 async fn read_dtc_info(
732 &mut self,
733 channel: Self::Channel,
734 r#type: DTCReportType,
735 data: request::DTCInfo,
736 ) -> CanResult<response::DTCInfo, Self::Error> {
737 self.with_context(channel, |listener, iso_tp, did| async move {
738 let data = data.clone();
739 async move {
740 let service = Service::ReadDTCInfo;
741 let request = Request::new(service, Some(r#type.into()), data.into(), &did)
742 .map_err(DoCanError::ISO14229Error)?;
743
744 let response = Self::send_and_response(
745 &listener,
746 &iso_tp,
747 &did,
748 AddressType::Physical,
749 request,
750 )
751 .await?;
752 Self::sub_func_check(&response, r#type.into(), service)?;
753
754 response
755 .data::<response::DTCInfo>(&did)
756 .map_err(DoCanError::ISO14229Error)
757 }
758 .await
759 })
760 .await
761 }
762
763 async fn io_control(
764 &mut self,
765 channel: Self::Channel,
766 did: DataIdentifier,
767 param: IOCtrlParameter,
768 state: Vec<u8>,
769 mask: Vec<u8>,
770 ) -> CanResult<response::IOCtrl, Self::Error> {
771 self.with_context(channel, |listener, iso_tp, cfg| {
772 let state = state.clone();
773 let mask = mask.clone();
774 async move {
775 let data = request::IOCtrl::new(did, param, state, mask, &cfg)
776 .map_err(DoCanError::ISO14229Error)?;
777 let request = Request::new(Service::IOCtrl, None, data.into(), &cfg)
778 .map_err(DoCanError::ISO14229Error)?;
779
780 let response = Self::send_and_response(
781 &listener,
782 &iso_tp,
783 &cfg,
784 AddressType::Physical,
785 request,
786 )
787 .await?;
788
789 response
790 .data::<response::IOCtrl>(&cfg)
791 .map_err(DoCanError::ISO14229Error)
792 }
793 })
794 .await
795 }
796
797 async fn routine_control(
798 &mut self,
799 channel: Self::Channel,
800 r#type: RoutineCtrlType,
801 routine_id: u16,
802 option_record: Vec<u8>,
803 ) -> CanResult<response::RoutineCtrl, Self::Error> {
804 self.with_context(channel, |listener, iso_tp, did| {
805 let option_record = option_record.clone();
806 async move {
807 let service = Service::RoutineCtrl;
808 let data = request::RoutineCtrl {
809 routine_id: RoutineId(routine_id),
810 option_record,
811 };
812 let request = Request::new(service, Some(r#type.into()), data.into(), &did)
813 .map_err(DoCanError::ISO14229Error)?;
814
815 let response = Self::send_and_response(
816 &listener,
817 &iso_tp,
818 &did,
819 AddressType::Physical,
820 request,
821 )
822 .await?;
823 Self::sub_func_check(&response, r#type.into(), service)?;
824
825 response
826 .data::<response::RoutineCtrl>(&did)
827 .map_err(DoCanError::ISO14229Error)
828 }
829 })
830 .await
831 }
832
833 async fn request_download(
834 &mut self,
835 channel: Self::Channel,
836 alfi: AddressAndLengthFormatIdentifier,
837 mem_addr: u128,
838 mem_size: u128,
839 dfi: Option<DataFormatIdentifier>,
840 ) -> CanResult<response::RequestDownload, Self::Error> {
841 self.with_context(channel, |listener, iso_tp, did| async move {
842 let data = request::RequestDownload {
843 dfi: dfi.unwrap_or_default(),
844 mem_loc: MemoryLocation::new(alfi, mem_addr, mem_size)
845 .map_err(DoCanError::ISO14229Error)?,
846 };
847 let request = Request::new(Service::RequestDownload, None, data.into(), &did)
848 .map_err(DoCanError::ISO14229Error)?;
849
850 let response =
851 Self::send_and_response(&listener, &iso_tp, &did, AddressType::Physical, request)
852 .await?;
853
854 response
855 .data::<response::RequestDownload>(&did)
856 .map_err(DoCanError::ISO14229Error)
857 })
858 .await
859 }
860
861 async fn request_upload(
862 &mut self,
863 channel: Self::Channel,
864 alfi: AddressAndLengthFormatIdentifier,
865 mem_addr: u128,
866 mem_size: u128,
867 dfi: Option<DataFormatIdentifier>,
868 ) -> CanResult<response::RequestUpload, Self::Error> {
869 self.with_context(channel, |listener, iso_tp, did| async move {
870 let data = request::RequestUpload {
871 dfi: dfi.unwrap_or_default(),
872 mem_loc: MemoryLocation::new(alfi, mem_addr, mem_size)
873 .map_err(DoCanError::ISO14229Error)?,
874 };
875 let request = Request::new(Service::RequestDownload, None, data.into(), &did)
876 .map_err(DoCanError::ISO14229Error)?;
877
878 let response =
879 Self::send_and_response(&listener, &iso_tp, &did, AddressType::Physical, request)
880 .await?;
881
882 response
883 .data::<response::RequestUpload>(&did)
884 .map_err(DoCanError::ISO14229Error)
885 })
886 .await
887 }
888
889 async fn transfer_data(
890 &mut self,
891 channel: Self::Channel,
892 sequence: u8,
893 data: Vec<u8>,
894 ) -> CanResult<response::TransferData, Self::Error> {
895 self.with_context(channel, |listener, iso_tp, did| {
896 let data = data.clone();
897 async move {
898 let data = response::TransferData { sequence, data };
899 let request = Request::new(Service::TransferData, None, data.into(), &did)
900 .map_err(DoCanError::ISO14229Error)?;
901
902 let response = Self::send_and_response(
903 &listener,
904 &iso_tp,
905 &did,
906 AddressType::Physical,
907 request,
908 )
909 .await?;
910
911 let data = response
912 .data::<response::TransferData>(&did)
913 .map_err(DoCanError::ISO14229Error)?;
914
915 if data.sequence != sequence {
916 return Err(DoCanError::UnexpectedTransferSequence {
917 expect: sequence,
918 actual: data.sequence,
919 });
920 }
921
922 Ok(data)
923 }
924 })
925 .await
926 }
927
928 async fn request_transfer_exit(
929 &mut self,
930 channel: Self::Channel,
931 parameter: Vec<u8>,
932 ) -> CanResult<Vec<u8>, Self::Error> {
933 self.with_context(channel, |listener, iso_tp, did| {
934 let parameter = parameter.clone();
935 async move {
936 let request = Request::new(Service::RequestTransferExit, None, parameter, &did)
937 .map_err(DoCanError::ISO14229Error)?;
938
939 let response = Self::send_and_response(
940 &listener,
941 &iso_tp,
942 &did,
943 AddressType::Physical,
944 request,
945 )
946 .await?;
947
948 Ok(response.raw_data().to_vec())
949 }
950 })
951 .await
952 }
953
954 #[cfg(any(feature = "std2013", feature = "std2020"))]
955 async fn request_file_transfer(
956 &mut self,
957 channel: Self::Channel,
958 operation: ModeOfOperation,
959 data: request::RequestFileTransfer,
960 ) -> CanResult<response::RequestFileTransfer, Self::Error> {
961 self.with_context(channel, |listener, iso_tp, did| {
962 let data = data.clone();
963 async move {
964 let service = Service::RequestFileTransfer;
965 let sub_func = operation.into();
966 let request = Request::new(service, Some(sub_func), data.into(), &did)
967 .map_err(DoCanError::ISO14229Error)?;
968
969 let response = Self::send_and_response(
970 &listener,
971 &iso_tp,
972 &did,
973 AddressType::Physical,
974 request,
975 )
976 .await?;
977 Self::sub_func_check(&response, operation.into(), service)?;
978
979 response
980 .data::<response::RequestFileTransfer>(&did)
981 .map_err(DoCanError::ISO14229Error)
982 }
983 })
984 .await
985 }
986}