Go to the documentation of this file.00001 #include "CrazyflieUSB.h"
00002
00003 #include <sstream>
00004 #include <stdexcept>
00005
00006 #include <libusb-1.0/libusb.h>
00007
00008 CrazyflieUSB::CrazyflieUSB(uint32_t devid)
00009 : ITransport()
00010 , USBDevice(0x0483, 0x5740)
00011 {
00012 open(devid);
00013 setCrtpToUsb(true);
00014 }
00015
00016 CrazyflieUSB::~CrazyflieUSB()
00017 {
00018 setCrtpToUsb(false);
00019 }
00020
00021 uint32_t CrazyflieUSB::numDevices()
00022 {
00023 return USBDevice::numDevices(0x0483, 0x5740);
00024 }
00025
00026 std::string CrazyflieUSB::serial()
00027 {
00028 return std::string("TODO");
00029 }
00030
00031 float CrazyflieUSB::version() const
00032 {
00033 return m_version;
00034 }
00035
00036 void CrazyflieUSB::sendPacket(
00037 const uint8_t* data,
00038 uint32_t length,
00039 Ack& result)
00040 {
00041 int status;
00042 int transferred;
00043
00044 if (!m_handle) {
00045 throw std::runtime_error("No valid device handle!");
00046 }
00047
00048
00049 status = libusb_bulk_transfer(
00050 m_handle,
00051 (0x01 | LIBUSB_ENDPOINT_OUT),
00052 (uint8_t*)data,
00053 length,
00054 &transferred,
00055 1000);
00056 if (status != LIBUSB_SUCCESS) {
00057 throw std::runtime_error(libusb_error_name(status));
00058 }
00059 if (length != transferred) {
00060 std::stringstream sstr;
00061 sstr << "Did transfer " << transferred << " but " << length << " was requested!";
00062 throw std::runtime_error(sstr.str());
00063 }
00064
00065
00066 result.ack = false;
00067 result.size = 0;
00068 status = libusb_bulk_transfer(
00069 m_handle,
00070 (0x81 | LIBUSB_ENDPOINT_IN),
00071 (unsigned char*)&result.data[0],
00072 sizeof(result) - 2,
00073 &transferred,
00074 1000);
00075 if (status != LIBUSB_SUCCESS) {
00076 throw std::runtime_error(libusb_error_name(status));
00077 }
00078 result.ack = true;
00079
00080 result.size = transferred;
00081 }
00082
00083 void CrazyflieUSB::sendPacketNoAck(
00084 const uint8_t* data,
00085 uint32_t length)
00086 {
00087 int status;
00088 int transferred;
00089
00090 if (!m_handle) {
00091 throw std::runtime_error("No valid device handle!");
00092 }
00093
00094
00095 status = libusb_bulk_transfer(
00096 m_handle,
00097 (0x01 | LIBUSB_ENDPOINT_OUT),
00098 (uint8_t*)data,
00099 length,
00100 &transferred,
00101 1000);
00102 if (status != LIBUSB_SUCCESS) {
00103 throw std::runtime_error(libusb_error_name(status));
00104 }
00105 if (length != transferred) {
00106 std::stringstream sstr;
00107 sstr << "Did transfer " << transferred << " but " << length << " was requested!";
00108 throw std::runtime_error(sstr.str());
00109 }
00110 }
00111
00112 void CrazyflieUSB::setCrtpToUsb(bool crtpToUsb)
00113 {
00114 sendVendorSetup(0x01, 0x01, crtpToUsb, NULL, 0);
00115 }