blob: afd3082ec51b33d50a00b85923bf3bd61690908c [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 Lockwood467ca0d2011-02-18 09:07:14 -050033#include "MtpStorage.h"
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
Mike Lockwooddcc31942011-07-11 15:04:38 -040037// MtpServer fields
38static jfieldID field_MtpServer_nativeContext;
Mike Lockwoodb239b6832011-04-05 10:21:27 -040039
40// MtpStorage fields
41static jfieldID field_MtpStorage_storageId;
42static jfieldID field_MtpStorage_path;
43static jfieldID field_MtpStorage_description;
44static jfieldID field_MtpStorage_reserveSpace;
Mike Lockwood51690542011-05-09 20:16:05 -070045static jfieldID field_MtpStorage_removable;
Mike Lockwood7a59dd22011-07-11 09:18:03 -040046static jfieldID field_MtpStorage_maxFileSize;
Mike Lockwoodb239b6832011-04-05 10:21:27 -040047
48static Mutex sMutex;
49
Mike Lockwood98ef64e2010-06-29 16:42:13 -040050// ----------------------------------------------------------------------------
51
Mike Lockwood0cd01362010-12-30 11:54:33 -050052// in android_mtp_MtpDatabase.cpp
Mike Lockwoodd21eac92010-07-03 00:44:05 -040053extern MtpDatabase* getMtpDatabase(JNIEnv *env, jobject database);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040054
Mike Lockwooddcc31942011-07-11 15:04:38 -040055static inline MtpServer* getMtpServer(JNIEnv *env, jobject thiz) {
Ashok Bhate2e59322013-12-17 19:04:19 +000056 return (MtpServer*)env->GetLongField(thiz, field_MtpServer_nativeContext);
Mike Lockwood98ef64e2010-06-29 16:42:13 -040057}
58
Jerry Zhangbb598ee2016-10-24 14:35:08 -070059static void android_mtp_configure(JNIEnv *, jobject, jboolean usePtp) {
60 MtpServer::configure(usePtp);
61}
62
Mike Lockwood98ef64e2010-06-29 16:42:13 -040063static void
Mike Lockwood7d40d422011-06-21 08:27:06 -040064android_mtp_MtpServer_setup(JNIEnv *env, jobject thiz, jobject javaDatabase, jboolean usePtp)
Mike Lockwood98ef64e2010-06-29 16:42:13 -040065{
Jerry Zhangbb598ee2016-10-24 14:35:08 -070066 MtpServer* server = new MtpServer(getMtpDatabase(env, javaDatabase),
67 usePtp, AID_MEDIA_RW, 0664, 0775);
68 env->SetLongField(thiz, field_MtpServer_nativeContext, (jlong)server);
Mike Lockwooddcc31942011-07-11 15:04:38 -040069}
70
71static void
72android_mtp_MtpServer_run(JNIEnv *env, jobject thiz)
73{
74 MtpServer* server = getMtpServer(env, thiz);
75 if (server)
76 server->run();
77 else
Steve Block3762c312012-01-06 19:20:56 +000078 ALOGE("server is null in run");
Mike Lockwooddcc31942011-07-11 15:04:38 -040079}
80
81static void
82android_mtp_MtpServer_cleanup(JNIEnv *env, jobject thiz)
83{
84 Mutex::Autolock autoLock(sMutex);
85
86 MtpServer* server = getMtpServer(env, thiz);
87 if (server) {
88 delete server;
Ashok Bhate2e59322013-12-17 19:04:19 +000089 env->SetLongField(thiz, field_MtpServer_nativeContext, 0);
Mike Lockwooddcc31942011-07-11 15:04:38 -040090 } else {
Steve Block3762c312012-01-06 19:20:56 +000091 ALOGE("server is null in cleanup");
Mike Lockwooddcc31942011-07-11 15:04:38 -040092 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -040093}
94
Mike Lockwoodbe125a52010-07-12 18:54:16 -040095static void
Mike Lockwood0cd01362010-12-30 11:54:33 -050096android_mtp_MtpServer_send_object_added(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -040097{
Mike Lockwooddcc31942011-07-11 15:04:38 -040098 Mutex::Autolock autoLock(sMutex);
99
100 MtpServer* server = getMtpServer(env, thiz);
101 if (server)
102 server->sendObjectAdded(handle);
103 else
Steve Block3762c312012-01-06 19:20:56 +0000104 ALOGE("server is null in send_object_added");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400105}
106
107static void
Mike Lockwood0cd01362010-12-30 11:54:33 -0500108android_mtp_MtpServer_send_object_removed(JNIEnv *env, jobject thiz, jint handle)
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400109{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400110 Mutex::Autolock autoLock(sMutex);
111
112 MtpServer* server = getMtpServer(env, thiz);
113 if (server)
114 server->sendObjectRemoved(handle);
115 else
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("server is null in send_object_removed");
Mike Lockwoodbe125a52010-07-12 18:54:16 -0400117}
118
Mike Lockwoodeabe8bf2010-08-31 14:35:23 -0400119static void
Mike Lockwood56c85242014-03-07 13:29:08 -0800120android_mtp_MtpServer_send_device_property_changed(JNIEnv *env, jobject thiz, jint property)
121{
122 Mutex::Autolock autoLock(sMutex);
123
124 MtpServer* server = getMtpServer(env, thiz);
125 if (server)
126 server->sendDevicePropertyChanged(property);
127 else
128 ALOGE("server is null in send_object_removed");
129}
130
131static void
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400132android_mtp_MtpServer_add_storage(JNIEnv *env, jobject thiz, jobject jstorage)
Mike Lockwood66e57f62011-02-18 13:24:01 -0500133{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400134 Mutex::Autolock autoLock(sMutex);
135
136 MtpServer* server = getMtpServer(env, thiz);
137 if (server) {
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400138 jint storageID = env->GetIntField(jstorage, field_MtpStorage_storageId);
139 jstring path = (jstring)env->GetObjectField(jstorage, field_MtpStorage_path);
140 jstring description = (jstring)env->GetObjectField(jstorage, field_MtpStorage_description);
141 jlong reserveSpace = env->GetLongField(jstorage, field_MtpStorage_reserveSpace);
Mike Lockwood51690542011-05-09 20:16:05 -0700142 jboolean removable = env->GetBooleanField(jstorage, field_MtpStorage_removable);
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400143 jlong maxFileSize = env->GetLongField(jstorage, field_MtpStorage_maxFileSize);
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400144
145 const char *pathStr = env->GetStringUTFChars(path, NULL);
James Dong39774722011-04-06 11:57:48 -0700146 if (pathStr != NULL) {
147 const char *descriptionStr = env->GetStringUTFChars(description, NULL);
148 if (descriptionStr != NULL) {
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400149 MtpStorage* storage = new MtpStorage(storageID, pathStr, descriptionStr,
150 reserveSpace, removable, maxFileSize);
Mike Lockwooddcc31942011-07-11 15:04:38 -0400151 server->addStorage(storage);
James Dong39774722011-04-06 11:57:48 -0700152 env->ReleaseStringUTFChars(path, pathStr);
153 env->ReleaseStringUTFChars(description, descriptionStr);
154 } else {
155 env->ReleaseStringUTFChars(path, pathStr);
156 }
157 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400158 } else {
Steve Block3762c312012-01-06 19:20:56 +0000159 ALOGE("server is null in add_storage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400160 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400161}
162
163static void
164android_mtp_MtpServer_remove_storage(JNIEnv *env, jobject thiz, jint storageId)
165{
Mike Lockwooddcc31942011-07-11 15:04:38 -0400166 Mutex::Autolock autoLock(sMutex);
167
168 MtpServer* server = getMtpServer(env, thiz);
169 if (server) {
170 MtpStorage* storage = server->getStorage(storageId);
171 if (storage) {
172 server->removeStorage(storage);
173 delete storage;
174 }
175 } else
Steve Block3762c312012-01-06 19:20:56 +0000176 ALOGE("server is null in remove_storage");
Mike Lockwood66e57f62011-02-18 13:24:01 -0500177}
178
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400179// ----------------------------------------------------------------------------
180
Daniel Micay76f6a862015-09-19 17:31:01 -0400181static const JNINativeMethod gMethods[] = {
Jerry Zhangbb598ee2016-10-24 14:35:08 -0700182 {"native_configure", "(Z)V", (void *)android_mtp_configure},
Mike Lockwood7d40d422011-06-21 08:27:06 -0400183 {"native_setup", "(Landroid/mtp/MtpDatabase;Z)V",
Mike Lockwood0cd01362010-12-30 11:54:33 -0500184 (void *)android_mtp_MtpServer_setup},
Mike Lockwooddcc31942011-07-11 15:04:38 -0400185 {"native_run", "()V", (void *)android_mtp_MtpServer_run},
186 {"native_cleanup", "()V", (void *)android_mtp_MtpServer_cleanup},
Mike Lockwood0cd01362010-12-30 11:54:33 -0500187 {"native_send_object_added", "(I)V", (void *)android_mtp_MtpServer_send_object_added},
188 {"native_send_object_removed", "(I)V", (void *)android_mtp_MtpServer_send_object_removed},
Mike Lockwood56c85242014-03-07 13:29:08 -0800189 {"native_send_device_property_changed", "(I)V",
190 (void *)android_mtp_MtpServer_send_device_property_changed},
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400191 {"native_add_storage", "(Landroid/mtp/MtpStorage;)V",
192 (void *)android_mtp_MtpServer_add_storage},
193 {"native_remove_storage", "(I)V", (void *)android_mtp_MtpServer_remove_storage},
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400194};
195
Mike Lockwood0cd01362010-12-30 11:54:33 -0500196int register_android_mtp_MtpServer(JNIEnv *env)
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400197{
198 jclass clazz;
199
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400200 clazz = env->FindClass("android/mtp/MtpStorage");
201 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000202 ALOGE("Can't find android/mtp/MtpStorage");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400203 return -1;
204 }
205 field_MtpStorage_storageId = env->GetFieldID(clazz, "mStorageId", "I");
206 if (field_MtpStorage_storageId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000207 ALOGE("Can't find MtpStorage.mStorageId");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400208 return -1;
209 }
210 field_MtpStorage_path = env->GetFieldID(clazz, "mPath", "Ljava/lang/String;");
211 if (field_MtpStorage_path == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000212 ALOGE("Can't find MtpStorage.mPath");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400213 return -1;
214 }
215 field_MtpStorage_description = env->GetFieldID(clazz, "mDescription", "Ljava/lang/String;");
216 if (field_MtpStorage_description == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000217 ALOGE("Can't find MtpStorage.mDescription");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400218 return -1;
219 }
220 field_MtpStorage_reserveSpace = env->GetFieldID(clazz, "mReserveSpace", "J");
221 if (field_MtpStorage_reserveSpace == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000222 ALOGE("Can't find MtpStorage.mReserveSpace");
Mike Lockwood51690542011-05-09 20:16:05 -0700223 return -1;
224 }
225 field_MtpStorage_removable = env->GetFieldID(clazz, "mRemovable", "Z");
226 if (field_MtpStorage_removable == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000227 ALOGE("Can't find MtpStorage.mRemovable");
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400228 return -1;
229 }
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400230 field_MtpStorage_maxFileSize = env->GetFieldID(clazz, "mMaxFileSize", "J");
231 if (field_MtpStorage_maxFileSize == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000232 ALOGE("Can't find MtpStorage.mMaxFileSize");
Mike Lockwood7a59dd22011-07-11 09:18:03 -0400233 return -1;
234 }
Mike Lockwoodb239b6832011-04-05 10:21:27 -0400235
Mike Lockwood0cd01362010-12-30 11:54:33 -0500236 clazz = env->FindClass("android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400237 if (clazz == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000238 ALOGE("Can't find android/mtp/MtpServer");
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400239 return -1;
240 }
Ashok Bhate2e59322013-12-17 19:04:19 +0000241 field_MtpServer_nativeContext = env->GetFieldID(clazz, "mNativeContext", "J");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400242 if (field_MtpServer_nativeContext == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000243 ALOGE("Can't find MtpServer.mNativeContext");
Mike Lockwooddcc31942011-07-11 15:04:38 -0400244 return -1;
245 }
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400246
247 return AndroidRuntime::registerNativeMethods(env,
Mike Lockwood0cd01362010-12-30 11:54:33 -0500248 "android/mtp/MtpServer", gMethods, NELEM(gMethods));
Mike Lockwood98ef64e2010-06-29 16:42:13 -0400249}