CrazyflieUSB.cpp
Go to the documentation of this file.
1 #include "CrazyflieUSB.h"
2 
3 #include <sstream>
4 #include <stdexcept>
5 
6 #include <libusb-1.0/libusb.h>
7 
9  : ITransport()
10  , USBDevice(0x0483, 0x5740)
11 {
12  open(devid);
13  setCrtpToUsb(true);
14 }
15 
17 {
18  setCrtpToUsb(false);
19 }
20 
22 {
23  return USBDevice::numDevices(0x0483, 0x5740);
24 }
25 
26 std::string CrazyflieUSB::serial()
27 {
28  return std::string("TODO");
29 }
30 
31 float CrazyflieUSB::version() const
32 {
33  return m_version;
34 }
35 
37  const uint8_t* data,
38  uint32_t length,
39  Ack& result)
40 {
41  int status;
42  int transferred;
43 
44  if (!m_handle) {
45  throw std::runtime_error("No valid device handle!");
46  }
47 
48  if (m_enableLogging) {
49  logPacket(data, length);
50  }
51 
52  // Send data
53  status = libusb_bulk_transfer(
54  m_handle,
55  /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
56  (uint8_t*)data,
57  length,
58  &transferred,
59  /*timeout*/ 1000);
60  if (status != LIBUSB_SUCCESS) {
61  throw std::runtime_error(libusb_error_name(status));
62  }
63  if (length != (uint32_t)transferred) {
64  std::stringstream sstr;
65  sstr << "Did transfer " << transferred << " but " << length << " was requested!";
66  throw std::runtime_error(sstr.str());
67  }
68 
69  // Read result
70  result.ack = false;
71  result.size = 0;
72  status = libusb_bulk_transfer(
73  m_handle,
74  /* endpoint*/ (0x81 | LIBUSB_ENDPOINT_IN),
75  (unsigned char*)&result.data[0],
76  sizeof(result) - 2,
77  &transferred,
78  /*timeout*/ 1000);
79  if (status != LIBUSB_SUCCESS) {
80  throw std::runtime_error(libusb_error_name(status));
81  }
82  result.ack = true;
83 
84  result.size = transferred;
85 
86  if (m_enableLogging) {
87  logAck(result);
88  }
89 }
90 
92  const uint8_t* data,
93  uint32_t length)
94 {
95  int status;
96  int transferred;
97 
98  if (!m_handle) {
99  throw std::runtime_error("No valid device handle!");
100  }
101 
102  if (m_enableLogging) {
103  logPacket(data, length);
104  }
105 
106  // Send data
107  status = libusb_bulk_transfer(
108  m_handle,
109  /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
110  (uint8_t*)data,
111  length,
112  &transferred,
113  /*timeout*/ 1000);
114  if (status != LIBUSB_SUCCESS) {
115  throw std::runtime_error(libusb_error_name(status));
116  }
117  if (length != (uint32_t)transferred) {
118  std::stringstream sstr;
119  sstr << "Did transfer " << transferred << " but " << length << " was requested!";
120  throw std::runtime_error(sstr.str());
121  }
122 }
123 
124 void CrazyflieUSB::setCrtpToUsb(bool crtpToUsb)
125 {
126  sendVendorSetup(0x01, 0x01, crtpToUsb, NULL, 0);
127 }
CrazyflieUSB(uint32_t devid)
Definition: CrazyflieUSB.cpp:8
float version() const
static uint32_t numDevices(uint16_t idVendor, uint16_t idProduct)
Definition: USBDevice.cpp:37
std::string serial()
void sendVendorSetup(uint8_t request, uint16_t value, uint16_t index, const unsigned char *data, uint16_t length)
Definition: USBDevice.cpp:145
virtual void sendPacketNoAck(const uint8_t *data, uint32_t length)
uint8_t data[32]
Definition: ITransport.h:19
uint8_t length
Definition: crtp.h:22
void logPacket(const uint8_t *data, uint32_t length)
Definition: ITransport.cpp:14
bool m_enableLogging
Definition: ITransport.h:54
uint8_t size
Definition: ITransport.h:21
uint8_t data[29]
Definition: crtp.h:363
uint8_t result
Definition: crtp.h:440
void open(uint32_t devid)
Definition: USBDevice.cpp:77
void setCrtpToUsb(bool crtpToUsb)
void logAck(const Ack &ack)
Definition: ITransport.cpp:31
static uint32_t numDevices()
float m_version
Definition: USBDevice.h:36
uint8_t status
Definition: crtp.h:440
virtual void sendPacket(const uint8_t *data, uint32_t length, ITransport::Ack &result)
libusb_device_handle * m_handle
Definition: USBDevice.h:34


crazyflie_cpp
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:10