#include #include #include #include #include "usbdevice.h" #include #include #include "stringutils.h" #include "vectorutils.h" #include "stdio_ipc_clt.h" #define IPCCLIENT stdio_ipc_client // replacement for the windows 'mtty.exe' tool struct copy_pipe2usb { usbdev& _ph; IPCCLIENT& _pp; copy_pipe2usb(usbdev& ph, IPCCLIENT& pp) : _ph(ph), _pp(pp) { } void operator()() { try { ByteVector buf(0x2000); size_t n; while ((n= _pp.readsome(&buf.front(), buf.size()))!=0) { fprintf(stderr, "2usb: %s\n", hexdump(&buf[0], n).c_str()); size_t total= 0; while (total < n) { size_t rn= _ph.bulkwrite(&buf.front()+total, &buf.front()+n); if (rn==size_t(-1)) { fprintf(stderr,"error writing to usb\n"); break; } total += rn; } } } catch(...) { fprintf(stderr, "EXCEPTION in copy_pipe2usb\n"); } } }; struct copy_usb2pipe { usbdev& _ph; IPCCLIENT& _pp; public: copy_usb2pipe(usbdev& ph, IPCCLIENT& pp) : _ph(ph), _pp(pp) { } void operator()() { try { ByteVector buf(0x2000); size_t n; while ((n= _ph.bulkread(buf.begin(), buf.end()))!=0) { fprintf(stderr, "2ppp: %s\n", hexdump(&buf[0], n).c_str()); if (!_pp.write(&buf.front(), n)) { fprintf(stderr,"error writing\n"); break; } } } catch(...) { fprintf(stderr, "EXCEPTION in copy_usb2pipe\n"); } } }; void handle_usbserial(usbdev& ph) { fprintf(stderr,"\nUSB Serial\n"); // note: with the viva only req2+req3 are sent ph.controlread(0x21, 0x22, 0x1, 0); ph.controlread(0x21, 0x22, 0x1, 0); ph.controlread(0x21, 0x22, 0x0, 0); IPCCLIENT pp("-", StringList()); fprintf(stderr,"start copying data\n"); boost::thread p2u(boost::ref(*new copy_pipe2usb(ph,pp))); boost::thread u2p(boost::ref(*new copy_usb2pipe(ph,pp))); ph.controlread(0x21, 0x22, 0x2, 0); ph.controlread(0x21, 0x22, 0x3, 0); fprintf(stderr,"sleep\n"); while(1) sleep(1); } int main(int,char**) { try { usbdev ph; // bootloader ph.support(vidpid(0xbb4,0xa51)); ph.support(vidpid(0xbb4,0xa07)); // viva bootloader ph.support(vidpid(0x45e,0x0ce)); ph.support(vidpid(0xbb4,0x0ce)); while (!ph.connect()) usleep(100); fprintf(stderr,"connected\n"); ph.dumpdesc(); handle_usbserial(ph); } catch(...) { fprintf(stderr,"EXCEPTION\n"); } }