fastboot: create Transport object (take 2).
(Second upload of this CL; original upload had the wrong version of
usb_windows.cpp that caused a compilation error. Fixed error and
re-tested.)
This CL creates a Transport object to provide a generic interface for
various transports. Specifically this is designed to be able to add UDP
support to fastboot in an upcoming CL without changing the main program
logic.
Also includes some minor code style fixes and replaces malloc/free
in the USB implementation files with smart pointers and std::string.
Bug: http://b/22029765
Change-Id: I1175bbce08690fbd15f51e68166be9b3e9973ea0
diff --git a/fastboot/usb_osx.cpp b/fastboot/usb_osx.cpp
index 45ae833..ee5d575 100644
--- a/fastboot/usb_osx.cpp
+++ b/fastboot/usb_osx.cpp
@@ -35,6 +35,8 @@
#include <IOKit/IOMessage.h>
#include <mach/mach_port.h>
+#include <memory>
+
#include "usb.h"
@@ -63,6 +65,21 @@
unsigned int zero_mask;
};
+class OsxUsbTransport : public Transport {
+ public:
+ OsxUsbTransport(std::unique_ptr<usb_handle> handle) : handle_(std::move(handle)) {}
+ ~OsxUsbTransport() override = default;
+
+ ssize_t Read(void* data, size_t len) override;
+ ssize_t Write(const void* data, size_t len) override;
+ int Close() override;
+
+ private:
+ std::unique_ptr<usb_handle> handle_;
+
+ DISALLOW_COPY_AND_ASSIGN(OsxUsbTransport);
+};
+
/** Try out all the interfaces and see if there's a match. Returns 0 on
* success, -1 on failure. */
static int try_interfaces(IOUSBDeviceInterface182 **dev, usb_handle *handle) {
@@ -390,7 +407,7 @@
/** Initializes the USB system. Returns 0 on success, -1 on error. */
-static int init_usb(ifc_match_func callback, usb_handle **handle) {
+static int init_usb(ifc_match_func callback, std::unique_ptr<usb_handle>* handle) {
int ret = -1;
CFMutableDictionaryRef matchingDict;
kern_return_t result;
@@ -443,8 +460,8 @@
}
if (h.success) {
- *handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle)));
- memcpy(*handle, &h, sizeof(usb_handle));
+ handle->reset(new usb_handle);
+ memcpy(handle->get(), &h, sizeof(usb_handle));
ret = 0;
break;
}
@@ -463,28 +480,23 @@
* Definitions of this file's public functions.
*/
-usb_handle *usb_open(ifc_match_func callback) {
- usb_handle *handle = NULL;
+Transport* usb_open(ifc_match_func callback) {
+ std::unique_ptr<usb_handle> handle;
if (init_usb(callback, &handle) < 0) {
/* Something went wrong initializing USB. */
- return NULL;
+ return nullptr;
}
- return handle;
+ return new OsxUsbTransport(std::move(handle));
}
-int usb_close(usb_handle *h) {
+int OsxUsbTransport::Close() {
/* TODO: Something better here? */
return 0;
}
-int usb_wait_for_disconnect(usb_handle *usb) {
- /* TODO: Punt for now */
- return 0;
-}
-
-int usb_read(usb_handle *h, void *data, int len) {
+ssize_t OsxUsbTransport::Read(void* data, size_t len) {
IOReturn result;
UInt32 numBytes = len;
@@ -492,22 +504,21 @@
return 0;
}
- if (h == NULL) {
+ if (handle_ == nullptr) {
return -1;
}
- if (h->interface == NULL) {
+ if (handle_->interface == nullptr) {
ERR("usb_read interface was null\n");
return -1;
}
- if (h->bulkIn == 0) {
+ if (handle_->bulkIn == 0) {
ERR("bulkIn endpoint not assigned\n");
return -1;
}
- result = (*h->interface)->ReadPipe(
- h->interface, h->bulkIn, data, &numBytes);
+ result = (*handle_->interface)->ReadPipe(handle_->interface, handle_->bulkIn, data, &numBytes);
if (result == 0) {
return (int) numBytes;
@@ -518,30 +529,30 @@
return -1;
}
-int usb_write(usb_handle *h, const void *data, int len) {
+ssize_t OsxUsbTransport::Write(const void* data, size_t len) {
IOReturn result;
if (len == 0) {
return 0;
}
- if (h == NULL) {
+ if (handle_ == NULL) {
return -1;
}
- if (h->interface == NULL) {
+ if (handle_->interface == NULL) {
ERR("usb_write interface was null\n");
return -1;
}
- if (h->bulkOut == 0) {
+ if (handle_->bulkOut == 0) {
ERR("bulkOut endpoint not assigned\n");
return -1;
}
#if 0
- result = (*h->interface)->WritePipe(
- h->interface, h->bulkOut, (void *)data, len);
+ result = (*handle_->interface)->WritePipe(
+ handle_->interface, handle_->bulkOut, (void *)data, len);
#else
/* Attempt to work around crashes in the USB driver that may be caused
* by trying to write too much data at once. The kernel IOCopyMapper
@@ -554,8 +565,8 @@
int lenToSend = lenRemaining > maxLenToSend
? maxLenToSend : lenRemaining;
- result = (*h->interface)->WritePipe(
- h->interface, h->bulkOut, (void *)data, lenToSend);
+ result = (*handle_->interface)->WritePipe(
+ handle_->interface, handle_->bulkOut, (void *)data, lenToSend);
if (result != 0) break;
lenRemaining -= lenToSend;
@@ -564,11 +575,11 @@
#endif
#if 0
- if ((result == 0) && (h->zero_mask)) {
+ if ((result == 0) && (handle_->zero_mask)) {
/* we need 0-markers and our transfer */
- if(!(len & h->zero_mask)) {
- result = (*h->interface)->WritePipe(
- h->interface, h->bulkOut, (void *)data, 0);
+ if(!(len & handle_->zero_mask)) {
+ result = (*handle_->interface)->WritePipe(
+ handle_->interface, handle_->bulkOut, (void *)data, 0);
}
}
#endif