CrazyflieUSB.cpp
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     if (m_enableLogging) {
00049         logPacket(data, length);
00050     }
00051 
00052     // Send data
00053     status = libusb_bulk_transfer(
00054         m_handle,
00055         /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
00056         (uint8_t*)data,
00057         length,
00058         &transferred,
00059         /*timeout*/ 1000);
00060     if (status != LIBUSB_SUCCESS) {
00061         throw std::runtime_error(libusb_error_name(status));
00062     }
00063     if (length != (uint32_t)transferred) {
00064         std::stringstream sstr;
00065         sstr << "Did transfer " << transferred << " but " << length << " was requested!";
00066         throw std::runtime_error(sstr.str());
00067     }
00068 
00069     // Read result
00070     result.ack = false;
00071     result.size = 0;
00072     status = libusb_bulk_transfer(
00073         m_handle,
00074         /* endpoint*/ (0x81 | LIBUSB_ENDPOINT_IN),
00075         (unsigned char*)&result.data[0],
00076         sizeof(result) - 2,
00077         &transferred,
00078         /*timeout*/ 1000);
00079     if (status != LIBUSB_SUCCESS) {
00080         throw std::runtime_error(libusb_error_name(status));
00081     }
00082     result.ack = true;
00083 
00084     result.size = transferred;
00085 
00086     if (m_enableLogging) {
00087         logAck(result);
00088     }
00089 }
00090 
00091 void CrazyflieUSB::sendPacketNoAck(
00092     const uint8_t* data,
00093     uint32_t length)
00094 {
00095     int status;
00096     int transferred;
00097 
00098     if (!m_handle) {
00099         throw std::runtime_error("No valid device handle!");
00100     }
00101 
00102     if (m_enableLogging) {
00103         logPacket(data, length);
00104     }
00105 
00106     // Send data
00107     status = libusb_bulk_transfer(
00108         m_handle,
00109         /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
00110         (uint8_t*)data,
00111         length,
00112         &transferred,
00113         /*timeout*/ 1000);
00114     if (status != LIBUSB_SUCCESS) {
00115         throw std::runtime_error(libusb_error_name(status));
00116     }
00117     if (length != (uint32_t)transferred) {
00118         std::stringstream sstr;
00119         sstr << "Did transfer " << transferred << " but " << length << " was requested!";
00120         throw std::runtime_error(sstr.str());
00121     }
00122 }
00123 
00124 void CrazyflieUSB::setCrtpToUsb(bool crtpToUsb)
00125 {
00126     sendVendorSetup(0x01, 0x01, crtpToUsb, NULL, 0);
00127 }


crazyflie_cpp
Author(s): Wolfgang Hoenig
autogenerated on Wed Jun 12 2019 19:20:44