summaryrefslogtreecommitdiffstats
path: root/media/mtp/MtpClient.cpp
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2010-07-01 11:32:08 -0400
committerMike Lockwood <lockwood@android.com>2010-07-01 11:39:42 -0400
commitcff0ef94eccba9ce2f72380a238793f1702474b7 (patch)
treeb1775a7417a3d798d70d3ed2ee011a06c83c2fdf /media/mtp/MtpClient.cpp
parent30cb2dd954f60047987b11020703c3bef1f8749f (diff)
downloadframeworks_av-cff0ef94eccba9ce2f72380a238793f1702474b7.zip
frameworks_av-cff0ef94eccba9ce2f72380a238793f1702474b7.tar.gz
frameworks_av-cff0ef94eccba9ce2f72380a238793f1702474b7.tar.bz2
MTP: Remove an unnecessary thread from the MtpClient class.
Now a single thread is used for passing USB host events up to MtpClient. Change-Id: I0e3a277956cb3d1036da122ea10acb03a27844d6 Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'media/mtp/MtpClient.cpp')
-rw-r--r--media/mtp/MtpClient.cpp67
1 files changed, 50 insertions, 17 deletions
diff --git a/media/mtp/MtpClient.cpp b/media/mtp/MtpClient.cpp
index fbf1c77..b4c47af 100644
--- a/media/mtp/MtpClient.cpp
+++ b/media/mtp/MtpClient.cpp
@@ -27,6 +27,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
+#include <utils/threads.h>
#include <usbhost/usbhost.h>
#include <linux/version.h>
@@ -38,34 +39,59 @@
namespace android {
+class MtpClientThread : public Thread {
+private:
+ MtpClient* mClient;
+
+public:
+ MtpClientThread(MtpClient* client)
+ : mClient(client)
+ {
+ }
+
+ virtual bool threadLoop() {
+ return mClient->threadLoop();
+ }
+};
+
+
MtpClient::MtpClient()
- : mStarted(false)
+ : mThread(NULL),
+ mUsbHostContext(NULL),
+ mDone(false)
{
}
MtpClient::~MtpClient() {
+ usb_host_cleanup(mUsbHostContext);
}
bool MtpClient::start() {
- if (mStarted)
+ if (mThread)
return true;
- if (usb_host_init(usb_device_added, usb_device_removed, this)) {
- LOGE("MtpClient::start failed\n");
+ mUsbHostContext = usb_host_init();
+ if (!mUsbHostContext)
return false;
- }
- mStarted = true;
+
+ mThread = new MtpClientThread(this);
+ mThread->run("MtpClientThread");
+
return true;
}
-void MtpClient::usbDeviceAdded(const char *devname) {
+void MtpClient::stop() {
+ mDone = true;
+}
+
+bool MtpClient::usbDeviceAdded(const char *devname) {
struct usb_descriptor_header* desc;
struct usb_descriptor_iter iter;
struct usb_device *device = usb_device_open(devname);
if (!device) {
LOGE("usb_device_open failed\n");
- return;
+ return mDone;
}
usb_descriptor_iter_init(device, &iter);
@@ -90,7 +116,7 @@ void MtpClient::usbDeviceAdded(const char *devname) {
ep = (struct usb_endpoint_descriptor *)usb_descriptor_iter_next(&iter);
if (!ep || ep->bDescriptorType != USB_DT_ENDPOINT) {
LOGE("endpoints not found\n");
- return;
+ return mDone;
}
if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK) {
if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
@@ -104,7 +130,7 @@ void MtpClient::usbDeviceAdded(const char *devname) {
}
if (!ep_in_desc || !ep_out_desc || !ep_intr_desc) {
LOGE("endpoints not found\n");
- return;
+ return mDone;
}
struct usb_endpoint *ep_in = usb_endpoint_open(device, ep_in_desc);
@@ -116,7 +142,7 @@ void MtpClient::usbDeviceAdded(const char *devname) {
usb_endpoint_close(ep_in);
usb_endpoint_close(ep_out);
usb_endpoint_close(ep_intr);
- return;
+ return mDone;
}
MtpDevice* mtpDevice = new MtpDevice(device, interface->bInterfaceNumber,
@@ -124,12 +150,13 @@ void MtpClient::usbDeviceAdded(const char *devname) {
mDeviceList.add(mtpDevice);
mtpDevice->initialize();
deviceAdded(mtpDevice);
- return;
+ return mDone;
}
}
}
usb_device_close(device);
+ return mDone;
}
MtpDevice* MtpClient::getDevice(int id) {
@@ -141,7 +168,7 @@ MtpDevice* MtpClient::getDevice(int id) {
return NULL;
}
-void MtpClient::usbDeviceRemoved(const char *devname) {
+bool MtpClient::usbDeviceRemoved(const char *devname) {
for (int i = 0; i < mDeviceList.size(); i++) {
MtpDevice* device = mDeviceList[i];
if (!strcmp(devname, device->getDeviceName())) {
@@ -152,16 +179,22 @@ void MtpClient::usbDeviceRemoved(const char *devname) {
break;
}
}
+ return mDone;
+}
+
+bool MtpClient::threadLoop() {
+ usb_host_run(mUsbHostContext, usb_device_added, usb_device_removed, this);
+ return false;
}
-void MtpClient::usb_device_added(const char *devname, void* client_data) {
+int MtpClient::usb_device_added(const char *devname, void* client_data) {
LOGD("usb_device_added %s\n", devname);
- ((MtpClient *)client_data)->usbDeviceAdded(devname);
+ return ((MtpClient *)client_data)->usbDeviceAdded(devname);
}
-void MtpClient::usb_device_removed(const char *devname, void* client_data) {
+int MtpClient::usb_device_removed(const char *devname, void* client_data) {
LOGD("usb_device_removed %s\n", devname);
- ((MtpClient *)client_data)->usbDeviceRemoved(devname);
+ return ((MtpClient *)client_data)->usbDeviceRemoved(devname);
}
} // namespace android