pub fn new_fake_spinel_pair() -> (
    SpinelDeviceSink<MockDeviceProxy>,
    SpinelDeviceStream<MockDeviceProxy, mpsc::Receiver<Result<SpinelDeviceEvent, fidl::Error>>>,
    BoxFuture<'static, ()>,
) {
    let (device_sink, device_stream, mut device_event_sender, mut device_request_receiver) =
        new_mock_spinel_pair(2000);

    let fake_device = FakeSpinelDevice::default();

    let ncp_task = async move {
        let outbound_frames_remaining = FlowWindow::default();
        let mut inbound_frames_remaining = 0;
        let mut is_open = false;
        let mut did_send_reset = false;

        traceln!("new_fake_spinel_device[ncp_task]: Start.");

        while let Some(request) = device_request_receiver.next().await {
            match request {
                DeviceRequest::Open => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got Open");
                    is_open = true;

                    traceln!(
                        "new_fake_spinel_device[ncp_task]: Sending SpinelDeviceEvent::OnReadyForSendFrames"
                    );
                    let event = SpinelDeviceEvent::OnReadyForSendFrames {
                        number_of_frames: INBOUND_WINDOW_SIZE,
                    };
                    assert_matches!(device_event_sender.start_send(Ok(event)), Ok(_));
                    inbound_frames_remaining = INBOUND_WINDOW_SIZE;
                    did_send_reset = false;
                    outbound_frames_remaining.reset();
                }
                DeviceRequest::Close => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got Close");
                    is_open = false;
                    outbound_frames_remaining.reset();
                }
                DeviceRequest::GetMaxFrameSize => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got GetMaxFrameSize");
                }
                DeviceRequest::SendFrame(frame) => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got SendFrame");
                    assert!(inbound_frames_remaining != 0, "inbound_frames_remaining == 0");
                    inbound_frames_remaining -= 1;

                    if frame == [129u8, 1u8] {
                        traceln!("new_fake_spinel_device[ncp_task]: Got software reset command!");
                        fake_device.on_reset();

                        outbound_frames_remaining.dec(1).await;
                        assert_matches!(
                            device_event_sender.start_send(Ok(SpinelDeviceEvent::OnReceiveFrame {
                                data: vec![0x80, 0x06, 0x00, 113]
                            })),
                            Ok(_)
                        );
                        did_send_reset = true;
                    } else {
                        for response in fake_device.handle_command(frame.as_slice()) {
                            outbound_frames_remaining.dec(1).await;
                            assert_matches!(
                                device_event_sender.start_send(Ok(
                                    SpinelDeviceEvent::OnReceiveFrame { data: response }
                                )),
                                Ok(_)
                            );
                        }
                    }

                    let event = SpinelDeviceEvent::OnReadyForSendFrames { number_of_frames: 1 };
                    assert_matches!(device_event_sender.start_send(Ok(event)), Ok(_));
                    inbound_frames_remaining += 1;
                }
                DeviceRequest::ReadyToReceiveFrames(n) => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got ReadyToReceiveFrames");
                    assert!(is_open);
                    outbound_frames_remaining.inc(n);
                    if !did_send_reset && outbound_frames_remaining.dec(1).now_or_never().is_some()
                    {
                        assert_matches!(
                            device_event_sender.start_send(Ok(SpinelDeviceEvent::OnReceiveFrame {
                                data: vec![0x80, 0x06, 0x00, 113]
                            })),
                            Ok(_)
                        );
                        did_send_reset = true;
                    }
                }
            }
        }
    };

    (device_sink, device_stream, ncp_task.boxed())
}
/// Returns a fake spinel device client and a future
/// to run in the background.
///
/// This is only used for testing.
Returns a fake spinel device client and a future
to run in the background.

This is only used for testing.
Returns a fake spinel device client and a future to run in the background . This is only used for testing .
pub fn new_fake_spinel_pair() -> (
    SpinelDeviceSink<MockDeviceProxy>,
    SpinelDeviceStream<MockDeviceProxy, mpsc::Receiver<Result<SpinelDeviceEvent, fidl::Error>>>,
    BoxFuture<'static, ()>,
) {
    let (device_sink, device_stream, mut device_event_sender, mut device_request_receiver) =
        new_mock_spinel_pair(2000);
    let fake_device = FakeSpinelDevice::default();
    let ncp_task = async move {
        let outbound_frames_remaining = FlowWindow::default();
        let mut inbound_frames_remaining = 0;
        let mut is_open = false;
        let mut did_send_reset = false;
        traceln!("new_fake_spinel_device[ncp_task]: Start.");
        while let Some(request) = device_request_receiver.next().await {
            match request {
                DeviceRequest::Open => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got Open");
                    is_open = true;
                    traceln!(
                        "new_fake_spinel_device[ncp_task]: Sending SpinelDeviceEvent::OnReadyForSendFrames"
                    );
                    let event = SpinelDeviceEvent::OnReadyForSendFrames {
                        number_of_frames: INBOUND_WINDOW_SIZE,
                    };
                    assert_matches!(device_event_sender.start_send(Ok(event)), Ok(_));
                    inbound_frames_remaining = INBOUND_WINDOW_SIZE;
                    did_send_reset = false;
                    outbound_frames_remaining.reset();
                }
                DeviceRequest::Close => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got Close");
                    is_open = false;
                    outbound_frames_remaining.reset();
                }
                DeviceRequest::GetMaxFrameSize => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got GetMaxFrameSize");
                }
                DeviceRequest::SendFrame(frame) => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got SendFrame");
                    assert!(inbound_frames_remaining != 0, "inbound_frames_remaining == 0");
                    inbound_frames_remaining -= 1;
                    if frame == [129u8, 1u8] {
                        traceln!("new_fake_spinel_device[ncp_task]: Got software reset command!");
                        fake_device.on_reset();
                        outbound_frames_remaining.dec(1).await;
                        assert_matches!(
                            device_event_sender.start_send(Ok(SpinelDeviceEvent::OnReceiveFrame {
                                data: vec![0x80, 0x06, 0x00, 113]
                            })),
                            Ok(_)
                        );
                        did_send_reset = true;
                    } else {
                        for response in fake_device.handle_command(frame.as_slice()) {
                            outbound_frames_remaining.dec(1).await;
                            assert_matches!(
                                device_event_sender.start_send(Ok(
                                    SpinelDeviceEvent::OnReceiveFrame { data: response }
                                )),
                                Ok(_)
                            );
                        }
                    }
                    let event = SpinelDeviceEvent::OnReadyForSendFrames { number_of_frames: 1 };
                    assert_matches!(device_event_sender.start_send(Ok(event)), Ok(_));
                    inbound_frames_remaining += 1;
                }
                DeviceRequest::ReadyToReceiveFrames(n) => {
                    traceln!("new_fake_spinel_device[ncp_task]: Got ReadyToReceiveFrames");
                    assert!(is_open);
                    outbound_frames_remaining.inc(n);
                    if !did_send_reset && outbound_frames_remaining.dec(1).now_or_never().is_some()
                    {
                        assert_matches!(
                            device_event_sender.start_send(Ok(SpinelDeviceEvent::OnReceiveFrame {
                                data: vec![0x80, 0x06, 0x00, 113]
                            })),
                            Ok(_)
                        );
                        did_send_reset = true;
                    }
                }
            }
        }
    };
    (device_sink, device_stream, ncp_task.boxed())
}
pub fn new_fake_spinel_pair ( ) -> ( SpinelDeviceSink < MockDeviceProxy > , SpinelDeviceStream < MockDeviceProxy , mpsc :: Receiver < Result < SpinelDeviceEvent , fidl :: Error > > > , BoxFuture < ' static , ( ) > , ) { let ( device_sink , device_stream , mut device_event_sender , mut device_request_receiver ) = new_mock_spinel_pair ( 2000 ) ; let fake_device = FakeSpinelDevice :: default ( ) ; let ncp_task = async move { let outbound_frames_remaining = FlowWindow :: default ( ) ; let mut inbound_frames_remaining = 0 ; let mut is_open = false ; let mut did_send_reset = false ; traceln ! ( " " ) ; while let Some ( request ) = device_request_receiver . next ( ) . await { match request { DeviceRequest :: Open => { traceln ! ( " " ) ; is_open = true ; traceln ! ( " " ) ; let event = SpinelDeviceEvent :: OnReadyForSendFrames { number_of_frames : INBOUND_WINDOW_SIZE , } ; assert_matches ! ( device_event_sender start_send ( Ok ( event ) ) Ok ( ) ) ; inbound_frames_remaining = INBOUND_WINDOW_SIZE ; did_send_reset = false ; outbound_frames_remaining . reset ( ) ; } DeviceRequest :: Close => { traceln ! ( " " ) ; is_open = false ; outbound_frames_remaining . reset ( ) ; } DeviceRequest :: GetMaxFrameSize => { traceln ! ( " " ) ; } DeviceRequest :: SendFrame ( frame ) => { traceln ! ( " " ) ; assert ! ( inbound_frames_remaining 0 " " ) ; inbound_frames_remaining -= 1 ; if frame == [ 129u8 , 1u8 ] { traceln ! ( " " ) ; fake_device . on_reset ( ) ; outbound_frames_remaining . dec ( 1 ) . await ; assert_matches ! ( device_event_sender start_send ( Ok ( SpinelDeviceEvent OnReceiveFrame { data vec [ 0x80 0x06 0x00 113 ] } ) ) Ok ( ) ) ; did_send_reset = true ; } else { for response in fake_device . handle_command ( frame . as_slice ( ) ) { outbound_frames_remaining . dec ( 1 ) . await ; assert_matches ! ( device_event_sender start_send ( Ok ( SpinelDeviceEvent OnReceiveFrame { data response } ) ) Ok ( ) ) ; } } let event = SpinelDeviceEvent :: OnReadyForSendFrames { number_of_frames : 1 } ; assert_matches ! ( device_event_sender start_send ( Ok ( event ) ) Ok ( ) ) ; inbound_frames_remaining += 1 ; } DeviceRequest :: ReadyToReceiveFrames ( n ) => { traceln ! ( " " ) ; assert ! ( is_open ) ; outbound_frames_remaining . inc ( n ) ; if ! did_send_reset && outbound_frames_remaining . dec ( 1 ) . now_or_never ( ) . is_some ( ) { assert_matches ! ( device_event_sender start_send ( Ok ( SpinelDeviceEvent OnReceiveFrame { data vec [ 0x80 0x06 0x00 113 ] } ) ) Ok ( ) ) ; did_send_reset = true ; } } } } } ; ( device_sink , device_stream , ncp_task . boxed ( ) ) }
Returns a fake spinel device client and a future
to run in the background.
Returns a fake spinel device client and a future to run in the background .