diff --git a/media/jni/android_media_MtpServer.cpp b/media/jni/android_media_MtpServer.cpp index 1ef2c581a8c47..33051362e0aa7 100644 --- a/media/jni/android_media_MtpServer.cpp +++ b/media/jni/android_media_MtpServer.cpp @@ -55,7 +55,6 @@ private: MtpDatabase* mDatabase; MtpServer* mServer; String8 mStoragePath; - bool mDone; jobject mJavaServer; public: @@ -63,41 +62,31 @@ public: : mDatabase(database), mServer(NULL), mStoragePath(storagePath), - mDone(false), mJavaServer(javaServer) { } virtual bool threadLoop() { - while (1) { - int fd = open("/dev/mtp_usb", O_RDWR); - printf("open returned %d\n", fd); - if (fd < 0) { - LOGE("could not open MTP driver\n"); - break; - } - - sMutex.lock(); - mServer = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775); - mServer->addStorage(mStoragePath); - sMutex.unlock(); - - LOGD("MtpThread mServer->run"); - mServer->run(); - close(fd); - - sMutex.lock(); - delete mServer; - mServer = NULL; - if (mDone) - goto done; - sMutex.unlock(); - // wait a bit before retrying - sleep(1); + int fd = open("/dev/mtp_usb", O_RDWR); + printf("open returned %d\n", fd); + if (fd < 0) { + LOGE("could not open MTP driver\n"); + return false; } sMutex.lock(); -done: + mServer = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775); + mServer->addStorage(mStoragePath); + sMutex.unlock(); + + LOGD("MtpThread mServer->run"); + mServer->run(); + close(fd); + + sMutex.lock(); + delete mServer; + mServer = NULL; + JNIEnv* env = AndroidRuntime::getJNIEnv(); env->SetIntField(mJavaServer, field_context, 0); env->DeleteGlobalRef(mJavaServer); @@ -107,11 +96,6 @@ done: return false; } - void setDone() { - LOGD("setDone"); - mDone = true; - } - void sendObjectAdded(MtpObjectHandle handle) { sMutex.lock(); if (mServer) @@ -171,11 +155,6 @@ android_media_MtpServer_stop(JNIEnv *env, jobject thiz) { #ifdef HAVE_ANDROID_OS LOGD("stop\n"); - sMutex.lock(); - MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context); - if (thread) - thread->setDone(); - sMutex.unlock(); #endif } diff --git a/media/mtp/MtpDataPacket.cpp b/media/mtp/MtpDataPacket.cpp index 9bfd00f44be88..27dc79663079e 100644 --- a/media/mtp/MtpDataPacket.cpp +++ b/media/mtp/MtpDataPacket.cpp @@ -325,9 +325,12 @@ void MtpDataPacket::putString(const uint16_t* string) { else break; } - putUInt8(count); + putUInt8(count > 0 ? count + 1 : 0); for (int i = 0; i < count; i++) putUInt16(string[i]); + // only terminate with zero if string is not empty + if (count > 0) + putUInt16(0); } #ifdef MTP_DEVICE diff --git a/media/mtp/MtpDataPacket.h b/media/mtp/MtpDataPacket.h index b458286ab89ee..1467aabb07581 100644 --- a/media/mtp/MtpDataPacket.h +++ b/media/mtp/MtpDataPacket.h @@ -83,7 +83,7 @@ public: void putString(const MtpStringBuffer& string); void putString(const char* string); void putString(const uint16_t* string); - inline void putEmptyString() { putUInt16(0); } + inline void putEmptyString() { putUInt8(0); } inline void putEmptyArray() { putUInt32(0); } diff --git a/media/mtp/MtpStringBuffer.cpp b/media/mtp/MtpStringBuffer.cpp index 2d3cf6942790e..8bf673194b031 100644 --- a/media/mtp/MtpStringBuffer.cpp +++ b/media/mtp/MtpStringBuffer.cpp @@ -112,7 +112,7 @@ void MtpStringBuffer::readFromPacket(MtpDataPacket* packet) { void MtpStringBuffer::writeToPacket(MtpDataPacket* packet) const { int count = mCharCount; const uint8_t* src = mBuffer; - packet->putUInt8(count); + packet->putUInt8(count > 0 ? count + 1 : 0); // expand utf8 to 16 bit chars for (int i = 0; i < count; i++) { @@ -133,6 +133,9 @@ void MtpStringBuffer::writeToPacket(MtpDataPacket* packet) const { } packet->putUInt16(ch); } + // only terminate with zero if string is not empty + if (count > 0) + packet->putUInt16(0); } } // namespace android