Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be125a50 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

MTP: Add support for sending events to the host when objects are added and removed



Change-Id: Ia1d5232b919c644c670ff9ca651eca92b3f9ad42
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 77ad5e64
Loading
Loading
Loading
Loading
+10 −0
Original line number Original line Diff line number Diff line
@@ -47,6 +47,14 @@ public class MtpServer {
        native_stop();
        native_stop();
    }
    }


    public void sendObjectAdded(int handle) {
        native_send_object_added(handle);
    }

    public void sendObjectRemoved(int handle) {
        native_send_object_removed(handle);
    }

    // used by the JNI code
    // used by the JNI code
    private int mNativeContext;
    private int mNativeContext;


@@ -54,4 +62,6 @@ public class MtpServer {
    private native final void native_finalize();
    private native final void native_finalize();
    private native final void native_start();
    private native final void native_start();
    private native final void native_stop();
    private native final void native_stop();
    private native final void native_send_object_added(int handle);
    private native final void native_send_object_removed(int handle);
}
}
+60 −11
Original line number Original line Diff line number Diff line
@@ -50,12 +50,14 @@ static bool ExceptionCheck(void* env)
class MtpThread : public Thread {
class MtpThread : public Thread {
private:
private:
    MtpDatabase*    mDatabase;
    MtpDatabase*    mDatabase;
    MtpServer*      mServer;
    String8 mStoragePath;
    String8 mStoragePath;
    bool mDone;
    bool mDone;
    Mutex           mMutex;


public:
public:
    MtpThread(MtpDatabase* database, const char* storagePath)
    MtpThread(MtpDatabase* database, const char* storagePath)
        : mDatabase(database), mStoragePath(storagePath), mDone(false)
        : mDatabase(database), mServer(NULL), mStoragePath(storagePath), mDone(false)
    {
    {
    }
    }


@@ -67,14 +69,19 @@ public:
            return false;
            return false;
        }
        }


        MtpServer* server = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775);
        mMutex.lock();
        server->addStorage(mStoragePath);
        mServer = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775);
        mServer->addStorage(mStoragePath);
        mMutex.unlock();


        // temporary
        LOGD("MtpThread mServer->run");
        LOGD("MtpThread server->run");
        mServer->run();
        server->run();
        close(fd);
        close(fd);
        delete server;

        mMutex.lock();
        delete mServer;
        mServer = NULL;
        mMutex.unlock();


        bool done = mDone;
        bool done = mDone;
        if (done)
        if (done)
@@ -84,6 +91,24 @@ public:
    }
    }


    void setDone() { mDone = true; }
    void setDone() { mDone = true; }

    void sendObjectAdded(MtpObjectHandle handle) {
        mMutex.lock();
        if (mServer)
            mServer->sendObjectAdded(handle);
        else
            LOGE("sendObjectAdded called while disconnected\n");
        mMutex.unlock();
    }

    void sendObjectRemoved(MtpObjectHandle handle) {
        mMutex.lock();
        if (mServer)
            mServer->sendObjectRemoved(handle);
        else
            LOGE("sendObjectRemoved called while disconnected\n");
        mMutex.unlock();
    }
};
};


static void
static void
@@ -126,6 +151,28 @@ android_media_MtpServer_stop(JNIEnv *env, jobject thiz)
    }
    }
}
}


static void
android_media_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
{
    LOGD("send_object_added %d\n", handle);
    MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
    if (thread)
        thread->sendObjectAdded(handle);
    else
        LOGE("sendObjectAdded called while disconnected\n");
}

static void
android_media_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
{
    LOGD("send_object_removed %d\n", handle);
    MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
    if (thread)
        thread->sendObjectRemoved(handle);
    else
        LOGE("sendObjectRemoved called while disconnected\n");
}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


static JNINativeMethod gMethods[] = {
static JNINativeMethod gMethods[] = {
@@ -134,6 +181,8 @@ static JNINativeMethod gMethods[] = {
    {"native_finalize",             "()V",  (void *)android_media_MtpServer_finalize},
    {"native_finalize",             "()V",  (void *)android_media_MtpServer_finalize},
    {"native_start",                "()V",  (void *)android_media_MtpServer_start},
    {"native_start",                "()V",  (void *)android_media_MtpServer_start},
    {"native_stop",                 "()V",  (void *)android_media_MtpServer_stop},
    {"native_stop",                 "()V",  (void *)android_media_MtpServer_stop},
    {"native_send_object_added",    "(I)V", (void *)android_media_MtpServer_send_object_added},
    {"native_send_object_removed",  "(I)V", (void *)android_media_MtpServer_send_object_removed},
};
};


static const char* const kClassPathName = "android/media/MtpServer";
static const char* const kClassPathName = "android/media/MtpServer";
+1 −0
Original line number Original line Diff line number Diff line
@@ -23,6 +23,7 @@ LOCAL_SRC_FILES:= \
                  MtpDataPacket.cpp                     \
                  MtpDataPacket.cpp                     \
                  MtpDebug.cpp                          \
                  MtpDebug.cpp                          \
                  MtpDevice.cpp                         \
                  MtpDevice.cpp                         \
                  MtpEventPacket.cpp                    \
                  MtpDeviceInfo.cpp                     \
                  MtpDeviceInfo.cpp                     \
                  MtpObjectInfo.cpp                     \
                  MtpObjectInfo.cpp                     \
                  MtpPacket.cpp                         \
                  MtpPacket.cpp                         \
+65 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "MtpEventPacket"

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <f_mtp.h>

#include "MtpEventPacket.h"

namespace android {

MtpEventPacket::MtpEventPacket()
    :   MtpPacket(512)
{
}

MtpEventPacket::~MtpEventPacket() {
}

#ifdef MTP_DEVICE
int MtpEventPacket::write(int fd) {
    struct mtp_event    event;

    putUInt32(MTP_CONTAINER_LENGTH_OFFSET, mPacketSize);
    putUInt16(MTP_CONTAINER_TYPE_OFFSET, MTP_CONTAINER_TYPE_EVENT);

    event.data = mBuffer;
    event.length = mPacketSize;
    int ret = ::ioctl(fd, MTP_SEND_EVENT, (unsigned long)&event);
    return (ret < 0 ? ret : 0);
}
#endif

#ifdef MTP_HOST
    // read our buffer from the given endpoint
int MtpEventPacket::read(struct usb_endpoint *ep) {
    int ret = transfer(ep, mBuffer, mBufferSize);
     if (ret >= 0)
        mPacketSize = ret;
    else
        mPacketSize = 0;
    return ret;
}
#endif

}  // namespace android
+48 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _MTP_EVENT_PACKET_H
#define _MTP_EVENT_PACKET_H

#include "MtpPacket.h"
#include "mtp.h"

namespace android {

class MtpEventPacket : public MtpPacket {

public:
                        MtpEventPacket();
    virtual             ~MtpEventPacket();

#ifdef MTP_DEVICE
    // write our data to the given file descriptor
    int                 write(int fd);
#endif

#ifdef MTP_HOST
    // read our buffer from the given endpoint
    int                 read(struct usb_endpoint *ep);
#endif

    inline MtpEventCode     getEventCode() const { return getContainerCode(); }
    inline void             setEventCode(MtpEventCode code)
                                                     { return setContainerCode(code); }
};

}; // namespace android

#endif // _MTP_EVENT_PACKET_H
Loading