libyb / test / shupito_flash.cpp

#include "test.h"
#include <libyb/async/serial_port.hpp>
#include <libyb/async/stream_device.hpp>
#include <libyb/async/sync_runner.hpp>
#include <libyb/async/descriptor_reader.hpp>
#include <libyb/tunnel.hpp>
#include <libyb/usb/usb.h>
#include <libyb/usb/bulk_stream.hpp>
#include <libyb/shupito/escape_sequence.hpp>

TEST_CASE(ShupitoFlash, "+shupito")
{
	yb::usb_context usb;
	std::vector<yb::usb_device> devs = usb.get_device_list();

	yb::usb_device * selected = 0;
	for (size_t i = 0; i < devs.size(); ++i)
	{
		yb::usb_device_descriptor desc = devs[i].descriptor();
		if (desc.idVendor == 0x4a61 && desc.idProduct == 0x679a)
		{
			selected = &devs[i];
			break;
		}
	}

	yb::usb_bulk_stream bs;
	yb::usb_interface_guard ig;
	yb::serial_port sp;

	yb::sync_runner r;

	if (selected)
	{
		yb::usb_config_descriptor cd = selected->get_config_descriptor();
		r << bs.claim_and_open(*selected, ig, cd);
	}

	if (bs.is_readable())
		r |= discard(bs);

	r << sp.open("COM3", 38400);

	yb::stream_device dev;
	yb::sync_future<void> f = r | dev.run(sp);

	yb::device_descriptor dd = r << yb::read_device_descriptor(dev);

	yb::tunnel_handler th;
	bool attached = th.attach(dev, dd);

	yb::tunnel_stream ts;
	r << ts.open(th, "usb");
	r << send_avr232boot_escape_seq(ts, 1000);

	static uint8_t const unescape_seq[] = { 0x11 };
	r << ts.write_all(unescape_seq, sizeof unescape_seq);

	r << ts.fast_close();

	f.wait(yb::cl_quit);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.