blob: 6a1c64b0dca9a94e542616a5c28026f9d91b9d0f [file] [log] [blame]
Mike Lockwood98ef64e2010-06-29 16:42:13 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "MtpServerJNI"
18#include "utils/Log.h"
19
20#include <stdio.h>
21#include <assert.h>
22#include <limits.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <utils/threads.h>
26
27#include "jni.h"
28#include "JNIHelp.h"
29#include "android_runtime/AndroidRuntime.h"
Mike Lockwooddad69272010-07-02 15:15:07 -040030#include "private/android_filesystem_config.h"
Mike Lockwood98ef64e2010-06-29 16:42:13 -040031
32#include "MtpServer.h"
Mike Lockwoodd21eac92010-07-03 00:44:05 -040033#include "MtpSqliteDatabase.h" // REMOVE
Mike Lockwood98ef64e2010-06-29 16:42:13 -040034
Mike Lockwood81ea83d2010-06-30 17:49:41 -040035using namespace android;
Mike Lockwood98ef64e2010-06-29 16:42:13 -040036
37// ----------------------------------------------------------------------------
38
39static jfieldID field_context;
40
Mike Lockwoodd21eac92010-07-03 00:44:05 -040041// in android_media_MtpDatabase.cpp
42extern MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040043
44// ----------------------------------------------------------------------------
45
46static bool ExceptionCheck(void* env)
47{
48 return ((JNIEnv *)env)->ExceptionCheck();
49}
50
51class MtpThread : public Thread {
52private:
Mike Lockwoodd21eac92010-07-03 00:44:05 -040053 MtpDatabase* mDatabase;
Mike Lockwood98ef64e2010-06-29 16:42:13 -040054 String8 mStoragePath;
Mike Lockwood98ef64e2010-06-29 16:42:13 -040055 bool mDone;
Mike Lockwood98ef64e2010-06-29 16:42:13 -040056
57public:
Mike Lockwoodd21eac92010-07-03 00:44:05 -040058 MtpThread(MtpDatabase* database, const char* storagePath)
59 : mDatabase(database), mStoragePath(storagePath), mDone(false)
Mike Lockwood98ef64e2010-06-29 16:42:13 -040060 {
61 }
62
63 virtual bool threadLoop() {
64 int fd = open("/dev/mtp_usb", O_RDWR);
65 printf("open returned %d\n", fd);
66 if (fd < 0) {
67 LOGE("could not open MTP driver\n");
68 return false;
69 }
70
Mike Lockwoodd21eac92010-07-03 00:44:05 -040071 MtpServer* server = new MtpServer(fd, mDatabase, AID_SDCARD_RW, 0664, 0775);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040072 server->addStorage(mStoragePath);
73
74 // temporary
Mike Lockwood98ef64e2010-06-29 16:42:13 -040075 LOGD("MtpThread server->run");
76 server->run();
77 close(fd);
78 delete server;
79
80 bool done = mDone;
81 if (done)
82 delete this;
83 LOGD("threadLoop returning %s", (done ? "false" : "true"));
84 return !done;
85 }
86
87 void setDone() { mDone = true; }
88};
89
90static void
Mike Lockwoodd21eac92010-07-03 00:44:05 -040091android_media_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jstring storagePath)
Mike Lockwood98ef64e2010-06-29 16:42:13 -040092{
93 LOGD("setup\n");
94
Mike Lockwoodd21eac92010-07-03 00:44:05 -040095 MtpDatabase* database = getMtpDatabase(env, javaDatabase);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040096 const char *storagePathStr = env->GetStringUTFChars(storagePath, NULL);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040097
Mike Lockwoodd21eac92010-07-03 00:44:05 -040098 MtpThread* thread = new MtpThread(database, storagePathStr);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040099 env->SetIntField(thiz, field_context, (int)thread);
100
101 env->ReleaseStringUTFChars(storagePath, storagePathStr);
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400102}
103
104static void
105android_media_MtpServer_finalize(JNIEnv *env, jobject thiz)
106{
107 LOGD("finalize\n");
108}
109
110
111static void
112android_media_MtpServer_start(JNIEnv *env, jobject thiz)
113{
114 LOGD("start\n");
115 MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
116 thread->run("MtpThread");
117}
118
119static void
120android_media_MtpServer_stop(JNIEnv *env, jobject thiz)
121{
122 LOGD("stop\n");
123 MtpThread *thread = (MtpThread *)env->GetIntField(thiz, field_context);
124 if (thread) {
125 thread->setDone();
126 env->SetIntField(thiz, field_context, 0);
127 }
128}
129
130// ----------------------------------------------------------------------------
131
132static JNINativeMethod gMethods[] = {
Mike Lockwoodd21eac92010-07-03 00:44:05 -0400133 {"native_setup", "(Landroid/media/MtpDatabase;Ljava/lang/String;)V",
134 (void *)android_media_MtpServer_setup},
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400135 {"native_finalize", "()V", (void *)android_media_MtpServer_finalize},
136 {"native_start", "()V", (void *)android_media_MtpServer_start},
137 {"native_stop", "()V", (void *)android_media_MtpServer_stop},
138};
139
140static const char* const kClassPathName = "android/media/MtpServer";
141
142int register_android_media_MtpServer(JNIEnv *env)
143{
144 jclass clazz;
145
146 LOGD("register_android_media_MtpServer\n");
147
148 clazz = env->FindClass("android/media/MtpServer");
149 if (clazz == NULL) {
150 LOGE("Can't find android/media/MtpServer");
151 return -1;
152 }
153 field_context = env->GetFieldID(clazz, "mNativeContext", "I");
154 if (field_context == NULL) {
155 LOGE("Can't find MtpServer.mNativeContext");
156 return -1;
157 }
158
159 return AndroidRuntime::registerNativeMethods(env,
160 "android/media/MtpServer", gMethods, NELEM(gMethods));
161}